UNPKG

27.6 kBJavaScriptView Raw
1/* eslint max-len: 0 */
2
3import {File} from "../index";
4import {
5 flowAfterParseClassSuper,
6 flowAfterParseVarHead,
7 flowParseExportDeclaration,
8 flowParseExportStar,
9 flowParseIdentifierStatement,
10 flowParseImportSpecifier,
11 flowParseTypeAnnotation,
12 flowParseTypeParameterDeclaration,
13 flowShouldDisallowExportDefaultSpecifier,
14 flowShouldParseExportDeclaration,
15 flowShouldParseExportStar,
16 flowStartParseFunctionParams,
17 flowStartParseImportSpecifiers,
18 flowTryParseStatement,
19} from "../plugins/flow";
20import {
21 tsAfterParseClassSuper,
22 tsAfterParseVarHead,
23 tsIsDeclarationStart,
24 tsParseAccessModifier,
25 tsParseExportDeclaration,
26 tsParseIdentifierStatement,
27 tsParseImportEqualsDeclaration,
28 tsParseMaybeDecoratorArguments,
29 tsStartParseFunctionParams,
30 tsTryParseClassMemberWithIsStatic,
31 tsTryParseExport,
32 tsTryParseExportDefaultExpression,
33 tsTryParseStatementContent,
34 tsTryParseTypeAnnotation,
35 tsTryParseTypeParameters,
36} from "../plugins/typescript";
37import {
38 eat,
39 IdentifierRole,
40 lookaheadType,
41 lookaheadTypeAndKeyword,
42 match,
43 next,
44 popTypeContext,
45 pushTypeContext,
46} from "../tokenizer";
47import {ContextualKeyword} from "../tokenizer/keywords";
48import {Scope} from "../tokenizer/state";
49import { TokenType as tt} from "../tokenizer/types";
50import {getNextContextId, isFlowEnabled, isTypeScriptEnabled, state} from "./base";
51import {
52 parseCallExpressionArguments,
53 parseExprAtom,
54 parseExpression,
55 parseExprSubscripts,
56 parseFunctionBodyAndFinish,
57 parseIdentifier,
58 parseMaybeAssign,
59 parseMethod,
60 parseParenExpression,
61 parsePropertyName,
62} from "./expression";
63import {parseBindingAtom, parseBindingIdentifier, parseBindingList} from "./lval";
64import {
65 canInsertSemicolon,
66 eatContextual,
67 expect,
68 expectContextual,
69 isContextual,
70 isLineTerminator,
71 semicolon,
72 unexpected,
73} from "./util";
74
75export function parseTopLevel() {
76 parseBlockBody(tt.eof);
77 state.scopes.push(new Scope(0, state.tokens.length, true));
78 if (state.scopeDepth !== 0) {
79 throw new Error(`Invalid scope depth at end of file: ${state.scopeDepth}`);
80 }
81 return new File(state.tokens, state.scopes);
82}
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
91export function parseStatement(declaration) {
92 if (isFlowEnabled) {
93 if (flowTryParseStatement()) {
94 return;
95 }
96 }
97 if (match(tt.at)) {
98 parseDecorators();
99 }
100 parseStatementContent(declaration);
101}
102
103function parseStatementContent(declaration) {
104 if (isTypeScriptEnabled) {
105 if (tsTryParseStatementContent()) {
106 return;
107 }
108 }
109
110 const starttype = 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 tt._break:
118 case tt._continue:
119 parseBreakContinueStatement();
120 return;
121 case tt._debugger:
122 parseDebuggerStatement();
123 return;
124 case tt._do:
125 parseDoStatement();
126 return;
127 case tt._for:
128 parseForStatement();
129 return;
130 case tt._function:
131 if (lookaheadType() === tt.dot) break;
132 if (!declaration) unexpected();
133 parseFunctionStatement();
134 return;
135
136 case tt._class:
137 if (!declaration) unexpected();
138 parseClass(true);
139 return;
140
141 case tt._if:
142 parseIfStatement();
143 return;
144 case tt._return:
145 parseReturnStatement();
146 return;
147 case tt._switch:
148 parseSwitchStatement();
149 return;
150 case tt._throw:
151 parseThrowStatement();
152 return;
153 case tt._try:
154 parseTryStatement();
155 return;
156
157 case tt._let:
158 case tt._const:
159 if (!declaration) unexpected(); // NOTE: falls through to _var
160
161 case tt._var:
162 parseVarStatement(starttype);
163 return;
164
165 case tt._while:
166 parseWhileStatement();
167 return;
168 case tt.braceL:
169 parseBlock();
170 return;
171 case tt.semi:
172 parseEmptyStatement();
173 return;
174 case tt._export:
175 case tt._import: {
176 const nextType = lookaheadType();
177 if (nextType === tt.parenL || nextType === tt.dot) {
178 break;
179 }
180 next();
181 if (starttype === tt._import) {
182 parseImport();
183 } else {
184 parseExport();
185 }
186 return;
187 }
188 case tt.name:
189 if (state.contextualKeyword === ContextualKeyword._async) {
190 const functionStart = state.start;
191 // peek ahead and see if next token is a function
192 const snapshot = state.snapshot();
193 next();
194 if (match(tt._function) && !canInsertSemicolon()) {
195 expect(tt._function);
196 parseFunction(functionStart, true, false);
197 return;
198 } else {
199 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 = state.tokens.length;
213 parseExpression();
214 let simpleName = null;
215 if (state.tokens.length === initialTokensLength + 1) {
216 const token = state.tokens[state.tokens.length - 1];
217 if (token.type === tt.name) {
218 simpleName = token.contextualKeyword;
219 }
220 }
221 if (simpleName == null) {
222 semicolon();
223 return;
224 }
225 if (eat(tt.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
233export function parseDecorators() {
234 while (match(tt.at)) {
235 parseDecorator();
236 }
237}
238
239function parseDecorator() {
240 next();
241 if (eat(tt.parenL)) {
242 parseExpression();
243 expect(tt.parenR);
244 } else {
245 parseIdentifier();
246 while (eat(tt.dot)) {
247 parseIdentifier();
248 }
249 }
250 parseMaybeDecoratorArguments();
251}
252
253function parseMaybeDecoratorArguments() {
254 if (isTypeScriptEnabled) {
255 tsParseMaybeDecoratorArguments();
256 } else {
257 baseParseMaybeDecoratorArguments();
258 }
259}
260
261export function baseParseMaybeDecoratorArguments() {
262 if (eat(tt.parenL)) {
263 parseCallExpressionArguments();
264 }
265}
266
267function parseBreakContinueStatement() {
268 next();
269 if (!isLineTerminator()) {
270 parseIdentifier();
271 semicolon();
272 }
273}
274
275function parseDebuggerStatement() {
276 next();
277 semicolon();
278}
279
280function parseDoStatement() {
281 next();
282 parseStatement(false);
283 expect(tt._while);
284 parseParenExpression();
285 eat(tt.semi);
286}
287
288function parseForStatement() {
289 state.scopeDepth++;
290 const startTokenIndex = state.tokens.length;
291 parseAmbiguousForStatement();
292 const endTokenIndex = state.tokens.length;
293 state.scopes.push(new Scope(startTokenIndex, endTokenIndex, false));
294 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 next();
306
307 let forAwait = false;
308 if (isContextual(ContextualKeyword._await)) {
309 forAwait = true;
310 next();
311 }
312 expect(tt.parenL);
313
314 if (match(tt.semi)) {
315 if (forAwait) {
316 unexpected();
317 }
318 parseFor();
319 return;
320 }
321
322 if (match(tt._var) || match(tt._let) || match(tt._const)) {
323 const varKind = state.type;
324 next();
325 parseVar(true, varKind);
326 if (match(tt._in) || isContextual(ContextualKeyword._of)) {
327 parseForIn(forAwait);
328 return;
329 }
330 parseFor();
331 return;
332 }
333
334 parseExpression(true);
335 if (match(tt._in) || isContextual(ContextualKeyword._of)) {
336 parseForIn(forAwait);
337 return;
338 }
339 if (forAwait) {
340 unexpected();
341 }
342 parseFor();
343}
344
345function parseFunctionStatement() {
346 const functionStart = state.start;
347 next();
348 parseFunction(functionStart, true);
349}
350
351function parseIfStatement() {
352 next();
353 parseParenExpression();
354 parseStatement(false);
355 if (eat(tt._else)) {
356 parseStatement(false);
357 }
358}
359
360function parseReturnStatement() {
361 next();
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 (!isLineTerminator()) {
368 parseExpression();
369 semicolon();
370 }
371}
372
373function parseSwitchStatement() {
374 next();
375 parseParenExpression();
376 state.scopeDepth++;
377 const startTokenIndex = state.tokens.length;
378 expect(tt.braceL);
379
380 // Don't bother validation; just go through any sequence of cases, defaults, and statements.
381 while (!match(tt.braceR) && !state.error) {
382 if (match(tt._case) || match(tt._default)) {
383 const isCase = match(tt._case);
384 next();
385 if (isCase) {
386 parseExpression();
387 }
388 expect(tt.colon);
389 } else {
390 parseStatement(true);
391 }
392 }
393 next(); // Closing brace
394 const endTokenIndex = state.tokens.length;
395 state.scopes.push(new Scope(startTokenIndex, endTokenIndex, false));
396 state.scopeDepth--;
397}
398
399function parseThrowStatement() {
400 next();
401 parseExpression();
402 semicolon();
403}
404
405function parseTryStatement() {
406 next();
407
408 parseBlock();
409
410 if (match(tt._catch)) {
411 next();
412 let catchBindingStartTokenIndex = null;
413 if (match(tt.parenL)) {
414 state.scopeDepth++;
415 catchBindingStartTokenIndex = state.tokens.length;
416 expect(tt.parenL);
417 parseBindingAtom(true /* isBlockScope */);
418 expect(tt.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 = state.tokens.length;
425 state.scopes.push(new Scope(catchBindingStartTokenIndex, endTokenIndex, false));
426 state.scopeDepth--;
427 }
428 }
429 if (eat(tt._finally)) {
430 parseBlock();
431 }
432}
433
434export function parseVarStatement(kind) {
435 next();
436 parseVar(false, kind);
437 semicolon();
438}
439
440function parseWhileStatement() {
441 next();
442 parseParenExpression();
443 parseStatement(false);
444}
445
446function parseEmptyStatement() {
447 next();
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 (isTypeScriptEnabled) {
460 tsParseIdentifierStatement(contextualKeyword);
461 } else if (isFlowEnabled) {
462 flowParseIdentifierStatement(contextualKeyword);
463 } else {
464 semicolon();
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
472export function parseBlock(
473 allowDirectives = false,
474 isFunctionScope = false,
475 contextId = 0,
476) {
477 const startTokenIndex = state.tokens.length;
478 state.scopeDepth++;
479 expect(tt.braceL);
480 if (contextId) {
481 state.tokens[state.tokens.length - 1].contextId = contextId;
482 }
483 parseBlockBody(tt.braceR);
484 if (contextId) {
485 state.tokens[state.tokens.length - 1].contextId = contextId;
486 }
487 const endTokenIndex = state.tokens.length;
488 state.scopes.push(new Scope(startTokenIndex, endTokenIndex, isFunctionScope));
489 state.scopeDepth--;
490}
491
492export function parseBlockBody(end) {
493 while (!eat(end) && !state.error) {
494 parseStatement(true);
495 }
496}
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 expect(tt.semi);
504 if (!match(tt.semi)) {
505 parseExpression();
506 }
507 expect(tt.semi);
508 if (!match(tt.parenR)) {
509 parseExpression();
510 }
511 expect(tt.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 eatContextual(ContextualKeyword._of);
521 } else {
522 next();
523 }
524 parseExpression();
525 expect(tt.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 === tt._const || kind === tt._let;
534 parseVarHead(isBlockScope);
535 if (eat(tt.eq)) {
536 const eqIndex = state.tokens.length - 1;
537 parseMaybeAssign(isFor);
538 state.tokens[eqIndex].rhsEndIndex = state.tokens.length;
539 }
540 if (!eat(tt.comma)) {
541 break;
542 }
543 }
544}
545
546function parseVarHead(isBlockScope) {
547 parseBindingAtom(isBlockScope);
548 if (isTypeScriptEnabled) {
549 tsAfterParseVarHead();
550 } else if (isFlowEnabled) {
551 flowAfterParseVarHead();
552 }
553}
554
555// Parse a function declaration or literal (depending on the
556// `isStatement` parameter).
557
558export function parseFunction(
559 functionStart,
560 isStatement,
561 allowExpressionBody = false,
562 optionalId = false,
563) {
564 let isGenerator = false;
565 if (match(tt.star)) {
566 isGenerator = true;
567 next();
568 }
569
570 if (isStatement && !optionalId && !match(tt.name) && !match(tt._yield)) {
571 unexpected();
572 }
573
574 let nameScopeStartTokenIndex = null;
575
576 if (match(tt.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 = state.tokens.length;
581 state.scopeDepth++;
582 }
583 parseBindingIdentifier(false);
584 }
585
586 const startTokenIndex = state.tokens.length;
587 state.scopeDepth++;
588 parseFunctionParams();
589 parseFunctionBodyAndFinish(functionStart, isGenerator, allowExpressionBody);
590 const endTokenIndex = 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 state.scopes.push(new Scope(startTokenIndex, endTokenIndex, true));
594 state.scopeDepth--;
595 if (nameScopeStartTokenIndex !== null) {
596 state.scopes.push(new Scope(nameScopeStartTokenIndex, endTokenIndex, true));
597 state.scopeDepth--;
598 }
599}
600
601export function parseFunctionParams(
602 allowModifiers = false,
603 funcContextId = 0,
604) {
605 if (isTypeScriptEnabled) {
606 tsStartParseFunctionParams();
607 } else if (isFlowEnabled) {
608 flowStartParseFunctionParams();
609 }
610
611 expect(tt.parenL);
612 if (funcContextId) {
613 state.tokens[state.tokens.length - 1].contextId = funcContextId;
614 }
615 parseBindingList(tt.parenR, false /* isBlockScope */, false /* allowEmpty */, allowModifiers);
616 if (funcContextId) {
617 state.tokens[state.tokens.length - 1].contextId = funcContextId;
618 }
619}
620
621// Parse a class declaration or literal (depending on the
622// `isStatement` parameter).
623
624export 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 = getNextContextId();
628
629 next();
630 state.tokens[state.tokens.length - 1].contextId = contextId;
631 state.tokens[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 = state.tokens.length;
638 state.scopeDepth++;
639 }
640 parseClassId(isStatement, optionalId);
641 parseClassSuper();
642 const openBraceIndex = state.tokens.length;
643 parseClassBody(contextId);
644 if (state.error) {
645 return;
646 }
647 state.tokens[openBraceIndex].contextId = contextId;
648 state.tokens[state.tokens.length - 1].contextId = contextId;
649 if (nameScopeStartTokenIndex !== null) {
650 const endTokenIndex = state.tokens.length;
651 state.scopes.push(new Scope(nameScopeStartTokenIndex, endTokenIndex, false));
652 state.scopeDepth--;
653 }
654}
655
656function isClassProperty() {
657 return match(tt.eq) || match(tt.semi) || match(tt.braceR) || match(tt.bang) || match(tt.colon);
658}
659
660function isClassMethod() {
661 return match(tt.parenL) || match(tt.lessThan);
662}
663
664function parseClassBody(classContextId) {
665 expect(tt.braceL);
666
667 while (!eat(tt.braceR) && !state.error) {
668 if (eat(tt.semi)) {
669 continue;
670 }
671
672 if (match(tt.at)) {
673 parseDecorator();
674 continue;
675 }
676 const memberStart = state.start;
677 parseClassMember(memberStart, classContextId);
678 }
679}
680
681function parseClassMember(memberStart, classContextId) {
682 if (isTypeScriptEnabled) {
683 tsParseAccessModifier();
684 }
685 let isStatic = false;
686 if (match(tt.name) && state.contextualKeyword === ContextualKeyword._static) {
687 parseIdentifier(); // 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 state.tokens[state.tokens.length - 1].type = tt._static;
697 isStatic = true;
698 }
699
700 parseClassMemberWithIsStatic(memberStart, isStatic, classContextId);
701}
702
703function parseClassMemberWithIsStatic(
704 memberStart,
705 isStatic,
706 classContextId,
707) {
708 if (isTypeScriptEnabled) {
709 if (tsTryParseClassMemberWithIsStatic(isStatic, classContextId)) {
710 return;
711 }
712 }
713 if (eat(tt.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 = state.tokens[state.tokens.length - 1];
725 // We allow "constructor" as either an identifier or a string.
726 if (token.contextualKeyword === 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 === ContextualKeyword._async && !isLineTerminator()) {
736 state.tokens[state.tokens.length - 1].type = tt._async;
737 // an async method
738 const isGenerator = match(tt.star);
739 if (isGenerator) {
740 next();
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 === ContextualKeyword._get ||
748 token.contextualKeyword === ContextualKeyword._set) &&
749 !(isLineTerminator() && match(tt.star))
750 ) {
751 if (token.contextualKeyword === ContextualKeyword._get) {
752 state.tokens[state.tokens.length - 1].type = tt._get;
753 } else {
754 state.tokens[state.tokens.length - 1].type = tt._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 (isLineTerminator()) {
762 // an uninitialized class property (due to ASI, since we don't otherwise recognize the next token)
763 parseClassProperty();
764 } else {
765 unexpected();
766 }
767}
768
769function parseClassMethod(
770 functionStart,
771 isGenerator,
772 isConstructor,
773) {
774 if (isTypeScriptEnabled) {
775 tsTryParseTypeParameters();
776 } else if (isFlowEnabled) {
777 if (match(tt.lessThan)) {
778 flowParseTypeParameterDeclaration();
779 }
780 }
781 parseMethod(functionStart, isGenerator, isConstructor);
782}
783
784// Return the name of the class property, if it is a simple identifier.
785export function parseClassPropertyName(classContextId) {
786 parsePropertyName(classContextId);
787}
788
789export function parsePostMemberNameModifiers() {
790 if (isTypeScriptEnabled) {
791 const oldIsType = pushTypeContext(0);
792 eat(tt.question);
793 popTypeContext(oldIsType);
794 }
795}
796
797export function parseClassProperty() {
798 if (isTypeScriptEnabled) {
799 eat(tt.bang);
800 tsTryParseTypeAnnotation();
801 } else if (isFlowEnabled) {
802 if (match(tt.colon)) {
803 flowParseTypeAnnotation();
804 }
805 }
806
807 if (match(tt.eq)) {
808 const equalsTokenIndex = state.tokens.length;
809 next();
810 parseMaybeAssign();
811 state.tokens[equalsTokenIndex].rhsEndIndex = state.tokens.length;
812 }
813 semicolon();
814}
815
816function parseClassId(isStatement, optionalId = false) {
817 if (
818 isTypeScriptEnabled &&
819 (!isStatement || optionalId) &&
820 isContextual(ContextualKeyword._implements)
821 ) {
822 return;
823 }
824
825 if (match(tt.name)) {
826 parseBindingIdentifier(true);
827 }
828
829 if (isTypeScriptEnabled) {
830 tsTryParseTypeParameters();
831 } else if (isFlowEnabled) {
832 if (match(tt.lessThan)) {
833 flowParseTypeParameterDeclaration();
834 }
835 }
836}
837
838// Returns true if there was a superclass.
839function parseClassSuper() {
840 let hasSuper = false;
841 if (eat(tt._extends)) {
842 parseExprSubscripts();
843 hasSuper = true;
844 } else {
845 hasSuper = false;
846 }
847 if (isTypeScriptEnabled) {
848 tsAfterParseClassSuper(hasSuper);
849 } else if (isFlowEnabled) {
850 flowAfterParseClassSuper(hasSuper);
851 }
852}
853
854// Parses module export declaration.
855
856export function parseExport() {
857 if (isTypeScriptEnabled) {
858 if (tsTryParseExport()) {
859 return;
860 }
861 }
862 // export * from '...'
863 if (shouldParseExportStar()) {
864 parseExportStar();
865 } else if (isExportDefaultSpecifier()) {
866 // export default from
867 parseIdentifier();
868 if (match(tt.comma) && lookaheadType() === tt.star) {
869 expect(tt.comma);
870 expect(tt.star);
871 expectContextual(ContextualKeyword._as);
872 parseIdentifier();
873 } else {
874 parseExportSpecifiersMaybe();
875 }
876 parseExportFrom();
877 } else if (eat(tt._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}
888
889function parseExportDefaultExpression() {
890 if (isTypeScriptEnabled) {
891 if (tsTryParseExportDefaultExpression()) {
892 return;
893 }
894 }
895 const functionStart = state.start;
896 if (eat(tt._function)) {
897 parseFunction(functionStart, true, false, true);
898 } else if (isContextual(ContextualKeyword._async) && lookaheadType() === tt._function) {
899 // async function declaration
900 eatContextual(ContextualKeyword._async);
901 eat(tt._function);
902 parseFunction(functionStart, true, false, true);
903 } else if (match(tt._class)) {
904 parseClass(true, true);
905 } else if (match(tt.at)) {
906 parseDecorators();
907 parseClass(true, true);
908 } else {
909 parseMaybeAssign();
910 semicolon();
911 }
912}
913
914function parseExportDeclaration() {
915 if (isTypeScriptEnabled) {
916 tsParseExportDeclaration();
917 } else if (isFlowEnabled) {
918 flowParseExportDeclaration();
919 } else {
920 parseStatement(true);
921 }
922}
923
924function isExportDefaultSpecifier() {
925 if (isTypeScriptEnabled && tsIsDeclarationStart()) {
926 return false;
927 } else if (isFlowEnabled && flowShouldDisallowExportDefaultSpecifier()) {
928 return false;
929 }
930 if (match(tt.name)) {
931 return state.contextualKeyword !== ContextualKeyword._async;
932 }
933
934 if (!match(tt._default)) {
935 return false;
936 }
937
938 const lookahead = lookaheadTypeAndKeyword();
939 return (
940 lookahead.type === tt.comma ||
941 (lookahead.type === tt.name && lookahead.contextualKeyword === ContextualKeyword._from)
942 );
943}
944
945function parseExportSpecifiersMaybe() {
946 if (eat(tt.comma)) {
947 parseExportSpecifiers();
948 }
949}
950
951export function parseExportFrom() {
952 if (eatContextual(ContextualKeyword._from)) {
953 parseExprAtom();
954 }
955 semicolon();
956}
957
958function shouldParseExportStar() {
959 if (isFlowEnabled) {
960 return flowShouldParseExportStar();
961 } else {
962 return match(tt.star);
963 }
964}
965
966function parseExportStar() {
967 if (isFlowEnabled) {
968 flowParseExportStar();
969 } else {
970 baseParseExportStar();
971 }
972}
973
974export function baseParseExportStar() {
975 expect(tt.star);
976
977 if (isContextual(ContextualKeyword._as)) {
978 parseExportNamespace();
979 } else {
980 parseExportFrom();
981 }
982}
983
984function parseExportNamespace() {
985 next();
986 state.tokens[state.tokens.length - 1].type = tt._as;
987 parseIdentifier();
988 parseExportSpecifiersMaybe();
989 parseExportFrom();
990}
991
992function shouldParseExportDeclaration() {
993 return (
994 (isTypeScriptEnabled && tsIsDeclarationStart()) ||
995 (isFlowEnabled && flowShouldParseExportDeclaration()) ||
996 state.type === tt._var ||
997 state.type === tt._const ||
998 state.type === tt._let ||
999 state.type === tt._function ||
1000 state.type === tt._class ||
1001 isContextual(ContextualKeyword._async) ||
1002 match(tt.at)
1003 );
1004}
1005
1006// Parses a comma-separated list of module exports.
1007export function parseExportSpecifiers() {
1008 let first = true;
1009
1010 // export { x, y as z } [from '...']
1011 expect(tt.braceL);
1012
1013 while (!eat(tt.braceR) && !state.error) {
1014 if (first) {
1015 first = false;
1016 } else {
1017 expect(tt.comma);
1018 if (eat(tt.braceR)) {
1019 break;
1020 }
1021 }
1022
1023 parseIdentifier();
1024 state.tokens[state.tokens.length - 1].identifierRole = IdentifierRole.ExportAccess;
1025 if (eatContextual(ContextualKeyword._as)) {
1026 parseIdentifier();
1027 }
1028 }
1029}
1030
1031// Parses import declaration.
1032
1033export function parseImport() {
1034 if (isTypeScriptEnabled && match(tt.name) && lookaheadType() === tt.eq) {
1035 tsParseImportEqualsDeclaration();
1036 return;
1037 }
1038
1039 // import '...'
1040 if (match(tt.string)) {
1041 parseExprAtom();
1042 } else {
1043 parseImportSpecifiers();
1044 expectContextual(ContextualKeyword._from);
1045 parseExprAtom();
1046 }
1047 semicolon();
1048}
1049
1050// eslint-disable-next-line no-unused-vars
1051function shouldParseDefaultImport() {
1052 return match(tt.name);
1053}
1054
1055function parseImportSpecifierLocal() {
1056 parseIdentifier();
1057}
1058
1059// Parses a comma-separated list of module imports.
1060function parseImportSpecifiers() {
1061 if (isFlowEnabled) {
1062 flowStartParseImportSpecifiers();
1063 }
1064
1065 let first = true;
1066 if (shouldParseDefaultImport()) {
1067 // import defaultObj, { x, y as z } from '...'
1068 parseImportSpecifierLocal();
1069
1070 if (!eat(tt.comma)) return;
1071 }
1072
1073 if (match(tt.star)) {
1074 next();
1075 expectContextual(ContextualKeyword._as);
1076
1077 parseImportSpecifierLocal();
1078
1079 return;
1080 }
1081
1082 expect(tt.braceL);
1083 while (!eat(tt.braceR) && !state.error) {
1084 if (first) {
1085 first = false;
1086 } else {
1087 // Detect an attempt to deep destructure
1088 if (eat(tt.colon)) {
1089 unexpected(
1090 "ES2015 named imports do not destructure. Use another statement for destructuring after the import.",
1091 );
1092 }
1093
1094 expect(tt.comma);
1095 if (eat(tt.braceR)) {
1096 break;
1097 }
1098 }
1099
1100 parseImportSpecifier();
1101 }
1102}
1103
1104function parseImportSpecifier() {
1105 if (isFlowEnabled) {
1106 flowParseImportSpecifier();
1107 return;
1108 }
1109 parseIdentifier();
1110 if (eatContextual(ContextualKeyword._as)) {
1111 parseIdentifier();
1112 }
1113}