UNPKG

35.7 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports, "__esModule", {value: true});/* eslint max-len: 0 */
2
3var _index = require('../index');
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19var _flow = require('../plugins/flow');
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36var _typescript = require('../plugins/typescript');
37
38
39
40
41
42
43
44
45
46var _tokenizer = require('../tokenizer');
47var _keywords = require('../tokenizer/keywords');
48var _state = require('../tokenizer/state');
49var _types = require('../tokenizer/types');
50var _base = require('./base');
51
52
53
54
55
56
57
58
59
60
61
62var _expression = require('./expression');
63var _lval = require('./lval');
64
65
66
67
68
69
70
71
72
73var _util = require('./util');
74
75 function parseTopLevel() {
76 parseBlockBody(_types.TokenType.eof);
77 _base.state.scopes.push(new (0, _state.Scope)(0, _base.state.tokens.length, true));
78 if (_base.state.scopeDepth !== 0) {
79 throw new Error(`Invalid scope depth at end of file: ${_base.state.scopeDepth}`);
80 }
81 return new (0, _index.File)(_base.state.tokens, _base.state.scopes);
82} exports.parseTopLevel = parseTopLevel;
83
84// Parse a single statement.
85//
86// If expecting a statement and finding a slash operator, parse a
87// regular expression literal. This is to handle cases like
88// `if (foo) /blah/.exec(foo)`, where looking at the previous token
89// does not help.
90
91 function parseStatement(declaration) {
92 if (_base.isFlowEnabled) {
93 if (_flow.flowTryParseStatement.call(void 0, )) {
94 return;
95 }
96 }
97 if (_tokenizer.match.call(void 0, _types.TokenType.at)) {
98 parseDecorators();
99 }
100 parseStatementContent(declaration);
101} exports.parseStatement = parseStatement;
102
103function parseStatementContent(declaration) {
104 if (_base.isTypeScriptEnabled) {
105 if (_typescript.tsTryParseStatementContent.call(void 0, )) {
106 return;
107 }
108 }
109
110 const starttype = _base.state.type;
111
112 // Most types of statements are recognized by the keyword they
113 // start with. Many are trivial to parse, some require a bit of
114 // complexity.
115
116 switch (starttype) {
117 case _types.TokenType._break:
118 case _types.TokenType._continue:
119 parseBreakContinueStatement();
120 return;
121 case _types.TokenType._debugger:
122 parseDebuggerStatement();
123 return;
124 case _types.TokenType._do:
125 parseDoStatement();
126 return;
127 case _types.TokenType._for:
128 parseForStatement();
129 return;
130 case _types.TokenType._function:
131 if (_tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.dot) break;
132 if (!declaration) _util.unexpected.call(void 0, );
133 parseFunctionStatement();
134 return;
135
136 case _types.TokenType._class:
137 if (!declaration) _util.unexpected.call(void 0, );
138 parseClass(true);
139 return;
140
141 case _types.TokenType._if:
142 parseIfStatement();
143 return;
144 case _types.TokenType._return:
145 parseReturnStatement();
146 return;
147 case _types.TokenType._switch:
148 parseSwitchStatement();
149 return;
150 case _types.TokenType._throw:
151 parseThrowStatement();
152 return;
153 case _types.TokenType._try:
154 parseTryStatement();
155 return;
156
157 case _types.TokenType._let:
158 case _types.TokenType._const:
159 if (!declaration) _util.unexpected.call(void 0, ); // NOTE: falls through to _var
160
161 case _types.TokenType._var:
162 parseVarStatement(starttype);
163 return;
164
165 case _types.TokenType._while:
166 parseWhileStatement();
167 return;
168 case _types.TokenType.braceL:
169 parseBlock();
170 return;
171 case _types.TokenType.semi:
172 parseEmptyStatement();
173 return;
174 case _types.TokenType._export:
175 case _types.TokenType._import: {
176 const nextType = _tokenizer.lookaheadType.call(void 0, );
177 if (nextType === _types.TokenType.parenL || nextType === _types.TokenType.dot) {
178 break;
179 }
180 _tokenizer.next.call(void 0, );
181 if (starttype === _types.TokenType._import) {
182 parseImport();
183 } else {
184 parseExport();
185 }
186 return;
187 }
188 case _types.TokenType.name:
189 if (_base.state.contextualKeyword === _keywords.ContextualKeyword._async) {
190 const functionStart = _base.state.start;
191 // peek ahead and see if next token is a function
192 const snapshot = _base.state.snapshot();
193 _tokenizer.next.call(void 0, );
194 if (_tokenizer.match.call(void 0, _types.TokenType._function) && !_util.canInsertSemicolon.call(void 0, )) {
195 _util.expect.call(void 0, _types.TokenType._function);
196 parseFunction(functionStart, true, false);
197 return;
198 } else {
199 _base.state.restoreFromSnapshot(snapshot);
200 }
201 }
202 default:
203 // Do nothing.
204 break;
205 }
206
207 // If the statement does not start with a statement keyword or a
208 // brace, it's an ExpressionStatement or LabeledStatement. We
209 // simply start parsing an expression, and afterwards, if the
210 // next token is a colon and the expression was a simple
211 // Identifier node, we switch to interpreting it as a label.
212 const initialTokensLength = _base.state.tokens.length;
213 _expression.parseExpression.call(void 0, );
214 let simpleName = null;
215 if (_base.state.tokens.length === initialTokensLength + 1) {
216 const token = _base.state.tokens[_base.state.tokens.length - 1];
217 if (token.type === _types.TokenType.name) {
218 simpleName = token.contextualKeyword;
219 }
220 }
221 if (simpleName == null) {
222 _util.semicolon.call(void 0, );
223 return;
224 }
225 if (_tokenizer.eat.call(void 0, _types.TokenType.colon)) {
226 parseLabeledStatement();
227 } else {
228 // This was an identifier, so we might want to handle flow/typescript-specific cases.
229 parseIdentifierStatement(simpleName);
230 }
231}
232
233 function parseDecorators() {
234 while (_tokenizer.match.call(void 0, _types.TokenType.at)) {
235 parseDecorator();
236 }
237} exports.parseDecorators = parseDecorators;
238
239function parseDecorator() {
240 _tokenizer.next.call(void 0, );
241 if (_tokenizer.eat.call(void 0, _types.TokenType.parenL)) {
242 _expression.parseExpression.call(void 0, );
243 _util.expect.call(void 0, _types.TokenType.parenR);
244 } else {
245 _expression.parseIdentifier.call(void 0, );
246 while (_tokenizer.eat.call(void 0, _types.TokenType.dot)) {
247 _expression.parseIdentifier.call(void 0, );
248 }
249 }
250 parseMaybeDecoratorArguments();
251}
252
253function parseMaybeDecoratorArguments() {
254 if (_base.isTypeScriptEnabled) {
255 _typescript.tsParseMaybeDecoratorArguments.call(void 0, );
256 } else {
257 baseParseMaybeDecoratorArguments();
258 }
259}
260
261 function baseParseMaybeDecoratorArguments() {
262 if (_tokenizer.eat.call(void 0, _types.TokenType.parenL)) {
263 _expression.parseCallExpressionArguments.call(void 0, );
264 }
265} exports.baseParseMaybeDecoratorArguments = baseParseMaybeDecoratorArguments;
266
267function parseBreakContinueStatement() {
268 _tokenizer.next.call(void 0, );
269 if (!_util.isLineTerminator.call(void 0, )) {
270 _expression.parseIdentifier.call(void 0, );
271 _util.semicolon.call(void 0, );
272 }
273}
274
275function parseDebuggerStatement() {
276 _tokenizer.next.call(void 0, );
277 _util.semicolon.call(void 0, );
278}
279
280function parseDoStatement() {
281 _tokenizer.next.call(void 0, );
282 parseStatement(false);
283 _util.expect.call(void 0, _types.TokenType._while);
284 _expression.parseParenExpression.call(void 0, );
285 _tokenizer.eat.call(void 0, _types.TokenType.semi);
286}
287
288function parseForStatement() {
289 _base.state.scopeDepth++;
290 const startTokenIndex = _base.state.tokens.length;
291 parseAmbiguousForStatement();
292 const endTokenIndex = _base.state.tokens.length;
293 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, false));
294 _base.state.scopeDepth--;
295}
296
297// Disambiguating between a `for` and a `for`/`in` or `for`/`of`
298// loop is non-trivial. Basically, we have to parse the init `var`
299// statement or expression, disallowing the `in` operator (see
300// the second parameter to `parseExpression`), and then check
301// whether the next token is `in` or `of`. When there is no init
302// part (semicolon immediately after the opening parenthesis), it
303// is a regular `for` loop.
304function parseAmbiguousForStatement() {
305 _tokenizer.next.call(void 0, );
306
307 let forAwait = false;
308 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._await)) {
309 forAwait = true;
310 _tokenizer.next.call(void 0, );
311 }
312 _util.expect.call(void 0, _types.TokenType.parenL);
313
314 if (_tokenizer.match.call(void 0, _types.TokenType.semi)) {
315 if (forAwait) {
316 _util.unexpected.call(void 0, );
317 }
318 parseFor();
319 return;
320 }
321
322 if (_tokenizer.match.call(void 0, _types.TokenType._var) || _tokenizer.match.call(void 0, _types.TokenType._let) || _tokenizer.match.call(void 0, _types.TokenType._const)) {
323 const varKind = _base.state.type;
324 _tokenizer.next.call(void 0, );
325 parseVar(true, varKind);
326 if (_tokenizer.match.call(void 0, _types.TokenType._in) || _util.isContextual.call(void 0, _keywords.ContextualKeyword._of)) {
327 parseForIn(forAwait);
328 return;
329 }
330 parseFor();
331 return;
332 }
333
334 _expression.parseExpression.call(void 0, true);
335 if (_tokenizer.match.call(void 0, _types.TokenType._in) || _util.isContextual.call(void 0, _keywords.ContextualKeyword._of)) {
336 parseForIn(forAwait);
337 return;
338 }
339 if (forAwait) {
340 _util.unexpected.call(void 0, );
341 }
342 parseFor();
343}
344
345function parseFunctionStatement() {
346 const functionStart = _base.state.start;
347 _tokenizer.next.call(void 0, );
348 parseFunction(functionStart, true);
349}
350
351function parseIfStatement() {
352 _tokenizer.next.call(void 0, );
353 _expression.parseParenExpression.call(void 0, );
354 parseStatement(false);
355 if (_tokenizer.eat.call(void 0, _types.TokenType._else)) {
356 parseStatement(false);
357 }
358}
359
360function parseReturnStatement() {
361 _tokenizer.next.call(void 0, );
362
363 // In `return` (and `break`/`continue`), the keywords with
364 // optional arguments, we eagerly look for a semicolon or the
365 // possibility to insert one.
366
367 if (!_util.isLineTerminator.call(void 0, )) {
368 _expression.parseExpression.call(void 0, );
369 _util.semicolon.call(void 0, );
370 }
371}
372
373function parseSwitchStatement() {
374 _tokenizer.next.call(void 0, );
375 _expression.parseParenExpression.call(void 0, );
376 _base.state.scopeDepth++;
377 const startTokenIndex = _base.state.tokens.length;
378 _util.expect.call(void 0, _types.TokenType.braceL);
379
380 // Don't bother validation; just go through any sequence of cases, defaults, and statements.
381 while (!_tokenizer.match.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
382 if (_tokenizer.match.call(void 0, _types.TokenType._case) || _tokenizer.match.call(void 0, _types.TokenType._default)) {
383 const isCase = _tokenizer.match.call(void 0, _types.TokenType._case);
384 _tokenizer.next.call(void 0, );
385 if (isCase) {
386 _expression.parseExpression.call(void 0, );
387 }
388 _util.expect.call(void 0, _types.TokenType.colon);
389 } else {
390 parseStatement(true);
391 }
392 }
393 _tokenizer.next.call(void 0, ); // Closing brace
394 const endTokenIndex = _base.state.tokens.length;
395 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, false));
396 _base.state.scopeDepth--;
397}
398
399function parseThrowStatement() {
400 _tokenizer.next.call(void 0, );
401 _expression.parseExpression.call(void 0, );
402 _util.semicolon.call(void 0, );
403}
404
405function parseTryStatement() {
406 _tokenizer.next.call(void 0, );
407
408 parseBlock();
409
410 if (_tokenizer.match.call(void 0, _types.TokenType._catch)) {
411 _tokenizer.next.call(void 0, );
412 let catchBindingStartTokenIndex = null;
413 if (_tokenizer.match.call(void 0, _types.TokenType.parenL)) {
414 _base.state.scopeDepth++;
415 catchBindingStartTokenIndex = _base.state.tokens.length;
416 _util.expect.call(void 0, _types.TokenType.parenL);
417 _lval.parseBindingAtom.call(void 0, true /* isBlockScope */);
418 _util.expect.call(void 0, _types.TokenType.parenR);
419 }
420 parseBlock();
421 if (catchBindingStartTokenIndex != null) {
422 // We need a special scope for the catch binding which includes the binding itself and the
423 // catch block.
424 const endTokenIndex = _base.state.tokens.length;
425 _base.state.scopes.push(new (0, _state.Scope)(catchBindingStartTokenIndex, endTokenIndex, false));
426 _base.state.scopeDepth--;
427 }
428 }
429 if (_tokenizer.eat.call(void 0, _types.TokenType._finally)) {
430 parseBlock();
431 }
432}
433
434 function parseVarStatement(kind) {
435 _tokenizer.next.call(void 0, );
436 parseVar(false, kind);
437 _util.semicolon.call(void 0, );
438} exports.parseVarStatement = parseVarStatement;
439
440function parseWhileStatement() {
441 _tokenizer.next.call(void 0, );
442 _expression.parseParenExpression.call(void 0, );
443 parseStatement(false);
444}
445
446function parseEmptyStatement() {
447 _tokenizer.next.call(void 0, );
448}
449
450function parseLabeledStatement() {
451 parseStatement(true);
452}
453
454/**
455 * Parse a statement starting with an identifier of the given name. Subclasses match on the name
456 * to handle statements like "declare".
457 */
458function parseIdentifierStatement(contextualKeyword) {
459 if (_base.isTypeScriptEnabled) {
460 _typescript.tsParseIdentifierStatement.call(void 0, contextualKeyword);
461 } else if (_base.isFlowEnabled) {
462 _flow.flowParseIdentifierStatement.call(void 0, contextualKeyword);
463 } else {
464 _util.semicolon.call(void 0, );
465 }
466}
467
468// Parse a semicolon-enclosed block of statements, handling `"use
469// strict"` declarations when `allowStrict` is true (used for
470// function bodies).
471
472 function parseBlock(
473 allowDirectives = false,
474 isFunctionScope = false,
475 contextId = 0,
476) {
477 const startTokenIndex = _base.state.tokens.length;
478 _base.state.scopeDepth++;
479 _util.expect.call(void 0, _types.TokenType.braceL);
480 if (contextId) {
481 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
482 }
483 parseBlockBody(_types.TokenType.braceR);
484 if (contextId) {
485 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
486 }
487 const endTokenIndex = _base.state.tokens.length;
488 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, isFunctionScope));
489 _base.state.scopeDepth--;
490} exports.parseBlock = parseBlock;
491
492 function parseBlockBody(end) {
493 while (!_tokenizer.eat.call(void 0, end) && !_base.state.error) {
494 parseStatement(true);
495 }
496} exports.parseBlockBody = parseBlockBody;
497
498// Parse a regular `for` loop. The disambiguation code in
499// `parseStatement` will already have parsed the init statement or
500// expression.
501
502function parseFor() {
503 _util.expect.call(void 0, _types.TokenType.semi);
504 if (!_tokenizer.match.call(void 0, _types.TokenType.semi)) {
505 _expression.parseExpression.call(void 0, );
506 }
507 _util.expect.call(void 0, _types.TokenType.semi);
508 if (!_tokenizer.match.call(void 0, _types.TokenType.parenR)) {
509 _expression.parseExpression.call(void 0, );
510 }
511 _util.expect.call(void 0, _types.TokenType.parenR);
512 parseStatement(false);
513}
514
515// Parse a `for`/`in` and `for`/`of` loop, which are almost
516// same from parser's perspective.
517
518function parseForIn(forAwait) {
519 if (forAwait) {
520 _util.eatContextual.call(void 0, _keywords.ContextualKeyword._of);
521 } else {
522 _tokenizer.next.call(void 0, );
523 }
524 _expression.parseExpression.call(void 0, );
525 _util.expect.call(void 0, _types.TokenType.parenR);
526 parseStatement(false);
527}
528
529// Parse a list of variable declarations.
530
531function parseVar(isFor, kind) {
532 while (true) {
533 const isBlockScope = kind === _types.TokenType._const || kind === _types.TokenType._let;
534 parseVarHead(isBlockScope);
535 if (_tokenizer.eat.call(void 0, _types.TokenType.eq)) {
536 const eqIndex = _base.state.tokens.length - 1;
537 _expression.parseMaybeAssign.call(void 0, isFor);
538 _base.state.tokens[eqIndex].rhsEndIndex = _base.state.tokens.length;
539 }
540 if (!_tokenizer.eat.call(void 0, _types.TokenType.comma)) {
541 break;
542 }
543 }
544}
545
546function parseVarHead(isBlockScope) {
547 _lval.parseBindingAtom.call(void 0, isBlockScope);
548 if (_base.isTypeScriptEnabled) {
549 _typescript.tsAfterParseVarHead.call(void 0, );
550 } else if (_base.isFlowEnabled) {
551 _flow.flowAfterParseVarHead.call(void 0, );
552 }
553}
554
555// Parse a function declaration or literal (depending on the
556// `isStatement` parameter).
557
558 function parseFunction(
559 functionStart,
560 isStatement,
561 allowExpressionBody = false,
562 optionalId = false,
563) {
564 let isGenerator = false;
565 if (_tokenizer.match.call(void 0, _types.TokenType.star)) {
566 isGenerator = true;
567 _tokenizer.next.call(void 0, );
568 }
569
570 if (isStatement && !optionalId && !_tokenizer.match.call(void 0, _types.TokenType.name) && !_tokenizer.match.call(void 0, _types.TokenType._yield)) {
571 _util.unexpected.call(void 0, );
572 }
573
574 let nameScopeStartTokenIndex = null;
575
576 if (_tokenizer.match.call(void 0, _types.TokenType.name)) {
577 // Expression-style functions should limit their name's scope to the function body, so we make
578 // a new function scope to enforce that.
579 if (!isStatement) {
580 nameScopeStartTokenIndex = _base.state.tokens.length;
581 _base.state.scopeDepth++;
582 }
583 _lval.parseBindingIdentifier.call(void 0, false);
584 }
585
586 const startTokenIndex = _base.state.tokens.length;
587 _base.state.scopeDepth++;
588 parseFunctionParams();
589 _expression.parseFunctionBodyAndFinish.call(void 0, functionStart, isGenerator, allowExpressionBody);
590 const endTokenIndex = _base.state.tokens.length;
591 // In addition to the block scope of the function body, we need a separate function-style scope
592 // that includes the params.
593 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, true));
594 _base.state.scopeDepth--;
595 if (nameScopeStartTokenIndex !== null) {
596 _base.state.scopes.push(new (0, _state.Scope)(nameScopeStartTokenIndex, endTokenIndex, true));
597 _base.state.scopeDepth--;
598 }
599} exports.parseFunction = parseFunction;
600
601 function parseFunctionParams(
602 allowModifiers = false,
603 funcContextId = 0,
604) {
605 if (_base.isTypeScriptEnabled) {
606 _typescript.tsStartParseFunctionParams.call(void 0, );
607 } else if (_base.isFlowEnabled) {
608 _flow.flowStartParseFunctionParams.call(void 0, );
609 }
610
611 _util.expect.call(void 0, _types.TokenType.parenL);
612 if (funcContextId) {
613 _base.state.tokens[_base.state.tokens.length - 1].contextId = funcContextId;
614 }
615 _lval.parseBindingList.call(void 0, _types.TokenType.parenR, false /* isBlockScope */, false /* allowEmpty */, allowModifiers);
616 if (funcContextId) {
617 _base.state.tokens[_base.state.tokens.length - 1].contextId = funcContextId;
618 }
619} exports.parseFunctionParams = parseFunctionParams;
620
621// Parse a class declaration or literal (depending on the
622// `isStatement` parameter).
623
624 function parseClass(isStatement, optionalId = false) {
625 // Put a context ID on the class keyword, the open-brace, and the close-brace, so that later
626 // code can easily navigate to meaningful points on the class.
627 const contextId = _base.getNextContextId.call(void 0, );
628
629 _tokenizer.next.call(void 0, );
630 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
631 _base.state.tokens[_base.state.tokens.length - 1].isExpression = !isStatement;
632 // Like with functions, we declare a special "name scope" from the start of the name to the end
633 // of the class, but only with expression-style classes, to represent the fact that the name is
634 // available to the body of the class but not an outer declaration.
635 let nameScopeStartTokenIndex = null;
636 if (!isStatement) {
637 nameScopeStartTokenIndex = _base.state.tokens.length;
638 _base.state.scopeDepth++;
639 }
640 parseClassId(isStatement, optionalId);
641 parseClassSuper();
642 const openBraceIndex = _base.state.tokens.length;
643 parseClassBody(contextId);
644 if (_base.state.error) {
645 return;
646 }
647 _base.state.tokens[openBraceIndex].contextId = contextId;
648 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
649 if (nameScopeStartTokenIndex !== null) {
650 const endTokenIndex = _base.state.tokens.length;
651 _base.state.scopes.push(new (0, _state.Scope)(nameScopeStartTokenIndex, endTokenIndex, false));
652 _base.state.scopeDepth--;
653 }
654} exports.parseClass = parseClass;
655
656function isClassProperty() {
657 return _tokenizer.match.call(void 0, _types.TokenType.eq) || _tokenizer.match.call(void 0, _types.TokenType.semi) || _tokenizer.match.call(void 0, _types.TokenType.braceR) || _tokenizer.match.call(void 0, _types.TokenType.bang) || _tokenizer.match.call(void 0, _types.TokenType.colon);
658}
659
660function isClassMethod() {
661 return _tokenizer.match.call(void 0, _types.TokenType.parenL) || _tokenizer.match.call(void 0, _types.TokenType.lessThan);
662}
663
664function parseClassBody(classContextId) {
665 _util.expect.call(void 0, _types.TokenType.braceL);
666
667 while (!_tokenizer.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
668 if (_tokenizer.eat.call(void 0, _types.TokenType.semi)) {
669 continue;
670 }
671
672 if (_tokenizer.match.call(void 0, _types.TokenType.at)) {
673 parseDecorator();
674 continue;
675 }
676 const memberStart = _base.state.start;
677 parseClassMember(memberStart, classContextId);
678 }
679}
680
681function parseClassMember(memberStart, classContextId) {
682 if (_base.isTypeScriptEnabled) {
683 _typescript.tsParseAccessModifier.call(void 0, );
684 }
685 let isStatic = false;
686 if (_tokenizer.match.call(void 0, _types.TokenType.name) && _base.state.contextualKeyword === _keywords.ContextualKeyword._static) {
687 _expression.parseIdentifier.call(void 0, ); // eats 'static'
688 if (isClassMethod()) {
689 parseClassMethod(memberStart, false, /* isConstructor */ false);
690 return;
691 } else if (isClassProperty()) {
692 parseClassProperty();
693 return;
694 }
695 // otherwise something static
696 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._static;
697 isStatic = true;
698 }
699
700 parseClassMemberWithIsStatic(memberStart, isStatic, classContextId);
701}
702
703function parseClassMemberWithIsStatic(
704 memberStart,
705 isStatic,
706 classContextId,
707) {
708 if (_base.isTypeScriptEnabled) {
709 if (_typescript.tsTryParseClassMemberWithIsStatic.call(void 0, isStatic, classContextId)) {
710 return;
711 }
712 }
713 if (_tokenizer.eat.call(void 0, _types.TokenType.star)) {
714 // a generator
715 parseClassPropertyName(classContextId);
716 parseClassMethod(memberStart, true, /* isConstructor */ false);
717 return;
718 }
719
720 // Get the identifier name so we can tell if it's actually a keyword like "async", "get", or
721 // "set".
722 parseClassPropertyName(classContextId);
723 let isConstructor = false;
724 const token = _base.state.tokens[_base.state.tokens.length - 1];
725 // We allow "constructor" as either an identifier or a string.
726 if (token.contextualKeyword === _keywords.ContextualKeyword._constructor) {
727 isConstructor = true;
728 }
729 parsePostMemberNameModifiers();
730
731 if (isClassMethod()) {
732 parseClassMethod(memberStart, false, isConstructor);
733 } else if (isClassProperty()) {
734 parseClassProperty();
735 } else if (token.contextualKeyword === _keywords.ContextualKeyword._async && !_util.isLineTerminator.call(void 0, )) {
736 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._async;
737 // an async method
738 const isGenerator = _tokenizer.match.call(void 0, _types.TokenType.star);
739 if (isGenerator) {
740 _tokenizer.next.call(void 0, );
741 }
742
743 // The so-called parsed name would have been "async": get the real name.
744 parseClassPropertyName(classContextId);
745 parseClassMethod(memberStart, isGenerator, false /* isConstructor */);
746 } else if (
747 (token.contextualKeyword === _keywords.ContextualKeyword._get ||
748 token.contextualKeyword === _keywords.ContextualKeyword._set) &&
749 !(_util.isLineTerminator.call(void 0, ) && _tokenizer.match.call(void 0, _types.TokenType.star))
750 ) {
751 if (token.contextualKeyword === _keywords.ContextualKeyword._get) {
752 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._get;
753 } else {
754 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._set;
755 }
756 // `get\n*` is an uninitialized property named 'get' followed by a generator.
757 // a getter or setter
758 // The so-called parsed name would have been "get/set": get the real name.
759 parseClassPropertyName(classContextId);
760 parseClassMethod(memberStart, false, /* isConstructor */ false);
761 } else if (_util.isLineTerminator.call(void 0, )) {
762 // an uninitialized class property (due to ASI, since we don't otherwise recognize the next token)
763 parseClassProperty();
764 } else {
765 _util.unexpected.call(void 0, );
766 }
767}
768
769function parseClassMethod(
770 functionStart,
771 isGenerator,
772 isConstructor,
773) {
774 if (_base.isTypeScriptEnabled) {
775 _typescript.tsTryParseTypeParameters.call(void 0, );
776 } else if (_base.isFlowEnabled) {
777 if (_tokenizer.match.call(void 0, _types.TokenType.lessThan)) {
778 _flow.flowParseTypeParameterDeclaration.call(void 0, );
779 }
780 }
781 _expression.parseMethod.call(void 0, functionStart, isGenerator, isConstructor);
782}
783
784// Return the name of the class property, if it is a simple identifier.
785 function parseClassPropertyName(classContextId) {
786 _expression.parsePropertyName.call(void 0, classContextId);
787} exports.parseClassPropertyName = parseClassPropertyName;
788
789 function parsePostMemberNameModifiers() {
790 if (_base.isTypeScriptEnabled) {
791 const oldIsType = _tokenizer.pushTypeContext.call(void 0, 0);
792 _tokenizer.eat.call(void 0, _types.TokenType.question);
793 _tokenizer.popTypeContext.call(void 0, oldIsType);
794 }
795} exports.parsePostMemberNameModifiers = parsePostMemberNameModifiers;
796
797 function parseClassProperty() {
798 if (_base.isTypeScriptEnabled) {
799 _tokenizer.eat.call(void 0, _types.TokenType.bang);
800 _typescript.tsTryParseTypeAnnotation.call(void 0, );
801 } else if (_base.isFlowEnabled) {
802 if (_tokenizer.match.call(void 0, _types.TokenType.colon)) {
803 _flow.flowParseTypeAnnotation.call(void 0, );
804 }
805 }
806
807 if (_tokenizer.match.call(void 0, _types.TokenType.eq)) {
808 const equalsTokenIndex = _base.state.tokens.length;
809 _tokenizer.next.call(void 0, );
810 _expression.parseMaybeAssign.call(void 0, );
811 _base.state.tokens[equalsTokenIndex].rhsEndIndex = _base.state.tokens.length;
812 }
813 _util.semicolon.call(void 0, );
814} exports.parseClassProperty = parseClassProperty;
815
816function parseClassId(isStatement, optionalId = false) {
817 if (
818 _base.isTypeScriptEnabled &&
819 (!isStatement || optionalId) &&
820 _util.isContextual.call(void 0, _keywords.ContextualKeyword._implements)
821 ) {
822 return;
823 }
824
825 if (_tokenizer.match.call(void 0, _types.TokenType.name)) {
826 _lval.parseBindingIdentifier.call(void 0, true);
827 }
828
829 if (_base.isTypeScriptEnabled) {
830 _typescript.tsTryParseTypeParameters.call(void 0, );
831 } else if (_base.isFlowEnabled) {
832 if (_tokenizer.match.call(void 0, _types.TokenType.lessThan)) {
833 _flow.flowParseTypeParameterDeclaration.call(void 0, );
834 }
835 }
836}
837
838// Returns true if there was a superclass.
839function parseClassSuper() {
840 let hasSuper = false;
841 if (_tokenizer.eat.call(void 0, _types.TokenType._extends)) {
842 _expression.parseExprSubscripts.call(void 0, );
843 hasSuper = true;
844 } else {
845 hasSuper = false;
846 }
847 if (_base.isTypeScriptEnabled) {
848 _typescript.tsAfterParseClassSuper.call(void 0, hasSuper);
849 } else if (_base.isFlowEnabled) {
850 _flow.flowAfterParseClassSuper.call(void 0, hasSuper);
851 }
852}
853
854// Parses module export declaration.
855
856 function parseExport() {
857 if (_base.isTypeScriptEnabled) {
858 if (_typescript.tsTryParseExport.call(void 0, )) {
859 return;
860 }
861 }
862 // export * from '...'
863 if (shouldParseExportStar()) {
864 parseExportStar();
865 } else if (isExportDefaultSpecifier()) {
866 // export default from
867 _expression.parseIdentifier.call(void 0, );
868 if (_tokenizer.match.call(void 0, _types.TokenType.comma) && _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.star) {
869 _util.expect.call(void 0, _types.TokenType.comma);
870 _util.expect.call(void 0, _types.TokenType.star);
871 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._as);
872 _expression.parseIdentifier.call(void 0, );
873 } else {
874 parseExportSpecifiersMaybe();
875 }
876 parseExportFrom();
877 } else if (_tokenizer.eat.call(void 0, _types.TokenType._default)) {
878 // export default ...
879 parseExportDefaultExpression();
880 } else if (shouldParseExportDeclaration()) {
881 parseExportDeclaration();
882 } else {
883 // export { x, y as z } [from '...']
884 parseExportSpecifiers();
885 parseExportFrom();
886 }
887} exports.parseExport = parseExport;
888
889function parseExportDefaultExpression() {
890 if (_base.isTypeScriptEnabled) {
891 if (_typescript.tsTryParseExportDefaultExpression.call(void 0, )) {
892 return;
893 }
894 }
895 const functionStart = _base.state.start;
896 if (_tokenizer.eat.call(void 0, _types.TokenType._function)) {
897 parseFunction(functionStart, true, false, true);
898 } else if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._async) && _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType._function) {
899 // async function declaration
900 _util.eatContextual.call(void 0, _keywords.ContextualKeyword._async);
901 _tokenizer.eat.call(void 0, _types.TokenType._function);
902 parseFunction(functionStart, true, false, true);
903 } else if (_tokenizer.match.call(void 0, _types.TokenType._class)) {
904 parseClass(true, true);
905 } else if (_tokenizer.match.call(void 0, _types.TokenType.at)) {
906 parseDecorators();
907 parseClass(true, true);
908 } else {
909 _expression.parseMaybeAssign.call(void 0, );
910 _util.semicolon.call(void 0, );
911 }
912}
913
914function parseExportDeclaration() {
915 if (_base.isTypeScriptEnabled) {
916 _typescript.tsParseExportDeclaration.call(void 0, );
917 } else if (_base.isFlowEnabled) {
918 _flow.flowParseExportDeclaration.call(void 0, );
919 } else {
920 parseStatement(true);
921 }
922}
923
924function isExportDefaultSpecifier() {
925 if (_base.isTypeScriptEnabled && _typescript.tsIsDeclarationStart.call(void 0, )) {
926 return false;
927 } else if (_base.isFlowEnabled && _flow.flowShouldDisallowExportDefaultSpecifier.call(void 0, )) {
928 return false;
929 }
930 if (_tokenizer.match.call(void 0, _types.TokenType.name)) {
931 return _base.state.contextualKeyword !== _keywords.ContextualKeyword._async;
932 }
933
934 if (!_tokenizer.match.call(void 0, _types.TokenType._default)) {
935 return false;
936 }
937
938 const lookahead = _tokenizer.lookaheadTypeAndKeyword.call(void 0, );
939 return (
940 lookahead.type === _types.TokenType.comma ||
941 (lookahead.type === _types.TokenType.name && lookahead.contextualKeyword === _keywords.ContextualKeyword._from)
942 );
943}
944
945function parseExportSpecifiersMaybe() {
946 if (_tokenizer.eat.call(void 0, _types.TokenType.comma)) {
947 parseExportSpecifiers();
948 }
949}
950
951 function parseExportFrom() {
952 if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._from)) {
953 _expression.parseExprAtom.call(void 0, );
954 }
955 _util.semicolon.call(void 0, );
956} exports.parseExportFrom = parseExportFrom;
957
958function shouldParseExportStar() {
959 if (_base.isFlowEnabled) {
960 return _flow.flowShouldParseExportStar.call(void 0, );
961 } else {
962 return _tokenizer.match.call(void 0, _types.TokenType.star);
963 }
964}
965
966function parseExportStar() {
967 if (_base.isFlowEnabled) {
968 _flow.flowParseExportStar.call(void 0, );
969 } else {
970 baseParseExportStar();
971 }
972}
973
974 function baseParseExportStar() {
975 _util.expect.call(void 0, _types.TokenType.star);
976
977 if (_util.isContextual.call(void 0, _keywords.ContextualKeyword._as)) {
978 parseExportNamespace();
979 } else {
980 parseExportFrom();
981 }
982} exports.baseParseExportStar = baseParseExportStar;
983
984function parseExportNamespace() {
985 _tokenizer.next.call(void 0, );
986 _base.state.tokens[_base.state.tokens.length - 1].type = _types.TokenType._as;
987 _expression.parseIdentifier.call(void 0, );
988 parseExportSpecifiersMaybe();
989 parseExportFrom();
990}
991
992function shouldParseExportDeclaration() {
993 return (
994 (_base.isTypeScriptEnabled && _typescript.tsIsDeclarationStart.call(void 0, )) ||
995 (_base.isFlowEnabled && _flow.flowShouldParseExportDeclaration.call(void 0, )) ||
996 _base.state.type === _types.TokenType._var ||
997 _base.state.type === _types.TokenType._const ||
998 _base.state.type === _types.TokenType._let ||
999 _base.state.type === _types.TokenType._function ||
1000 _base.state.type === _types.TokenType._class ||
1001 _util.isContextual.call(void 0, _keywords.ContextualKeyword._async) ||
1002 _tokenizer.match.call(void 0, _types.TokenType.at)
1003 );
1004}
1005
1006// Parses a comma-separated list of module exports.
1007 function parseExportSpecifiers() {
1008 let first = true;
1009
1010 // export { x, y as z } [from '...']
1011 _util.expect.call(void 0, _types.TokenType.braceL);
1012
1013 while (!_tokenizer.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
1014 if (first) {
1015 first = false;
1016 } else {
1017 _util.expect.call(void 0, _types.TokenType.comma);
1018 if (_tokenizer.eat.call(void 0, _types.TokenType.braceR)) {
1019 break;
1020 }
1021 }
1022
1023 _expression.parseIdentifier.call(void 0, );
1024 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _tokenizer.IdentifierRole.ExportAccess;
1025 if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._as)) {
1026 _expression.parseIdentifier.call(void 0, );
1027 }
1028 }
1029} exports.parseExportSpecifiers = parseExportSpecifiers;
1030
1031// Parses import declaration.
1032
1033 function parseImport() {
1034 if (_base.isTypeScriptEnabled && _tokenizer.match.call(void 0, _types.TokenType.name) && _tokenizer.lookaheadType.call(void 0, ) === _types.TokenType.eq) {
1035 _typescript.tsParseImportEqualsDeclaration.call(void 0, );
1036 return;
1037 }
1038
1039 // import '...'
1040 if (_tokenizer.match.call(void 0, _types.TokenType.string)) {
1041 _expression.parseExprAtom.call(void 0, );
1042 } else {
1043 parseImportSpecifiers();
1044 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._from);
1045 _expression.parseExprAtom.call(void 0, );
1046 }
1047 _util.semicolon.call(void 0, );
1048} exports.parseImport = parseImport;
1049
1050// eslint-disable-next-line no-unused-vars
1051function shouldParseDefaultImport() {
1052 return _tokenizer.match.call(void 0, _types.TokenType.name);
1053}
1054
1055function parseImportSpecifierLocal() {
1056 _expression.parseIdentifier.call(void 0, );
1057}
1058
1059// Parses a comma-separated list of module imports.
1060function parseImportSpecifiers() {
1061 if (_base.isFlowEnabled) {
1062 _flow.flowStartParseImportSpecifiers.call(void 0, );
1063 }
1064
1065 let first = true;
1066 if (shouldParseDefaultImport()) {
1067 // import defaultObj, { x, y as z } from '...'
1068 parseImportSpecifierLocal();
1069
1070 if (!_tokenizer.eat.call(void 0, _types.TokenType.comma)) return;
1071 }
1072
1073 if (_tokenizer.match.call(void 0, _types.TokenType.star)) {
1074 _tokenizer.next.call(void 0, );
1075 _util.expectContextual.call(void 0, _keywords.ContextualKeyword._as);
1076
1077 parseImportSpecifierLocal();
1078
1079 return;
1080 }
1081
1082 _util.expect.call(void 0, _types.TokenType.braceL);
1083 while (!_tokenizer.eat.call(void 0, _types.TokenType.braceR) && !_base.state.error) {
1084 if (first) {
1085 first = false;
1086 } else {
1087 // Detect an attempt to deep destructure
1088 if (_tokenizer.eat.call(void 0, _types.TokenType.colon)) {
1089 _util.unexpected.call(void 0,
1090 "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
1091 );
1092 }
1093
1094 _util.expect.call(void 0, _types.TokenType.comma);
1095 if (_tokenizer.eat.call(void 0, _types.TokenType.braceR)) {
1096 break;
1097 }
1098 }
1099
1100 parseImportSpecifier();
1101 }
1102}
1103
1104function parseImportSpecifier() {
1105 if (_base.isFlowEnabled) {
1106 _flow.flowParseImportSpecifier.call(void 0, );
1107 return;
1108 }
1109 _expression.parseIdentifier.call(void 0, );
1110 if (_util.eatContextual.call(void 0, _keywords.ContextualKeyword._as)) {
1111 _expression.parseIdentifier.call(void 0, );
1112 }
1113}