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 | next();
|
239 | }
|
240 | return false;
|
241 | }
|
242 |
|
243 |
|
244 |
|
245 | export function parseExprSubscripts() {
|
246 | const startPos = state.start;
|
247 | const wasArrow = parseExprAtom();
|
248 | if (wasArrow) {
|
249 | return true;
|
250 | }
|
251 | parseSubscripts(startPos);
|
252 | return false;
|
253 | }
|
254 |
|
255 | function parseSubscripts(startPos, noCalls = false) {
|
256 | if (isFlowEnabled) {
|
257 | flowParseSubscripts(startPos, noCalls);
|
258 | } else {
|
259 | baseParseSubscripts(startPos, noCalls);
|
260 | }
|
261 | }
|
262 |
|
263 | export function baseParseSubscripts(startPos, noCalls = false) {
|
264 | const stopState = new StopState(false);
|
265 | do {
|
266 | parseSubscript(startPos, noCalls, stopState);
|
267 | } while (!stopState.stop && !state.error);
|
268 | }
|
269 |
|
270 | function parseSubscript(startPos, noCalls, stopState) {
|
271 | if (isTypeScriptEnabled) {
|
272 | tsParseSubscript(startPos, noCalls, stopState);
|
273 | } else if (isFlowEnabled) {
|
274 | flowParseSubscript(startPos, noCalls, stopState);
|
275 | } else {
|
276 | baseParseSubscript(startPos, noCalls, stopState);
|
277 | }
|
278 | }
|
279 |
|
280 |
|
281 | export function baseParseSubscript(startPos, noCalls, stopState) {
|
282 | if (!noCalls && eat(tt.doubleColon)) {
|
283 | parseNoCallExpr();
|
284 | stopState.stop = true;
|
285 | parseSubscripts(startPos, noCalls);
|
286 | } else if (match(tt.questionDot)) {
|
287 | if (noCalls && lookaheadType() === tt.parenL) {
|
288 | stopState.stop = true;
|
289 | return;
|
290 | }
|
291 | next();
|
292 |
|
293 | if (eat(tt.bracketL)) {
|
294 | parseExpression();
|
295 | expect(tt.bracketR);
|
296 | } else if (eat(tt.parenL)) {
|
297 | parseCallExpressionArguments();
|
298 | } else {
|
299 | parseIdentifier();
|
300 | }
|
301 | } else if (eat(tt.dot)) {
|
302 | parseMaybePrivateName();
|
303 | } else if (eat(tt.bracketL)) {
|
304 | parseExpression();
|
305 | expect(tt.bracketR);
|
306 | } else if (!noCalls && match(tt.parenL)) {
|
307 | if (atPossibleAsync()) {
|
308 |
|
309 |
|
310 | const snapshot = state.snapshot();
|
311 | const startTokenIndex = state.tokens.length;
|
312 | next();
|
313 |
|
314 | const callContextId = getNextContextId();
|
315 |
|
316 | state.tokens[state.tokens.length - 1].contextId = callContextId;
|
317 | parseCallExpressionArguments();
|
318 | state.tokens[state.tokens.length - 1].contextId = callContextId;
|
319 |
|
320 | if (shouldParseAsyncArrow()) {
|
321 |
|
322 | state.restoreFromSnapshot(snapshot);
|
323 | stopState.stop = true;
|
324 | state.scopeDepth++;
|
325 |
|
326 | parseFunctionParams();
|
327 | parseAsyncArrowFromCallExpression(startPos, startTokenIndex);
|
328 | }
|
329 | } else {
|
330 | next();
|
331 | const callContextId = getNextContextId();
|
332 | state.tokens[state.tokens.length - 1].contextId = callContextId;
|
333 | parseCallExpressionArguments();
|
334 | state.tokens[state.tokens.length - 1].contextId = callContextId;
|
335 | }
|
336 | } else if (match(tt.backQuote)) {
|
337 |
|
338 | parseTemplate();
|
339 | } else {
|
340 | stopState.stop = true;
|
341 | }
|
342 | }
|
343 |
|
344 | export function atPossibleAsync() {
|
345 |
|
346 |
|
347 | return (
|
348 | state.tokens[state.tokens.length - 1].contextualKeyword === ContextualKeyword._async &&
|
349 | !canInsertSemicolon()
|
350 | );
|
351 | }
|
352 |
|
353 | export function parseCallExpressionArguments() {
|
354 | let first = true;
|
355 | while (!eat(tt.parenR) && !state.error) {
|
356 | if (first) {
|
357 | first = false;
|
358 | } else {
|
359 | expect(tt.comma);
|
360 | if (eat(tt.parenR)) {
|
361 | break;
|
362 | }
|
363 | }
|
364 |
|
365 | parseExprListItem(false);
|
366 | }
|
367 | }
|
368 |
|
369 | function shouldParseAsyncArrow() {
|
370 | return match(tt.colon) || match(tt.arrow);
|
371 | }
|
372 |
|
373 | function parseAsyncArrowFromCallExpression(functionStart, startTokenIndex) {
|
374 | if (isTypeScriptEnabled) {
|
375 | tsStartParseAsyncArrowFromCallExpression();
|
376 | } else if (isFlowEnabled) {
|
377 | flowStartParseAsyncArrowFromCallExpression();
|
378 | }
|
379 | expect(tt.arrow);
|
380 | parseArrowExpression(functionStart, startTokenIndex);
|
381 | }
|
382 |
|
383 |
|
384 |
|
385 | function parseNoCallExpr() {
|
386 | const startPos = state.start;
|
387 | parseExprAtom();
|
388 | parseSubscripts(startPos, true);
|
389 | }
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | export function parseExprAtom() {
|
397 | if (match(tt.jsxText)) {
|
398 | parseLiteral();
|
399 | return false;
|
400 | } else if (match(tt.lessThan) && isJSXEnabled) {
|
401 | state.type = tt.jsxTagStart;
|
402 | jsxParseElement();
|
403 | next();
|
404 | return false;
|
405 | }
|
406 |
|
407 | const canBeArrow = state.potentialArrowAt === state.start;
|
408 | switch (state.type) {
|
409 | case tt.slash:
|
410 | case tt.assign:
|
411 | retokenizeSlashAsRegex();
|
412 |
|
413 |
|
414 | case tt._super:
|
415 | case tt._this:
|
416 | case tt.regexp:
|
417 | case tt.num:
|
418 | case tt.bigint:
|
419 | case tt.string:
|
420 | case tt._null:
|
421 | case tt._true:
|
422 | case tt._false:
|
423 | next();
|
424 | return false;
|
425 |
|
426 | case tt._import:
|
427 | if (lookaheadType() === tt.dot) {
|
428 | parseImportMetaProperty();
|
429 | return false;
|
430 | }
|
431 | next();
|
432 | return false;
|
433 |
|
434 | case tt.name: {
|
435 | const startTokenIndex = state.tokens.length;
|
436 | const functionStart = state.start;
|
437 | const contextualKeyword = state.contextualKeyword;
|
438 | parseIdentifier();
|
439 | if (contextualKeyword === ContextualKeyword._await) {
|
440 | parseAwait();
|
441 | return false;
|
442 | } else if (
|
443 | contextualKeyword === ContextualKeyword._async &&
|
444 | match(tt._function) &&
|
445 | !canInsertSemicolon()
|
446 | ) {
|
447 | next();
|
448 | parseFunction(functionStart, false, false);
|
449 | return false;
|
450 | } else if (
|
451 | canBeArrow &&
|
452 | !canInsertSemicolon() &&
|
453 | contextualKeyword === ContextualKeyword._async &&
|
454 | match(tt.name)
|
455 | ) {
|
456 | state.scopeDepth++;
|
457 | parseBindingIdentifier(false);
|
458 | expect(tt.arrow);
|
459 |
|
460 | parseArrowExpression(functionStart, startTokenIndex);
|
461 | return true;
|
462 | }
|
463 |
|
464 | if (canBeArrow && !canInsertSemicolon() && match(tt.arrow)) {
|
465 | state.scopeDepth++;
|
466 | markPriorBindingIdentifier(false);
|
467 | expect(tt.arrow);
|
468 | parseArrowExpression(functionStart, startTokenIndex);
|
469 | return true;
|
470 | }
|
471 |
|
472 | state.tokens[state.tokens.length - 1].identifierRole = IdentifierRole.Access;
|
473 | return false;
|
474 | }
|
475 |
|
476 | case tt._do: {
|
477 | next();
|
478 | parseBlock(false);
|
479 | return false;
|
480 | }
|
481 |
|
482 | case tt.parenL: {
|
483 | const wasArrow = parseParenAndDistinguishExpression(canBeArrow);
|
484 | return wasArrow;
|
485 | }
|
486 |
|
487 | case tt.bracketL:
|
488 | next();
|
489 | parseExprList(tt.bracketR, true);
|
490 | return false;
|
491 |
|
492 | case tt.braceL:
|
493 | parseObj(false, false);
|
494 | return false;
|
495 |
|
496 | case tt._function:
|
497 | parseFunctionExpression();
|
498 | return false;
|
499 |
|
500 | case tt.at:
|
501 | parseDecorators();
|
502 |
|
503 |
|
504 | case tt._class:
|
505 | parseClass(false);
|
506 | return false;
|
507 |
|
508 | case tt._new:
|
509 | parseNew();
|
510 | return false;
|
511 |
|
512 | case tt.backQuote:
|
513 | parseTemplate();
|
514 | return false;
|
515 |
|
516 | case tt.doubleColon: {
|
517 | next();
|
518 | parseNoCallExpr();
|
519 | return false;
|
520 | }
|
521 |
|
522 | default:
|
523 | unexpected();
|
524 | return false;
|
525 | }
|
526 | }
|
527 |
|
528 | function parseMaybePrivateName() {
|
529 | eat(tt.hash);
|
530 | parseIdentifier();
|
531 | }
|
532 |
|
533 | function parseFunctionExpression() {
|
534 | const functionStart = state.start;
|
535 | parseIdentifier();
|
536 | if (eat(tt.dot)) {
|
537 |
|
538 | parseMetaProperty();
|
539 | }
|
540 | parseFunction(functionStart, false);
|
541 | }
|
542 |
|
543 | function parseMetaProperty() {
|
544 | parseIdentifier();
|
545 | }
|
546 |
|
547 | function parseImportMetaProperty() {
|
548 | parseIdentifier();
|
549 | expect(tt.dot);
|
550 |
|
551 | parseMetaProperty();
|
552 | }
|
553 |
|
554 | export function parseLiteral() {
|
555 | next();
|
556 | }
|
557 |
|
558 | export function parseParenExpression() {
|
559 | expect(tt.parenL);
|
560 | parseExpression();
|
561 | expect(tt.parenR);
|
562 | }
|
563 |
|
564 |
|
565 | function parseParenAndDistinguishExpression(canBeArrow) {
|
566 |
|
567 |
|
568 | const snapshot = state.snapshot();
|
569 |
|
570 | const startTokenIndex = state.tokens.length;
|
571 | expect(tt.parenL);
|
572 |
|
573 | let first = true;
|
574 |
|
575 | while (!match(tt.parenR) && !state.error) {
|
576 | if (first) {
|
577 | first = false;
|
578 | } else {
|
579 | expect(tt.comma);
|
580 | if (match(tt.parenR)) {
|
581 | break;
|
582 | }
|
583 | }
|
584 |
|
585 | if (match(tt.ellipsis)) {
|
586 | parseRest(false );
|
587 | parseParenItem();
|
588 | break;
|
589 | } else {
|
590 | parseMaybeAssign(false, true);
|
591 | }
|
592 | }
|
593 |
|
594 | expect(tt.parenR);
|
595 |
|
596 | if (canBeArrow && shouldParseArrow()) {
|
597 | const wasArrow = parseArrow();
|
598 | if (wasArrow) {
|
599 |
|
600 |
|
601 | state.restoreFromSnapshot(snapshot);
|
602 | state.scopeDepth++;
|
603 |
|
604 | const functionStart = state.start;
|
605 |
|
606 | parseFunctionParams();
|
607 | parseArrow();
|
608 | parseArrowExpression(functionStart, startTokenIndex);
|
609 | return true;
|
610 | }
|
611 | }
|
612 |
|
613 | return false;
|
614 | }
|
615 |
|
616 | function shouldParseArrow() {
|
617 | return match(tt.colon) || !canInsertSemicolon();
|
618 | }
|
619 |
|
620 |
|
621 | export function parseArrow() {
|
622 | if (isTypeScriptEnabled) {
|
623 | return tsParseArrow();
|
624 | } else if (isFlowEnabled) {
|
625 | return flowParseArrow();
|
626 | } else {
|
627 | return eat(tt.arrow);
|
628 | }
|
629 | }
|
630 |
|
631 | function parseParenItem() {
|
632 | if (isTypeScriptEnabled || isFlowEnabled) {
|
633 | typedParseParenItem();
|
634 | }
|
635 | }
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 | function parseNew() {
|
643 | expect(tt._new);
|
644 | if (eat(tt.dot)) {
|
645 |
|
646 | parseMetaProperty();
|
647 | return;
|
648 | }
|
649 | parseNoCallExpr();
|
650 | eat(tt.questionDot);
|
651 | parseNewArguments();
|
652 | }
|
653 |
|
654 | function parseNewArguments() {
|
655 | if (isTypeScriptEnabled) {
|
656 | tsStartParseNewArguments();
|
657 | } else if (isFlowEnabled) {
|
658 | flowStartParseNewArguments();
|
659 | }
|
660 | if (eat(tt.parenL)) {
|
661 | parseExprList(tt.parenR);
|
662 | }
|
663 | }
|
664 |
|
665 | export function parseTemplate() {
|
666 |
|
667 | nextTemplateToken();
|
668 |
|
669 | nextTemplateToken();
|
670 | while (!match(tt.backQuote) && !state.error) {
|
671 | expect(tt.dollarBraceL);
|
672 | parseExpression();
|
673 |
|
674 | nextTemplateToken();
|
675 |
|
676 | nextTemplateToken();
|
677 | }
|
678 | next();
|
679 | }
|
680 |
|
681 |
|
682 | export function parseObj(isPattern, isBlockScope) {
|
683 |
|
684 | const contextId = getNextContextId();
|
685 | let first = true;
|
686 |
|
687 | next();
|
688 | state.tokens[state.tokens.length - 1].contextId = contextId;
|
689 |
|
690 | while (!eat(tt.braceR) && !state.error) {
|
691 | if (first) {
|
692 | first = false;
|
693 | } else {
|
694 | expect(tt.comma);
|
695 | if (eat(tt.braceR)) {
|
696 | break;
|
697 | }
|
698 | }
|
699 |
|
700 | let isGenerator = false;
|
701 | if (match(tt.ellipsis)) {
|
702 | const previousIndex = state.tokens.length;
|
703 | parseSpread();
|
704 | if (isPattern) {
|
705 |
|
706 | if (state.tokens.length === previousIndex + 2) {
|
707 | markPriorBindingIdentifier(isBlockScope);
|
708 | }
|
709 | if (eat(tt.braceR)) {
|
710 | break;
|
711 | }
|
712 | }
|
713 | continue;
|
714 | }
|
715 |
|
716 | if (!isPattern) {
|
717 | isGenerator = eat(tt.star);
|
718 | }
|
719 |
|
720 | if (!isPattern && isContextual(ContextualKeyword._async)) {
|
721 | if (isGenerator) unexpected();
|
722 |
|
723 | parseIdentifier();
|
724 | if (
|
725 | match(tt.colon) ||
|
726 | match(tt.parenL) ||
|
727 | match(tt.braceR) ||
|
728 | match(tt.eq) ||
|
729 | match(tt.comma)
|
730 | ) {
|
731 |
|
732 | } else {
|
733 | if (match(tt.star)) {
|
734 | next();
|
735 | isGenerator = true;
|
736 | }
|
737 | parsePropertyName(contextId);
|
738 | }
|
739 | } else {
|
740 | parsePropertyName(contextId);
|
741 | }
|
742 |
|
743 | parseObjPropValue(isGenerator, isPattern, isBlockScope, contextId);
|
744 | }
|
745 |
|
746 | state.tokens[state.tokens.length - 1].contextId = contextId;
|
747 | }
|
748 |
|
749 | function isGetterOrSetterMethod(isPattern) {
|
750 |
|
751 |
|
752 | return (
|
753 | !isPattern &&
|
754 | (match(tt.string) ||
|
755 | match(tt.num) ||
|
756 | match(tt.bracketL) ||
|
757 | match(tt.name) ||
|
758 | !!(state.type & TokenType.IS_KEYWORD))
|
759 | );
|
760 | }
|
761 |
|
762 |
|
763 | function parseObjectMethod(
|
764 | isGenerator,
|
765 | isPattern,
|
766 | objectContextId,
|
767 | ) {
|
768 |
|
769 |
|
770 | const functionStart = state.start;
|
771 | if (match(tt.parenL)) {
|
772 | if (isPattern) unexpected();
|
773 | parseMethod(functionStart, isGenerator, false);
|
774 | return true;
|
775 | }
|
776 |
|
777 | if (isGetterOrSetterMethod(isPattern)) {
|
778 | parsePropertyName(objectContextId);
|
779 | parseMethod(functionStart, false, false);
|
780 | return true;
|
781 | }
|
782 | return false;
|
783 | }
|
784 |
|
785 | function parseObjectProperty(isPattern, isBlockScope) {
|
786 | if (eat(tt.colon)) {
|
787 | if (isPattern) {
|
788 | parseMaybeDefault(isBlockScope);
|
789 | } else {
|
790 | parseMaybeAssign(false);
|
791 | }
|
792 | return;
|
793 | }
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 | if (isPattern) {
|
801 | state.tokens[state.tokens.length - 1].identifierRole = isBlockScope
|
802 | ? IdentifierRole.ObjectShorthandBlockScopedDeclaration
|
803 | : IdentifierRole.ObjectShorthandFunctionScopedDeclaration;
|
804 | } else {
|
805 | state.tokens[state.tokens.length - 1].identifierRole = IdentifierRole.ObjectShorthand;
|
806 | }
|
807 |
|
808 |
|
809 |
|
810 | parseMaybeDefault(isBlockScope, true);
|
811 | }
|
812 |
|
813 | function parseObjPropValue(
|
814 | isGenerator,
|
815 | isPattern,
|
816 | isBlockScope,
|
817 | objectContextId,
|
818 | ) {
|
819 | if (isTypeScriptEnabled) {
|
820 | tsStartParseObjPropValue();
|
821 | } else if (isFlowEnabled) {
|
822 | flowStartParseObjPropValue();
|
823 | }
|
824 | const wasMethod = parseObjectMethod(isGenerator, isPattern, objectContextId);
|
825 | if (!wasMethod) {
|
826 | parseObjectProperty(isPattern, isBlockScope);
|
827 | }
|
828 | }
|
829 |
|
830 | export function parsePropertyName(objectContextId) {
|
831 | if (isFlowEnabled) {
|
832 | flowParseVariance();
|
833 | }
|
834 | if (eat(tt.bracketL)) {
|
835 | state.tokens[state.tokens.length - 1].contextId = objectContextId;
|
836 | parseMaybeAssign();
|
837 | expect(tt.bracketR);
|
838 | state.tokens[state.tokens.length - 1].contextId = objectContextId;
|
839 | } else {
|
840 | if (match(tt.num) || match(tt.string)) {
|
841 | parseExprAtom();
|
842 | } else {
|
843 | parseMaybePrivateName();
|
844 | }
|
845 |
|
846 | state.tokens[state.tokens.length - 1].identifierRole = IdentifierRole.ObjectKey;
|
847 | state.tokens[state.tokens.length - 1].contextId = objectContextId;
|
848 | }
|
849 | }
|
850 |
|
851 |
|
852 | export function parseMethod(
|
853 | functionStart,
|
854 | isGenerator,
|
855 | isConstructor,
|
856 | ) {
|
857 | const funcContextId = getNextContextId();
|
858 |
|
859 | state.scopeDepth++;
|
860 | const startTokenIndex = state.tokens.length;
|
861 | const allowModifiers = isConstructor;
|
862 | parseFunctionParams(allowModifiers, funcContextId);
|
863 | parseFunctionBodyAndFinish(
|
864 | functionStart,
|
865 | isGenerator,
|
866 | false ,
|
867 | funcContextId,
|
868 | );
|
869 | const endTokenIndex = state.tokens.length;
|
870 | state.scopes.push(new Scope(startTokenIndex, endTokenIndex, true));
|
871 | state.scopeDepth--;
|
872 | }
|
873 |
|
874 |
|
875 |
|
876 |
|
877 | export function parseArrowExpression(functionStart, startTokenIndex) {
|
878 | parseFunctionBody(functionStart, false , true);
|
879 | const endTokenIndex = state.tokens.length;
|
880 | state.scopes.push(new Scope(startTokenIndex, endTokenIndex, true));
|
881 | state.scopeDepth--;
|
882 | }
|
883 |
|
884 | export function parseFunctionBodyAndFinish(
|
885 | functionStart,
|
886 | isGenerator,
|
887 | allowExpressionBody = false,
|
888 | funcContextId = 0,
|
889 | ) {
|
890 | if (isTypeScriptEnabled) {
|
891 | tsParseFunctionBodyAndFinish(functionStart, isGenerator, allowExpressionBody, funcContextId);
|
892 | } else if (isFlowEnabled) {
|
893 | flowParseFunctionBodyAndFinish(functionStart, isGenerator, allowExpressionBody, funcContextId);
|
894 | } else {
|
895 | parseFunctionBody(functionStart, isGenerator, allowExpressionBody, funcContextId);
|
896 | }
|
897 | }
|
898 |
|
899 |
|
900 | export function parseFunctionBody(
|
901 | functionStart,
|
902 | isGenerator,
|
903 | allowExpression,
|
904 | funcContextId = 0,
|
905 | ) {
|
906 | const isExpression = allowExpression && !match(tt.braceL);
|
907 |
|
908 | if (isExpression) {
|
909 | parseMaybeAssign();
|
910 | } else {
|
911 | parseBlock(true , true , funcContextId);
|
912 | }
|
913 | }
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 | function parseExprList(close, allowEmpty = false) {
|
922 | let first = true;
|
923 | while (!eat(close) && !state.error) {
|
924 | if (first) {
|
925 | first = false;
|
926 | } else {
|
927 | expect(tt.comma);
|
928 | if (eat(close)) break;
|
929 | }
|
930 | parseExprListItem(allowEmpty);
|
931 | }
|
932 | }
|
933 |
|
934 | function parseExprListItem(allowEmpty) {
|
935 | if (allowEmpty && match(tt.comma)) {
|
936 |
|
937 | } else if (match(tt.ellipsis)) {
|
938 | parseSpread();
|
939 | parseParenItem();
|
940 | } else {
|
941 | parseMaybeAssign(false, true);
|
942 | }
|
943 | }
|
944 |
|
945 |
|
946 | export function parseIdentifier() {
|
947 | next();
|
948 | state.tokens[state.tokens.length - 1].type = tt.name;
|
949 | }
|
950 |
|
951 |
|
952 | function parseAwait() {
|
953 | parseMaybeUnary();
|
954 | }
|
955 |
|
956 |
|
957 | function parseYield() {
|
958 | next();
|
959 | if (!match(tt.semi) && !canInsertSemicolon()) {
|
960 | eat(tt.star);
|
961 | parseMaybeAssign();
|
962 | }
|
963 | }
|