1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | import {
|
22 | flowParseArrow,
|
23 | flowParseFunctionBodyAndFinish,
|
24 | flowParseMaybeAssign,
|
25 | flowParseSubscript,
|
26 | flowParseSubscripts,
|
27 | flowParseVariance,
|
28 | flowStartParseAsyncArrowFromCallExpression,
|
29 | flowStartParseNewArguments,
|
30 | flowStartParseObjPropValue,
|
31 | } from "../plugins/flow";
|
32 | import {jsxParseElement} from "../plugins/jsx/index";
|
33 | import {typedParseConditional, typedParseParenItem} from "../plugins/types";
|
34 | import {
|
35 | tsParseArrow,
|
36 | tsParseFunctionBodyAndFinish,
|
37 | tsParseMaybeAssign,
|
38 | tsParseSubscript,
|
39 | tsParseType,
|
40 | tsParseTypeAssertion,
|
41 | tsStartParseAsyncArrowFromCallExpression,
|
42 | tsStartParseNewArguments,
|
43 | tsStartParseObjPropValue,
|
44 | } from "../plugins/typescript";
|
45 | import {
|
46 | eat,
|
47 | IdentifierRole,
|
48 | lookaheadType,
|
49 | match,
|
50 | next,
|
51 | nextTemplateToken,
|
52 | popTypeContext,
|
53 | pushTypeContext,
|
54 | retokenizeSlashAsRegex,
|
55 | } from "../tokenizer/index";
|
56 | import {ContextualKeyword} from "../tokenizer/keywords";
|
57 | import {Scope} from "../tokenizer/state";
|
58 | import {TokenType, TokenType as tt} from "../tokenizer/types";
|
59 | import {getNextContextId, isFlowEnabled, isJSXEnabled, isTypeScriptEnabled, state} from "./base";
|
60 | import {
|
61 | markPriorBindingIdentifier,
|
62 | parseBindingIdentifier,
|
63 | parseMaybeDefault,
|
64 | parseRest,
|
65 | parseSpread,
|
66 | } from "./lval";
|
67 | import {
|
68 | parseBlock,
|
69 | parseClass,
|
70 | parseDecorators,
|
71 | parseFunction,
|
72 | parseFunctionParams,
|
73 | } from "./statement";
|
74 | import {
|
75 | canInsertSemicolon,
|
76 | eatContextual,
|
77 | expect,
|
78 | hasPrecedingLineBreak,
|
79 | isContextual,
|
80 | unexpected,
|
81 | } from "./util";
|
82 |
|
83 | export class StopState {
|
84 |
|
85 | constructor(stop) {
|
86 | this.stop = stop;
|
87 | }
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | export function parseExpression(noIn = false) {
|
98 | parseMaybeAssign(noIn);
|
99 | if (match(tt.comma)) {
|
100 | while (eat(tt.comma)) {
|
101 | parseMaybeAssign(noIn);
|
102 | }
|
103 | }
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | export function parseMaybeAssign(noIn = false, isWithinParens = false) {
|
114 | if (isTypeScriptEnabled) {
|
115 | return tsParseMaybeAssign(noIn, isWithinParens);
|
116 | } else if (isFlowEnabled) {
|
117 | return flowParseMaybeAssign(noIn, isWithinParens);
|
118 | } else {
|
119 | return baseParseMaybeAssign(noIn, isWithinParens);
|
120 | }
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | export function baseParseMaybeAssign(noIn, isWithinParens) {
|
127 | if (match(tt._yield)) {
|
128 | parseYield();
|
129 | return false;
|
130 | }
|
131 |
|
132 | if (match(tt.parenL) || match(tt.name) || match(tt._yield)) {
|
133 | state.potentialArrowAt = state.start;
|
134 | }
|
135 |
|
136 | const wasArrow = parseMaybeConditional(noIn);
|
137 | if (isWithinParens) {
|
138 | parseParenItem();
|
139 | }
|
140 | if (state.type & TokenType.IS_ASSIGN) {
|
141 | next();
|
142 | parseMaybeAssign(noIn);
|
143 | return false;
|
144 | }
|
145 | return wasArrow;
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 | function parseMaybeConditional(noIn) {
|
151 | const wasArrow = parseExprOps(noIn);
|
152 | if (wasArrow) {
|
153 | return true;
|
154 | }
|
155 | parseConditional(noIn);
|
156 | return false;
|
157 | }
|
158 |
|
159 | function parseConditional(noIn) {
|
160 | if (isTypeScriptEnabled || isFlowEnabled) {
|
161 | typedParseConditional(noIn);
|
162 | } else {
|
163 | baseParseConditional(noIn);
|
164 | }
|
165 | }
|
166 |
|
167 | export function baseParseConditional(noIn) {
|
168 | if (eat(tt.question)) {
|
169 | parseMaybeAssign();
|
170 | expect(tt.colon);
|
171 | parseMaybeAssign(noIn);
|
172 | }
|
173 | }
|
174 |
|
175 |
|
176 |
|
177 | function parseExprOps(noIn) {
|
178 | const wasArrow = parseMaybeUnary();
|
179 | if (wasArrow) {
|
180 | return true;
|
181 | }
|
182 | parseExprOp(-1, noIn);
|
183 | return false;
|
184 | }
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | function parseExprOp(minPrec, noIn) {
|
192 | if (
|
193 | isTypeScriptEnabled &&
|
194 | (tt._in & TokenType.PRECEDENCE_MASK) > minPrec &&
|
195 | !hasPrecedingLineBreak() &&
|
196 | eatContextual(ContextualKeyword._as)
|
197 | ) {
|
198 | state.tokens[state.tokens.length - 1].type = tt._as;
|
199 | const oldIsType = pushTypeContext(1);
|
200 | tsParseType();
|
201 | popTypeContext(oldIsType);
|
202 | parseExprOp(minPrec, noIn);
|
203 | return;
|
204 | }
|
205 |
|
206 | const prec = state.type & TokenType.PRECEDENCE_MASK;
|
207 | if (prec > 0 && (!noIn || !match(tt._in))) {
|
208 | if (prec > minPrec) {
|
209 | const op = state.type;
|
210 | next();
|
211 |
|
212 | parseMaybeUnary();
|
213 | parseExprOp(op & TokenType.IS_RIGHT_ASSOCIATIVE ? prec - 1 : prec, noIn);
|
214 | parseExprOp(minPrec, noIn);
|
215 | }
|
216 | }
|
217 | }
|
218 |
|
219 |
|
220 |
|
221 | export function parseMaybeUnary() {
|
222 | if (isTypeScriptEnabled && !isJSXEnabled && eat(tt.lessThan)) {
|
223 | tsParseTypeAssertion();
|
224 | return false;
|
225 | }
|
226 |
|
227 | if (state.type & TokenType.IS_PREFIX) {
|
228 | next();
|
229 | parseMaybeUnary();
|
230 | return false;
|
231 | }
|
232 |
|
233 | const wasArrow = parseExprSubscripts();
|
234 | if (wasArrow) {
|
235 | return true;
|
236 | }
|
237 | while (state.type & TokenType.IS_POSTFIX && !canInsertSemicolon()) {
|
238 |
|
239 |
|
240 | if (state.type === tt.preIncDec) {
|
241 | state.type = tt.postIncDec;
|
242 | }
|
243 | next();
|
244 | }
|
245 | return false;
|
246 | }
|
247 |
|
248 |
|
249 |
|
250 | export function parseExprSubscripts() {
|
251 | const startPos = state.start;
|
252 | const wasArrow = parseExprAtom();
|
253 | if (wasArrow) {
|
254 | return true;
|
255 | }
|
256 | parseSubscripts(startPos);
|
257 | return false;
|
258 | }
|
259 |
|
260 | function parseSubscripts(startPos, noCalls = false) {
|
261 | if (isFlowEnabled) {
|
262 | flowParseSubscripts(startPos, noCalls);
|
263 | } else {
|
264 | baseParseSubscripts(startPos, noCalls);
|
265 | }
|
266 | }
|
267 |
|
268 | export function baseParseSubscripts(startPos, noCalls = false) {
|
269 | const stopState = new StopState(false);
|
270 | do {
|
271 | parseSubscript(startPos, noCalls, stopState);
|
272 | } while (!stopState.stop && !state.error);
|
273 | }
|
274 |
|
275 | function parseSubscript(startPos, noCalls, stopState) {
|
276 | if (isTypeScriptEnabled) {
|
277 | tsParseSubscript(startPos, noCalls, stopState);
|
278 | } else if (isFlowEnabled) {
|
279 | flowParseSubscript(startPos, noCalls, stopState);
|
280 | } else {
|
281 | baseParseSubscript(startPos, noCalls, stopState);
|
282 | }
|
283 | }
|
284 |
|
285 |
|
286 | export function baseParseSubscript(startPos, noCalls, stopState) {
|
287 | if (!noCalls && eat(tt.doubleColon)) {
|
288 | parseNoCallExpr();
|
289 | stopState.stop = true;
|
290 | parseSubscripts(startPos, noCalls);
|
291 | } else if (match(tt.questionDot)) {
|
292 | if (noCalls && lookaheadType() === tt.parenL) {
|
293 | stopState.stop = true;
|
294 | return;
|
295 | }
|
296 | next();
|
297 |
|
298 | if (eat(tt.bracketL)) {
|
299 | parseExpression();
|
300 | expect(tt.bracketR);
|
301 | } else if (eat(tt.parenL)) {
|
302 | parseCallExpressionArguments();
|
303 | } else {
|
304 | parseIdentifier();
|
305 | }
|
306 | } else if (eat(tt.dot)) {
|
307 | parseMaybePrivateName();
|
308 | } else if (eat(tt.bracketL)) {
|
309 | parseExpression();
|
310 | expect(tt.bracketR);
|
311 | } else if (!noCalls && match(tt.parenL)) {
|
312 | if (atPossibleAsync()) {
|
313 |
|
314 |
|
315 | const snapshot = state.snapshot();
|
316 | const startTokenIndex = state.tokens.length;
|
317 | next();
|
318 |
|
319 | const callContextId = getNextContextId();
|
320 |
|
321 | state.tokens[state.tokens.length - 1].contextId = callContextId;
|
322 | parseCallExpressionArguments();
|
323 | state.tokens[state.tokens.length - 1].contextId = callContextId;
|
324 |
|
325 | if (shouldParseAsyncArrow()) {
|
326 |
|
327 | state.restoreFromSnapshot(snapshot);
|
328 | stopState.stop = true;
|
329 | state.scopeDepth++;
|
330 |
|
331 | parseFunctionParams();
|
332 | parseAsyncArrowFromCallExpression(startPos, startTokenIndex);
|
333 | }
|
334 | } else {
|
335 | next();
|
336 | const callContextId = getNextContextId();
|
337 | state.tokens[state.tokens.length - 1].contextId = callContextId;
|
338 | parseCallExpressionArguments();
|
339 | state.tokens[state.tokens.length - 1].contextId = callContextId;
|
340 | }
|
341 | } else if (match(tt.backQuote)) {
|
342 |
|
343 | parseTemplate();
|
344 | } else {
|
345 | stopState.stop = true;
|
346 | }
|
347 | }
|
348 |
|
349 | export function atPossibleAsync() {
|
350 |
|
351 |
|
352 | return (
|
353 | state.tokens[state.tokens.length - 1].contextualKeyword === ContextualKeyword._async &&
|
354 | !canInsertSemicolon()
|
355 | );
|
356 | }
|
357 |
|
358 | export function parseCallExpressionArguments() {
|
359 | let first = true;
|
360 | while (!eat(tt.parenR) && !state.error) {
|
361 | if (first) {
|
362 | first = false;
|
363 | } else {
|
364 | expect(tt.comma);
|
365 | if (eat(tt.parenR)) {
|
366 | break;
|
367 | }
|
368 | }
|
369 |
|
370 | parseExprListItem(false);
|
371 | }
|
372 | }
|
373 |
|
374 | function shouldParseAsyncArrow() {
|
375 | return match(tt.colon) || match(tt.arrow);
|
376 | }
|
377 |
|
378 | function parseAsyncArrowFromCallExpression(functionStart, startTokenIndex) {
|
379 | if (isTypeScriptEnabled) {
|
380 | tsStartParseAsyncArrowFromCallExpression();
|
381 | } else if (isFlowEnabled) {
|
382 | flowStartParseAsyncArrowFromCallExpression();
|
383 | }
|
384 | expect(tt.arrow);
|
385 | parseArrowExpression(functionStart, startTokenIndex);
|
386 | }
|
387 |
|
388 |
|
389 |
|
390 | function parseNoCallExpr() {
|
391 | const startPos = state.start;
|
392 | parseExprAtom();
|
393 | parseSubscripts(startPos, true);
|
394 | }
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 | export function parseExprAtom() {
|
402 | if (match(tt.jsxText)) {
|
403 | parseLiteral();
|
404 | return false;
|
405 | } else if (match(tt.lessThan) && isJSXEnabled) {
|
406 | state.type = tt.jsxTagStart;
|
407 | jsxParseElement();
|
408 | next();
|
409 | return false;
|
410 | }
|
411 |
|
412 | const canBeArrow = state.potentialArrowAt === state.start;
|
413 | switch (state.type) {
|
414 | case tt.slash:
|
415 | case tt.assign:
|
416 | retokenizeSlashAsRegex();
|
417 |
|
418 |
|
419 | case tt._super:
|
420 | case tt._this:
|
421 | case tt.regexp:
|
422 | case tt.num:
|
423 | case tt.bigint:
|
424 | case tt.string:
|
425 | case tt._null:
|
426 | case tt._true:
|
427 | case tt._false:
|
428 | next();
|
429 | return false;
|
430 |
|
431 | case tt._import:
|
432 | if (lookaheadType() === tt.dot) {
|
433 | parseImportMetaProperty();
|
434 | return false;
|
435 | }
|
436 | next();
|
437 | return false;
|
438 |
|
439 | case tt.name: {
|
440 | const startTokenIndex = state.tokens.length;
|
441 | const functionStart = state.start;
|
442 | const contextualKeyword = state.contextualKeyword;
|
443 | parseIdentifier();
|
444 | if (contextualKeyword === ContextualKeyword._await) {
|
445 | parseAwait();
|
446 | return false;
|
447 | } else if (
|
448 | contextualKeyword === ContextualKeyword._async &&
|
449 | match(tt._function) &&
|
450 | !canInsertSemicolon()
|
451 | ) {
|
452 | next();
|
453 | parseFunction(functionStart, false, false);
|
454 | return false;
|
455 | } else if (
|
456 | canBeArrow &&
|
457 | !canInsertSemicolon() &&
|
458 | contextualKeyword === ContextualKeyword._async &&
|
459 | match(tt.name)
|
460 | ) {
|
461 | state.scopeDepth++;
|
462 | parseBindingIdentifier(false);
|
463 | expect(tt.arrow);
|
464 |
|
465 | parseArrowExpression(functionStart, startTokenIndex);
|
466 | return true;
|
467 | }
|
468 |
|
469 | if (canBeArrow && !canInsertSemicolon() && match(tt.arrow)) {
|
470 | state.scopeDepth++;
|
471 | markPriorBindingIdentifier(false);
|
472 | expect(tt.arrow);
|
473 | parseArrowExpression(functionStart, startTokenIndex);
|
474 | return true;
|
475 | }
|
476 |
|
477 | state.tokens[state.tokens.length - 1].identifierRole = IdentifierRole.Access;
|
478 | return false;
|
479 | }
|
480 |
|
481 | case tt._do: {
|
482 | next();
|
483 | parseBlock(false);
|
484 | return false;
|
485 | }
|
486 |
|
487 | case tt.parenL: {
|
488 | const wasArrow = parseParenAndDistinguishExpression(canBeArrow);
|
489 | return wasArrow;
|
490 | }
|
491 |
|
492 | case tt.bracketL:
|
493 | next();
|
494 | parseExprList(tt.bracketR, true);
|
495 | return false;
|
496 |
|
497 | case tt.braceL:
|
498 | parseObj(false, false);
|
499 | return false;
|
500 |
|
501 | case tt._function:
|
502 | parseFunctionExpression();
|
503 | return false;
|
504 |
|
505 | case tt.at:
|
506 | parseDecorators();
|
507 |
|
508 |
|
509 | case tt._class:
|
510 | parseClass(false);
|
511 | return false;
|
512 |
|
513 | case tt._new:
|
514 | parseNew();
|
515 | return false;
|
516 |
|
517 | case tt.backQuote:
|
518 | parseTemplate();
|
519 | return false;
|
520 |
|
521 | case tt.doubleColon: {
|
522 | next();
|
523 | parseNoCallExpr();
|
524 | return false;
|
525 | }
|
526 |
|
527 | default:
|
528 | unexpected();
|
529 | return false;
|
530 | }
|
531 | }
|
532 |
|
533 | function parseMaybePrivateName() {
|
534 | eat(tt.hash);
|
535 | parseIdentifier();
|
536 | }
|
537 |
|
538 | function parseFunctionExpression() {
|
539 | const functionStart = state.start;
|
540 | parseIdentifier();
|
541 | if (eat(tt.dot)) {
|
542 |
|
543 | parseMetaProperty();
|
544 | }
|
545 | parseFunction(functionStart, false);
|
546 | }
|
547 |
|
548 | function parseMetaProperty() {
|
549 | parseIdentifier();
|
550 | }
|
551 |
|
552 | function parseImportMetaProperty() {
|
553 | parseIdentifier();
|
554 | expect(tt.dot);
|
555 |
|
556 | parseMetaProperty();
|
557 | }
|
558 |
|
559 | export function parseLiteral() {
|
560 | next();
|
561 | }
|
562 |
|
563 | export function parseParenExpression() {
|
564 | expect(tt.parenL);
|
565 | parseExpression();
|
566 | expect(tt.parenR);
|
567 | }
|
568 |
|
569 |
|
570 | function parseParenAndDistinguishExpression(canBeArrow) {
|
571 |
|
572 |
|
573 | const snapshot = state.snapshot();
|
574 |
|
575 | const startTokenIndex = state.tokens.length;
|
576 | expect(tt.parenL);
|
577 |
|
578 | let first = true;
|
579 |
|
580 | while (!match(tt.parenR) && !state.error) {
|
581 | if (first) {
|
582 | first = false;
|
583 | } else {
|
584 | expect(tt.comma);
|
585 | if (match(tt.parenR)) {
|
586 | break;
|
587 | }
|
588 | }
|
589 |
|
590 | if (match(tt.ellipsis)) {
|
591 | parseRest(false );
|
592 | parseParenItem();
|
593 | break;
|
594 | } else {
|
595 | parseMaybeAssign(false, true);
|
596 | }
|
597 | }
|
598 |
|
599 | expect(tt.parenR);
|
600 |
|
601 | if (canBeArrow && shouldParseArrow()) {
|
602 | const wasArrow = parseArrow();
|
603 | if (wasArrow) {
|
604 |
|
605 |
|
606 | state.restoreFromSnapshot(snapshot);
|
607 | state.scopeDepth++;
|
608 |
|
609 | const functionStart = state.start;
|
610 |
|
611 | parseFunctionParams();
|
612 | parseArrow();
|
613 | parseArrowExpression(functionStart, startTokenIndex);
|
614 | return true;
|
615 | }
|
616 | }
|
617 |
|
618 | return false;
|
619 | }
|
620 |
|
621 | function shouldParseArrow() {
|
622 | return match(tt.colon) || !canInsertSemicolon();
|
623 | }
|
624 |
|
625 |
|
626 | export function parseArrow() {
|
627 | if (isTypeScriptEnabled) {
|
628 | return tsParseArrow();
|
629 | } else if (isFlowEnabled) {
|
630 | return flowParseArrow();
|
631 | } else {
|
632 | return eat(tt.arrow);
|
633 | }
|
634 | }
|
635 |
|
636 | function parseParenItem() {
|
637 | if (isTypeScriptEnabled || isFlowEnabled) {
|
638 | typedParseParenItem();
|
639 | }
|
640 | }
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 |
|
647 | function parseNew() {
|
648 | expect(tt._new);
|
649 | if (eat(tt.dot)) {
|
650 |
|
651 | parseMetaProperty();
|
652 | return;
|
653 | }
|
654 | parseNoCallExpr();
|
655 | eat(tt.questionDot);
|
656 | parseNewArguments();
|
657 | }
|
658 |
|
659 | function parseNewArguments() {
|
660 | if (isTypeScriptEnabled) {
|
661 | tsStartParseNewArguments();
|
662 | } else if (isFlowEnabled) {
|
663 | flowStartParseNewArguments();
|
664 | }
|
665 | if (eat(tt.parenL)) {
|
666 | parseExprList(tt.parenR);
|
667 | }
|
668 | }
|
669 |
|
670 | export function parseTemplate() {
|
671 |
|
672 | nextTemplateToken();
|
673 |
|
674 | nextTemplateToken();
|
675 | while (!match(tt.backQuote) && !state.error) {
|
676 | expect(tt.dollarBraceL);
|
677 | parseExpression();
|
678 |
|
679 | nextTemplateToken();
|
680 |
|
681 | nextTemplateToken();
|
682 | }
|
683 | next();
|
684 | }
|
685 |
|
686 |
|
687 | export function parseObj(isPattern, isBlockScope) {
|
688 |
|
689 | const contextId = getNextContextId();
|
690 | let first = true;
|
691 |
|
692 | next();
|
693 | state.tokens[state.tokens.length - 1].contextId = contextId;
|
694 |
|
695 | while (!eat(tt.braceR) && !state.error) {
|
696 | if (first) {
|
697 | first = false;
|
698 | } else {
|
699 | expect(tt.comma);
|
700 | if (eat(tt.braceR)) {
|
701 | break;
|
702 | }
|
703 | }
|
704 |
|
705 | let isGenerator = false;
|
706 | if (match(tt.ellipsis)) {
|
707 | const previousIndex = state.tokens.length;
|
708 | parseSpread();
|
709 | if (isPattern) {
|
710 |
|
711 | if (state.tokens.length === previousIndex + 2) {
|
712 | markPriorBindingIdentifier(isBlockScope);
|
713 | }
|
714 | if (eat(tt.braceR)) {
|
715 | break;
|
716 | }
|
717 | }
|
718 | continue;
|
719 | }
|
720 |
|
721 | if (!isPattern) {
|
722 | isGenerator = eat(tt.star);
|
723 | }
|
724 |
|
725 | if (!isPattern && isContextual(ContextualKeyword._async)) {
|
726 | if (isGenerator) unexpected();
|
727 |
|
728 | parseIdentifier();
|
729 | if (
|
730 | match(tt.colon) ||
|
731 | match(tt.parenL) ||
|
732 | match(tt.braceR) ||
|
733 | match(tt.eq) ||
|
734 | match(tt.comma)
|
735 | ) {
|
736 |
|
737 | } else {
|
738 | if (match(tt.star)) {
|
739 | next();
|
740 | isGenerator = true;
|
741 | }
|
742 | parsePropertyName(contextId);
|
743 | }
|
744 | } else {
|
745 | parsePropertyName(contextId);
|
746 | }
|
747 |
|
748 | parseObjPropValue(isGenerator, isPattern, isBlockScope, contextId);
|
749 | }
|
750 |
|
751 | state.tokens[state.tokens.length - 1].contextId = contextId;
|
752 | }
|
753 |
|
754 | function isGetterOrSetterMethod(isPattern) {
|
755 |
|
756 |
|
757 | return (
|
758 | !isPattern &&
|
759 | (match(tt.string) ||
|
760 | match(tt.num) ||
|
761 | match(tt.bracketL) ||
|
762 | match(tt.name) ||
|
763 | !!(state.type & TokenType.IS_KEYWORD))
|
764 | );
|
765 | }
|
766 |
|
767 |
|
768 | function parseObjectMethod(
|
769 | isGenerator,
|
770 | isPattern,
|
771 | objectContextId,
|
772 | ) {
|
773 |
|
774 |
|
775 | const functionStart = state.start;
|
776 | if (match(tt.parenL)) {
|
777 | if (isPattern) unexpected();
|
778 | parseMethod(functionStart, isGenerator, false);
|
779 | return true;
|
780 | }
|
781 |
|
782 | if (isGetterOrSetterMethod(isPattern)) {
|
783 | parsePropertyName(objectContextId);
|
784 | parseMethod(functionStart, false, false);
|
785 | return true;
|
786 | }
|
787 | return false;
|
788 | }
|
789 |
|
790 | function parseObjectProperty(isPattern, isBlockScope) {
|
791 | if (eat(tt.colon)) {
|
792 | if (isPattern) {
|
793 | parseMaybeDefault(isBlockScope);
|
794 | } else {
|
795 | parseMaybeAssign(false);
|
796 | }
|
797 | return;
|
798 | }
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 | if (isPattern) {
|
806 | state.tokens[state.tokens.length - 1].identifierRole = isBlockScope
|
807 | ? IdentifierRole.ObjectShorthandBlockScopedDeclaration
|
808 | : IdentifierRole.ObjectShorthandFunctionScopedDeclaration;
|
809 | } else {
|
810 | state.tokens[state.tokens.length - 1].identifierRole = IdentifierRole.ObjectShorthand;
|
811 | }
|
812 |
|
813 |
|
814 |
|
815 | parseMaybeDefault(isBlockScope, true);
|
816 | }
|
817 |
|
818 | function parseObjPropValue(
|
819 | isGenerator,
|
820 | isPattern,
|
821 | isBlockScope,
|
822 | objectContextId,
|
823 | ) {
|
824 | if (isTypeScriptEnabled) {
|
825 | tsStartParseObjPropValue();
|
826 | } else if (isFlowEnabled) {
|
827 | flowStartParseObjPropValue();
|
828 | }
|
829 | const wasMethod = parseObjectMethod(isGenerator, isPattern, objectContextId);
|
830 | if (!wasMethod) {
|
831 | parseObjectProperty(isPattern, isBlockScope);
|
832 | }
|
833 | }
|
834 |
|
835 | export function parsePropertyName(objectContextId) {
|
836 | if (isFlowEnabled) {
|
837 | flowParseVariance();
|
838 | }
|
839 | if (eat(tt.bracketL)) {
|
840 | state.tokens[state.tokens.length - 1].contextId = objectContextId;
|
841 | parseMaybeAssign();
|
842 | expect(tt.bracketR);
|
843 | state.tokens[state.tokens.length - 1].contextId = objectContextId;
|
844 | } else {
|
845 | if (match(tt.num) || match(tt.string)) {
|
846 | parseExprAtom();
|
847 | } else {
|
848 | parseMaybePrivateName();
|
849 | }
|
850 |
|
851 | state.tokens[state.tokens.length - 1].identifierRole = IdentifierRole.ObjectKey;
|
852 | state.tokens[state.tokens.length - 1].contextId = objectContextId;
|
853 | }
|
854 | }
|
855 |
|
856 |
|
857 | export function parseMethod(
|
858 | functionStart,
|
859 | isGenerator,
|
860 | isConstructor,
|
861 | ) {
|
862 | const funcContextId = getNextContextId();
|
863 |
|
864 | state.scopeDepth++;
|
865 | const startTokenIndex = state.tokens.length;
|
866 | const allowModifiers = isConstructor;
|
867 | parseFunctionParams(allowModifiers, funcContextId);
|
868 | parseFunctionBodyAndFinish(
|
869 | functionStart,
|
870 | isGenerator,
|
871 | false ,
|
872 | funcContextId,
|
873 | );
|
874 | const endTokenIndex = state.tokens.length;
|
875 | state.scopes.push(new Scope(startTokenIndex, endTokenIndex, true));
|
876 | state.scopeDepth--;
|
877 | }
|
878 |
|
879 |
|
880 |
|
881 |
|
882 | export function parseArrowExpression(functionStart, startTokenIndex) {
|
883 | parseFunctionBody(functionStart, false , true);
|
884 | const endTokenIndex = state.tokens.length;
|
885 | state.scopes.push(new Scope(startTokenIndex, endTokenIndex, true));
|
886 | state.scopeDepth--;
|
887 | }
|
888 |
|
889 | export function parseFunctionBodyAndFinish(
|
890 | functionStart,
|
891 | isGenerator,
|
892 | allowExpressionBody = false,
|
893 | funcContextId = 0,
|
894 | ) {
|
895 | if (isTypeScriptEnabled) {
|
896 | tsParseFunctionBodyAndFinish(functionStart, isGenerator, allowExpressionBody, funcContextId);
|
897 | } else if (isFlowEnabled) {
|
898 | flowParseFunctionBodyAndFinish(functionStart, isGenerator, allowExpressionBody, funcContextId);
|
899 | } else {
|
900 | parseFunctionBody(functionStart, isGenerator, allowExpressionBody, funcContextId);
|
901 | }
|
902 | }
|
903 |
|
904 |
|
905 | export function parseFunctionBody(
|
906 | functionStart,
|
907 | isGenerator,
|
908 | allowExpression,
|
909 | funcContextId = 0,
|
910 | ) {
|
911 | const isExpression = allowExpression && !match(tt.braceL);
|
912 |
|
913 | if (isExpression) {
|
914 | parseMaybeAssign();
|
915 | } else {
|
916 | parseBlock(true , true , funcContextId);
|
917 | }
|
918 | }
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 | function parseExprList(close, allowEmpty = false) {
|
927 | let first = true;
|
928 | while (!eat(close) && !state.error) {
|
929 | if (first) {
|
930 | first = false;
|
931 | } else {
|
932 | expect(tt.comma);
|
933 | if (eat(close)) break;
|
934 | }
|
935 | parseExprListItem(allowEmpty);
|
936 | }
|
937 | }
|
938 |
|
939 | function parseExprListItem(allowEmpty) {
|
940 | if (allowEmpty && match(tt.comma)) {
|
941 |
|
942 | } else if (match(tt.ellipsis)) {
|
943 | parseSpread();
|
944 | parseParenItem();
|
945 | } else {
|
946 | parseMaybeAssign(false, true);
|
947 | }
|
948 | }
|
949 |
|
950 |
|
951 | export function parseIdentifier() {
|
952 | next();
|
953 | state.tokens[state.tokens.length - 1].type = tt.name;
|
954 | }
|
955 |
|
956 |
|
957 | function parseAwait() {
|
958 | parseMaybeUnary();
|
959 | }
|
960 |
|
961 |
|
962 | function parseYield() {
|
963 | next();
|
964 | if (!match(tt.semi) && !canInsertSemicolon()) {
|
965 | eat(tt.star);
|
966 | parseMaybeAssign();
|
967 | }
|
968 | }
|