1 |
|
2 |
|
3 | import {File} from "../index";
|
4 | import {
|
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";
|
20 | import {
|
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";
|
37 | import {
|
38 | eat,
|
39 | IdentifierRole,
|
40 | lookaheadType,
|
41 | lookaheadTypeAndKeyword,
|
42 | match,
|
43 | next,
|
44 | popTypeContext,
|
45 | pushTypeContext,
|
46 | } from "../tokenizer";
|
47 | import {ContextualKeyword} from "../tokenizer/keywords";
|
48 | import {Scope} from "../tokenizer/state";
|
49 | import { TokenType as tt} from "../tokenizer/types";
|
50 | import {getNextContextId, isFlowEnabled, isTypeScriptEnabled, state} from "./base";
|
51 | import {
|
52 | parseCallExpressionArguments,
|
53 | parseExprAtom,
|
54 | parseExpression,
|
55 | parseExprSubscripts,
|
56 | parseFunctionBodyAndFinish,
|
57 | parseIdentifier,
|
58 | parseMaybeAssign,
|
59 | parseMethod,
|
60 | parseParenExpression,
|
61 | parsePropertyName,
|
62 | } from "./expression";
|
63 | import {parseBindingAtom, parseBindingIdentifier, parseBindingList} from "./lval";
|
64 | import {
|
65 | canInsertSemicolon,
|
66 | eatContextual,
|
67 | expect,
|
68 | expectContextual,
|
69 | isContextual,
|
70 | isLineTerminator,
|
71 | semicolon,
|
72 | unexpected,
|
73 | } from "./util";
|
74 |
|
75 | export 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 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | export 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 |
|
103 | function parseStatementContent(declaration) {
|
104 | if (isTypeScriptEnabled) {
|
105 | if (tsTryParseStatementContent()) {
|
106 | return;
|
107 | }
|
108 | }
|
109 |
|
110 | const starttype = state.type;
|
111 |
|
112 |
|
113 |
|
114 |
|
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();
|
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 |
|
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 |
|
204 | break;
|
205 | }
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
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 |
|
229 | parseIdentifierStatement(simpleName);
|
230 | }
|
231 | }
|
232 |
|
233 | export function parseDecorators() {
|
234 | while (match(tt.at)) {
|
235 | parseDecorator();
|
236 | }
|
237 | }
|
238 |
|
239 | function 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 |
|
253 | function parseMaybeDecoratorArguments() {
|
254 | if (isTypeScriptEnabled) {
|
255 | tsParseMaybeDecoratorArguments();
|
256 | } else {
|
257 | baseParseMaybeDecoratorArguments();
|
258 | }
|
259 | }
|
260 |
|
261 | export function baseParseMaybeDecoratorArguments() {
|
262 | if (eat(tt.parenL)) {
|
263 | parseCallExpressionArguments();
|
264 | }
|
265 | }
|
266 |
|
267 | function parseBreakContinueStatement() {
|
268 | next();
|
269 | if (!isLineTerminator()) {
|
270 | parseIdentifier();
|
271 | semicolon();
|
272 | }
|
273 | }
|
274 |
|
275 | function parseDebuggerStatement() {
|
276 | next();
|
277 | semicolon();
|
278 | }
|
279 |
|
280 | function parseDoStatement() {
|
281 | next();
|
282 | parseStatement(false);
|
283 | expect(tt._while);
|
284 | parseParenExpression();
|
285 | eat(tt.semi);
|
286 | }
|
287 |
|
288 | function 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 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 | function 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 |
|
345 | function parseFunctionStatement() {
|
346 | const functionStart = state.start;
|
347 | next();
|
348 | parseFunction(functionStart, true);
|
349 | }
|
350 |
|
351 | function parseIfStatement() {
|
352 | next();
|
353 | parseParenExpression();
|
354 | parseStatement(false);
|
355 | if (eat(tt._else)) {
|
356 | parseStatement(false);
|
357 | }
|
358 | }
|
359 |
|
360 | function parseReturnStatement() {
|
361 | next();
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | if (!isLineTerminator()) {
|
368 | parseExpression();
|
369 | semicolon();
|
370 | }
|
371 | }
|
372 |
|
373 | function parseSwitchStatement() {
|
374 | next();
|
375 | parseParenExpression();
|
376 | state.scopeDepth++;
|
377 | const startTokenIndex = state.tokens.length;
|
378 | expect(tt.braceL);
|
379 |
|
380 |
|
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();
|
394 | const endTokenIndex = state.tokens.length;
|
395 | state.scopes.push(new Scope(startTokenIndex, endTokenIndex, false));
|
396 | state.scopeDepth--;
|
397 | }
|
398 |
|
399 | function parseThrowStatement() {
|
400 | next();
|
401 | parseExpression();
|
402 | semicolon();
|
403 | }
|
404 |
|
405 | function 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 );
|
418 | expect(tt.parenR);
|
419 | }
|
420 | parseBlock();
|
421 | if (catchBindingStartTokenIndex != null) {
|
422 |
|
423 |
|
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 |
|
434 | export function parseVarStatement(kind) {
|
435 | next();
|
436 | parseVar(false, kind);
|
437 | semicolon();
|
438 | }
|
439 |
|
440 | function parseWhileStatement() {
|
441 | next();
|
442 | parseParenExpression();
|
443 | parseStatement(false);
|
444 | }
|
445 |
|
446 | function parseEmptyStatement() {
|
447 | next();
|
448 | }
|
449 |
|
450 | function parseLabeledStatement() {
|
451 | parseStatement(true);
|
452 | }
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 | function parseIdentifierStatement(contextualKeyword) {
|
459 | if (isTypeScriptEnabled) {
|
460 | tsParseIdentifierStatement(contextualKeyword);
|
461 | } else if (isFlowEnabled) {
|
462 | flowParseIdentifierStatement(contextualKeyword);
|
463 | } else {
|
464 | semicolon();
|
465 | }
|
466 | }
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 | export 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 |
|
492 | export function parseBlockBody(end) {
|
493 | while (!eat(end) && !state.error) {
|
494 | parseStatement(true);
|
495 | }
|
496 | }
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 | function 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 |
|
516 |
|
517 |
|
518 | function 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 |
|
530 |
|
531 | function 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 |
|
546 | function parseVarHead(isBlockScope) {
|
547 | parseBindingAtom(isBlockScope);
|
548 | if (isTypeScriptEnabled) {
|
549 | tsAfterParseVarHead();
|
550 | } else if (isFlowEnabled) {
|
551 | flowAfterParseVarHead();
|
552 | }
|
553 | }
|
554 |
|
555 |
|
556 |
|
557 |
|
558 | export 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 |
|
578 |
|
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 |
|
592 |
|
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 |
|
601 | export 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 , false , allowModifiers);
|
616 | if (funcContextId) {
|
617 | state.tokens[state.tokens.length - 1].contextId = funcContextId;
|
618 | }
|
619 | }
|
620 |
|
621 |
|
622 |
|
623 |
|
624 | export function parseClass(isStatement, optionalId = false) {
|
625 |
|
626 |
|
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 |
|
633 |
|
634 |
|
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 |
|
656 | function isClassProperty() {
|
657 | return match(tt.eq) || match(tt.semi) || match(tt.braceR) || match(tt.bang) || match(tt.colon);
|
658 | }
|
659 |
|
660 | function isClassMethod() {
|
661 | return match(tt.parenL) || match(tt.lessThan);
|
662 | }
|
663 |
|
664 | function 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 |
|
681 | function parseClassMember(memberStart, classContextId) {
|
682 | if (isTypeScriptEnabled) {
|
683 | tsParseAccessModifier();
|
684 | }
|
685 | let isStatic = false;
|
686 | if (match(tt.name) && state.contextualKeyword === ContextualKeyword._static) {
|
687 | parseIdentifier();
|
688 | if (isClassMethod()) {
|
689 | parseClassMethod(memberStart, false, false);
|
690 | return;
|
691 | } else if (isClassProperty()) {
|
692 | parseClassProperty();
|
693 | return;
|
694 | }
|
695 |
|
696 | state.tokens[state.tokens.length - 1].type = tt._static;
|
697 | isStatic = true;
|
698 | }
|
699 |
|
700 | parseClassMemberWithIsStatic(memberStart, isStatic, classContextId);
|
701 | }
|
702 |
|
703 | function 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 |
|
715 | parseClassPropertyName(classContextId);
|
716 | parseClassMethod(memberStart, true, false);
|
717 | return;
|
718 | }
|
719 |
|
720 |
|
721 |
|
722 | parseClassPropertyName(classContextId);
|
723 | let isConstructor = false;
|
724 | const token = state.tokens[state.tokens.length - 1];
|
725 |
|
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 |
|
738 | const isGenerator = match(tt.star);
|
739 | if (isGenerator) {
|
740 | next();
|
741 | }
|
742 |
|
743 |
|
744 | parseClassPropertyName(classContextId);
|
745 | parseClassMethod(memberStart, isGenerator, false );
|
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 |
|
757 |
|
758 |
|
759 | parseClassPropertyName(classContextId);
|
760 | parseClassMethod(memberStart, false, false);
|
761 | } else if (isLineTerminator()) {
|
762 |
|
763 | parseClassProperty();
|
764 | } else {
|
765 | unexpected();
|
766 | }
|
767 | }
|
768 |
|
769 | function 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 |
|
785 | export function parseClassPropertyName(classContextId) {
|
786 | parsePropertyName(classContextId);
|
787 | }
|
788 |
|
789 | export function parsePostMemberNameModifiers() {
|
790 | if (isTypeScriptEnabled) {
|
791 | const oldIsType = pushTypeContext(0);
|
792 | eat(tt.question);
|
793 | popTypeContext(oldIsType);
|
794 | }
|
795 | }
|
796 |
|
797 | export 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 |
|
816 | function 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 |
|
839 | function 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 |
|
855 |
|
856 | export function parseExport() {
|
857 | if (isTypeScriptEnabled) {
|
858 | if (tsTryParseExport()) {
|
859 | return;
|
860 | }
|
861 | }
|
862 |
|
863 | if (shouldParseExportStar()) {
|
864 | parseExportStar();
|
865 | } else if (isExportDefaultSpecifier()) {
|
866 |
|
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 |
|
879 | parseExportDefaultExpression();
|
880 | } else if (shouldParseExportDeclaration()) {
|
881 | parseExportDeclaration();
|
882 | } else {
|
883 |
|
884 | parseExportSpecifiers();
|
885 | parseExportFrom();
|
886 | }
|
887 | }
|
888 |
|
889 | function 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 |
|
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 |
|
914 | function parseExportDeclaration() {
|
915 | if (isTypeScriptEnabled) {
|
916 | tsParseExportDeclaration();
|
917 | } else if (isFlowEnabled) {
|
918 | flowParseExportDeclaration();
|
919 | } else {
|
920 | parseStatement(true);
|
921 | }
|
922 | }
|
923 |
|
924 | function 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 |
|
945 | function parseExportSpecifiersMaybe() {
|
946 | if (eat(tt.comma)) {
|
947 | parseExportSpecifiers();
|
948 | }
|
949 | }
|
950 |
|
951 | export function parseExportFrom() {
|
952 | if (eatContextual(ContextualKeyword._from)) {
|
953 | parseExprAtom();
|
954 | }
|
955 | semicolon();
|
956 | }
|
957 |
|
958 | function shouldParseExportStar() {
|
959 | if (isFlowEnabled) {
|
960 | return flowShouldParseExportStar();
|
961 | } else {
|
962 | return match(tt.star);
|
963 | }
|
964 | }
|
965 |
|
966 | function parseExportStar() {
|
967 | if (isFlowEnabled) {
|
968 | flowParseExportStar();
|
969 | } else {
|
970 | baseParseExportStar();
|
971 | }
|
972 | }
|
973 |
|
974 | export function baseParseExportStar() {
|
975 | expect(tt.star);
|
976 |
|
977 | if (isContextual(ContextualKeyword._as)) {
|
978 | parseExportNamespace();
|
979 | } else {
|
980 | parseExportFrom();
|
981 | }
|
982 | }
|
983 |
|
984 | function parseExportNamespace() {
|
985 | next();
|
986 | state.tokens[state.tokens.length - 1].type = tt._as;
|
987 | parseIdentifier();
|
988 | parseExportSpecifiersMaybe();
|
989 | parseExportFrom();
|
990 | }
|
991 |
|
992 | function 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 |
|
1007 | export function parseExportSpecifiers() {
|
1008 | let first = true;
|
1009 |
|
1010 |
|
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 |
|
1032 |
|
1033 | export function parseImport() {
|
1034 | if (isTypeScriptEnabled && match(tt.name) && lookaheadType() === tt.eq) {
|
1035 | tsParseImportEqualsDeclaration();
|
1036 | return;
|
1037 | }
|
1038 |
|
1039 |
|
1040 | if (match(tt.string)) {
|
1041 | parseExprAtom();
|
1042 | } else {
|
1043 | parseImportSpecifiers();
|
1044 | expectContextual(ContextualKeyword._from);
|
1045 | parseExprAtom();
|
1046 | }
|
1047 | semicolon();
|
1048 | }
|
1049 |
|
1050 |
|
1051 | function shouldParseDefaultImport() {
|
1052 | return match(tt.name);
|
1053 | }
|
1054 |
|
1055 | function parseImportSpecifierLocal() {
|
1056 | parseIdentifier();
|
1057 | }
|
1058 |
|
1059 |
|
1060 | function parseImportSpecifiers() {
|
1061 | if (isFlowEnabled) {
|
1062 | flowStartParseImportSpecifiers();
|
1063 | }
|
1064 |
|
1065 | let first = true;
|
1066 | if (shouldParseDefaultImport()) {
|
1067 |
|
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 |
|
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 |
|
1104 | function parseImportSpecifier() {
|
1105 | if (isFlowEnabled) {
|
1106 | flowParseImportSpecifier();
|
1107 | return;
|
1108 | }
|
1109 | parseIdentifier();
|
1110 | if (eatContextual(ContextualKeyword._as)) {
|
1111 | parseIdentifier();
|
1112 | }
|
1113 | }
|