UNPKG

32 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports, "__esModule", {value: true});/* eslint max-len: 0 */
2
3// A recursive descent parser operates by defining functions for all
4// syntactic elements, and recursively calling those, each function
5// advancing the input stream and returning an AST node. Precedence
6// of constructs (for example, the fact that `!x[1]` means `!(x[1])`
7// instead of `(!x)[1]` is handled by the fact that the parser
8// function that parses unary prefix operators is called first, and
9// in turn calls the function that parses `[]` subscripts — that
10// way, it'll receive the node for `x[1]` already parsed, and wraps
11// *that* in the unary operator node.
12//
13// Acorn uses an [operator precedence parser][opp] to handle binary
14// operator precedence, because it is much more compact than using
15// the technique outlined above, which uses different, nesting
16// functions to specify precedence, for all of the ten binary
17// precedence levels that JavaScript defines.
18//
19// [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
20
21
22
23
24
25
26
27
28
29
30
31var _flow = require('../plugins/flow');
32var _index = require('../plugins/jsx/index');
33var _types = require('../plugins/types');
34
35
36
37
38
39
40
41
42
43
44var _typescript = require('../plugins/typescript');
45
46
47
48
49
50
51
52
53
54
55var _index3 = require('../tokenizer/index');
56var _keywords = require('../tokenizer/keywords');
57var _state = require('../tokenizer/state');
58var _types3 = require('../tokenizer/types');
59var _base = require('./base');
60
61
62
63
64
65
66var _lval = require('./lval');
67
68
69
70
71
72
73var _statement = require('./statement');
74
75
76
77
78
79
80
81var _util = require('./util');
82
83 class StopState {
84
85 constructor(stop) {
86 this.stop = stop;
87 }
88} exports.StopState = StopState;
89
90// ### Expression parsing
91
92// These nest, from the most general expression type at the top to
93// 'atomic', nondivisible expression types at the bottom. Most of
94// the functions will simply let the function (s) below them parse,
95// and, *if* the syntactic construct they handle is present, wrap
96// the AST node that the inner parser gave them in another node.
97 function parseExpression(noIn = false) {
98 parseMaybeAssign(noIn);
99 if (_index3.match.call(void 0, _types3.TokenType.comma)) {
100 while (_index3.eat.call(void 0, _types3.TokenType.comma)) {
101 parseMaybeAssign(noIn);
102 }
103 }
104} exports.parseExpression = parseExpression;
105
106/**
107 * noIn is used when parsing a for loop so that we don't interpret a following "in" as the binary
108 * operatior.
109 * isWithinParens is used to indicate that we're parsing something that might be a comma expression
110 * or might be an arrow function or might be a Flow type assertion (which requires explicit parens).
111 * In these cases, we should allow : and ?: after the initial "left" part.
112 */
113 function parseMaybeAssign(noIn = false, isWithinParens = false) {
114 if (_base.isTypeScriptEnabled) {
115 return _typescript.tsParseMaybeAssign.call(void 0, noIn, isWithinParens);
116 } else if (_base.isFlowEnabled) {
117 return _flow.flowParseMaybeAssign.call(void 0, noIn, isWithinParens);
118 } else {
119 return baseParseMaybeAssign(noIn, isWithinParens);
120 }
121} exports.parseMaybeAssign = parseMaybeAssign;
122
123// Parse an assignment expression. This includes applications of
124// operators like `+=`.
125// Returns true if the expression was an arrow function.
126 function baseParseMaybeAssign(noIn, isWithinParens) {
127 if (_index3.match.call(void 0, _types3.TokenType._yield)) {
128 parseYield();
129 return false;
130 }
131
132 if (_index3.match.call(void 0, _types3.TokenType.parenL) || _index3.match.call(void 0, _types3.TokenType.name) || _index3.match.call(void 0, _types3.TokenType._yield)) {
133 _base.state.potentialArrowAt = _base.state.start;
134 }
135
136 const wasArrow = parseMaybeConditional(noIn);
137 if (isWithinParens) {
138 parseParenItem();
139 }
140 if (_base.state.type & _types3.TokenType.IS_ASSIGN) {
141 _index3.next.call(void 0, );
142 parseMaybeAssign(noIn);
143 return false;
144 }
145 return wasArrow;
146} exports.baseParseMaybeAssign = baseParseMaybeAssign;
147
148// Parse a ternary conditional (`?:`) operator.
149// Returns true if the expression was an arrow function.
150function parseMaybeConditional(noIn) {
151 const wasArrow = parseExprOps(noIn);
152 if (wasArrow) {
153 return true;
154 }
155 parseConditional(noIn);
156 return false;
157}
158
159function parseConditional(noIn) {
160 if (_base.isTypeScriptEnabled || _base.isFlowEnabled) {
161 _types.typedParseConditional.call(void 0, noIn);
162 } else {
163 baseParseConditional(noIn);
164 }
165}
166
167 function baseParseConditional(noIn) {
168 if (_index3.eat.call(void 0, _types3.TokenType.question)) {
169 parseMaybeAssign();
170 _util.expect.call(void 0, _types3.TokenType.colon);
171 parseMaybeAssign(noIn);
172 }
173} exports.baseParseConditional = baseParseConditional;
174
175// Start the precedence parser.
176// Returns true if this was an arrow function
177function parseExprOps(noIn) {
178 const wasArrow = parseMaybeUnary();
179 if (wasArrow) {
180 return true;
181 }
182 parseExprOp(-1, noIn);
183 return false;
184}
185
186// Parse binary operators with the operator precedence parsing
187// algorithm. `left` is the left-hand side of the operator.
188// `minPrec` provides context that allows the function to stop and
189// defer further parser to one of its callers when it encounters an
190// operator that has a lower precedence than the set it is parsing.
191function parseExprOp(minPrec, noIn) {
192 if (
193 _base.isTypeScriptEnabled &&
194 (_types3.TokenType._in & _types3.TokenType.PRECEDENCE_MASK) > minPrec &&
195 !_util.hasPrecedingLineBreak.call(void 0, ) &&
196 _util.eatContextual.call(void 0, _keywords.ContextualKeyword._as)
197 ) {
198 _base.state.tokens[_base.state.tokens.length - 1].type = _types3.TokenType._as;
199 const oldIsType = _index3.pushTypeContext.call(void 0, 1);
200 _typescript.tsParseType.call(void 0, );
201 _index3.popTypeContext.call(void 0, oldIsType);
202 parseExprOp(minPrec, noIn);
203 return;
204 }
205
206 const prec = _base.state.type & _types3.TokenType.PRECEDENCE_MASK;
207 if (prec > 0 && (!noIn || !_index3.match.call(void 0, _types3.TokenType._in))) {
208 if (prec > minPrec) {
209 const op = _base.state.type;
210 _index3.next.call(void 0, );
211
212 parseMaybeUnary();
213 parseExprOp(op & _types3.TokenType.IS_RIGHT_ASSOCIATIVE ? prec - 1 : prec, noIn);
214 parseExprOp(minPrec, noIn);
215 }
216 }
217}
218
219// Parse unary operators, both prefix and postfix.
220// Returns true if this was an arrow function.
221 function parseMaybeUnary() {
222 if (_base.isTypeScriptEnabled && !_base.isJSXEnabled && _index3.eat.call(void 0, _types3.TokenType.lessThan)) {
223 _typescript.tsParseTypeAssertion.call(void 0, );
224 return false;
225 }
226
227 if (_base.state.type & _types3.TokenType.IS_PREFIX) {
228 _index3.next.call(void 0, );
229 parseMaybeUnary();
230 return false;
231 }
232
233 const wasArrow = parseExprSubscripts();
234 if (wasArrow) {
235 return true;
236 }
237 while (_base.state.type & _types3.TokenType.IS_POSTFIX && !_util.canInsertSemicolon.call(void 0, )) {
238 // The tokenizer calls everything a preincrement, so make it a postincrement when
239 // we see it in that context.
240 if (_base.state.type === _types3.TokenType.preIncDec) {
241 _base.state.type = _types3.TokenType.postIncDec;
242 }
243 _index3.next.call(void 0, );
244 }
245 return false;
246} exports.parseMaybeUnary = parseMaybeUnary;
247
248// Parse call, dot, and `[]`-subscript expressions.
249// Returns true if this was an arrow function.
250 function parseExprSubscripts() {
251 const startPos = _base.state.start;
252 const wasArrow = parseExprAtom();
253 if (wasArrow) {
254 return true;
255 }
256 parseSubscripts(startPos);
257 return false;
258} exports.parseExprSubscripts = parseExprSubscripts;
259
260function parseSubscripts(startPos, noCalls = false) {
261 if (_base.isFlowEnabled) {
262 _flow.flowParseSubscripts.call(void 0, startPos, noCalls);
263 } else {
264 baseParseSubscripts(startPos, noCalls);
265 }
266}
267
268 function baseParseSubscripts(startPos, noCalls = false) {
269 const stopState = new StopState(false);
270 do {
271 parseSubscript(startPos, noCalls, stopState);
272 } while (!stopState.stop && !_base.state.error);
273} exports.baseParseSubscripts = baseParseSubscripts;
274
275function parseSubscript(startPos, noCalls, stopState) {
276 if (_base.isTypeScriptEnabled) {
277 _typescript.tsParseSubscript.call(void 0, startPos, noCalls, stopState);
278 } else if (_base.isFlowEnabled) {
279 _flow.flowParseSubscript.call(void 0, startPos, noCalls, stopState);
280 } else {
281 baseParseSubscript(startPos, noCalls, stopState);
282 }
283}
284
285/** Set 'state.stop = true' to indicate that we should stop parsing subscripts. */
286 function baseParseSubscript(startPos, noCalls, stopState) {
287 if (!noCalls && _index3.eat.call(void 0, _types3.TokenType.doubleColon)) {
288 parseNoCallExpr();
289 stopState.stop = true;
290 parseSubscripts(startPos, noCalls);
291 } else if (_index3.match.call(void 0, _types3.TokenType.questionDot)) {
292 if (noCalls && _index3.lookaheadType.call(void 0, ) === _types3.TokenType.parenL) {
293 stopState.stop = true;
294 return;
295 }
296 _index3.next.call(void 0, );
297
298 if (_index3.eat.call(void 0, _types3.TokenType.bracketL)) {
299 parseExpression();
300 _util.expect.call(void 0, _types3.TokenType.bracketR);
301 } else if (_index3.eat.call(void 0, _types3.TokenType.parenL)) {
302 parseCallExpressionArguments();
303 } else {
304 parseIdentifier();
305 }
306 } else if (_index3.eat.call(void 0, _types3.TokenType.dot)) {
307 parseMaybePrivateName();
308 } else if (_index3.eat.call(void 0, _types3.TokenType.bracketL)) {
309 parseExpression();
310 _util.expect.call(void 0, _types3.TokenType.bracketR);
311 } else if (!noCalls && _index3.match.call(void 0, _types3.TokenType.parenL)) {
312 if (atPossibleAsync()) {
313 // We see "async", but it's possible it's a usage of the name "async". Parse as if it's a
314 // function call, and if we see an arrow later, backtrack and re-parse as a parameter list.
315 const snapshot = _base.state.snapshot();
316 const startTokenIndex = _base.state.tokens.length;
317 _index3.next.call(void 0, );
318
319 const callContextId = _base.getNextContextId.call(void 0, );
320
321 _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
322 parseCallExpressionArguments();
323 _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
324
325 if (shouldParseAsyncArrow()) {
326 // We hit an arrow, so backtrack and start again parsing function parameters.
327 _base.state.restoreFromSnapshot(snapshot);
328 stopState.stop = true;
329 _base.state.scopeDepth++;
330
331 _statement.parseFunctionParams.call(void 0, );
332 parseAsyncArrowFromCallExpression(startPos, startTokenIndex);
333 }
334 } else {
335 _index3.next.call(void 0, );
336 const callContextId = _base.getNextContextId.call(void 0, );
337 _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
338 parseCallExpressionArguments();
339 _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
340 }
341 } else if (_index3.match.call(void 0, _types3.TokenType.backQuote)) {
342 // Tagged template expression.
343 parseTemplate();
344 } else {
345 stopState.stop = true;
346 }
347} exports.baseParseSubscript = baseParseSubscript;
348
349 function atPossibleAsync() {
350 // This was made less strict than the original version to avoid passing around nodes, but it
351 // should be safe to have rare false positives here.
352 return (
353 _base.state.tokens[_base.state.tokens.length - 1].contextualKeyword === _keywords.ContextualKeyword._async &&
354 !_util.canInsertSemicolon.call(void 0, )
355 );
356} exports.atPossibleAsync = atPossibleAsync;
357
358 function parseCallExpressionArguments() {
359 let first = true;
360 while (!_index3.eat.call(void 0, _types3.TokenType.parenR) && !_base.state.error) {
361 if (first) {
362 first = false;
363 } else {
364 _util.expect.call(void 0, _types3.TokenType.comma);
365 if (_index3.eat.call(void 0, _types3.TokenType.parenR)) {
366 break;
367 }
368 }
369
370 parseExprListItem(false);
371 }
372} exports.parseCallExpressionArguments = parseCallExpressionArguments;
373
374function shouldParseAsyncArrow() {
375 return _index3.match.call(void 0, _types3.TokenType.colon) || _index3.match.call(void 0, _types3.TokenType.arrow);
376}
377
378function parseAsyncArrowFromCallExpression(functionStart, startTokenIndex) {
379 if (_base.isTypeScriptEnabled) {
380 _typescript.tsStartParseAsyncArrowFromCallExpression.call(void 0, );
381 } else if (_base.isFlowEnabled) {
382 _flow.flowStartParseAsyncArrowFromCallExpression.call(void 0, );
383 }
384 _util.expect.call(void 0, _types3.TokenType.arrow);
385 parseArrowExpression(functionStart, startTokenIndex);
386}
387
388// Parse a no-call expression (like argument of `new` or `::` operators).
389
390function parseNoCallExpr() {
391 const startPos = _base.state.start;
392 parseExprAtom();
393 parseSubscripts(startPos, true);
394}
395
396// Parse an atomic expression — either a single token that is an
397// expression, an expression started by a keyword like `function` or
398// `new`, or an expression wrapped in punctuation like `()`, `[]`,
399// or `{}`.
400// Returns true if the parsed expression was an arrow function.
401 function parseExprAtom() {
402 if (_index3.match.call(void 0, _types3.TokenType.jsxText)) {
403 parseLiteral();
404 return false;
405 } else if (_index3.match.call(void 0, _types3.TokenType.lessThan) && _base.isJSXEnabled) {
406 _base.state.type = _types3.TokenType.jsxTagStart;
407 _index.jsxParseElement.call(void 0, );
408 _index3.next.call(void 0, );
409 return false;
410 }
411
412 const canBeArrow = _base.state.potentialArrowAt === _base.state.start;
413 switch (_base.state.type) {
414 case _types3.TokenType.slash:
415 case _types3.TokenType.assign:
416 _index3.retokenizeSlashAsRegex.call(void 0, );
417 // Fall through.
418
419 case _types3.TokenType._super:
420 case _types3.TokenType._this:
421 case _types3.TokenType.regexp:
422 case _types3.TokenType.num:
423 case _types3.TokenType.bigint:
424 case _types3.TokenType.string:
425 case _types3.TokenType._null:
426 case _types3.TokenType._true:
427 case _types3.TokenType._false:
428 _index3.next.call(void 0, );
429 return false;
430
431 case _types3.TokenType._import:
432 if (_index3.lookaheadType.call(void 0, ) === _types3.TokenType.dot) {
433 parseImportMetaProperty();
434 return false;
435 }
436 _index3.next.call(void 0, );
437 return false;
438
439 case _types3.TokenType.name: {
440 const startTokenIndex = _base.state.tokens.length;
441 const functionStart = _base.state.start;
442 const contextualKeyword = _base.state.contextualKeyword;
443 parseIdentifier();
444 if (contextualKeyword === _keywords.ContextualKeyword._await) {
445 parseAwait();
446 return false;
447 } else if (
448 contextualKeyword === _keywords.ContextualKeyword._async &&
449 _index3.match.call(void 0, _types3.TokenType._function) &&
450 !_util.canInsertSemicolon.call(void 0, )
451 ) {
452 _index3.next.call(void 0, );
453 _statement.parseFunction.call(void 0, functionStart, false, false);
454 return false;
455 } else if (
456 canBeArrow &&
457 !_util.canInsertSemicolon.call(void 0, ) &&
458 contextualKeyword === _keywords.ContextualKeyword._async &&
459 _index3.match.call(void 0, _types3.TokenType.name)
460 ) {
461 _base.state.scopeDepth++;
462 _lval.parseBindingIdentifier.call(void 0, false);
463 _util.expect.call(void 0, _types3.TokenType.arrow);
464 // let foo = async bar => {};
465 parseArrowExpression(functionStart, startTokenIndex);
466 return true;
467 }
468
469 if (canBeArrow && !_util.canInsertSemicolon.call(void 0, ) && _index3.match.call(void 0, _types3.TokenType.arrow)) {
470 _base.state.scopeDepth++;
471 _lval.markPriorBindingIdentifier.call(void 0, false);
472 _util.expect.call(void 0, _types3.TokenType.arrow);
473 parseArrowExpression(functionStart, startTokenIndex);
474 return true;
475 }
476
477 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _index3.IdentifierRole.Access;
478 return false;
479 }
480
481 case _types3.TokenType._do: {
482 _index3.next.call(void 0, );
483 _statement.parseBlock.call(void 0, false);
484 return false;
485 }
486
487 case _types3.TokenType.parenL: {
488 const wasArrow = parseParenAndDistinguishExpression(canBeArrow);
489 return wasArrow;
490 }
491
492 case _types3.TokenType.bracketL:
493 _index3.next.call(void 0, );
494 parseExprList(_types3.TokenType.bracketR, true);
495 return false;
496
497 case _types3.TokenType.braceL:
498 parseObj(false, false);
499 return false;
500
501 case _types3.TokenType._function:
502 parseFunctionExpression();
503 return false;
504
505 case _types3.TokenType.at:
506 _statement.parseDecorators.call(void 0, );
507 // Fall through.
508
509 case _types3.TokenType._class:
510 _statement.parseClass.call(void 0, false);
511 return false;
512
513 case _types3.TokenType._new:
514 parseNew();
515 return false;
516
517 case _types3.TokenType.backQuote:
518 parseTemplate();
519 return false;
520
521 case _types3.TokenType.doubleColon: {
522 _index3.next.call(void 0, );
523 parseNoCallExpr();
524 return false;
525 }
526
527 default:
528 _util.unexpected.call(void 0, );
529 return false;
530 }
531} exports.parseExprAtom = parseExprAtom;
532
533function parseMaybePrivateName() {
534 _index3.eat.call(void 0, _types3.TokenType.hash);
535 parseIdentifier();
536}
537
538function parseFunctionExpression() {
539 const functionStart = _base.state.start;
540 parseIdentifier();
541 if (_index3.eat.call(void 0, _types3.TokenType.dot)) {
542 // function.sent
543 parseMetaProperty();
544 }
545 _statement.parseFunction.call(void 0, functionStart, false);
546}
547
548function parseMetaProperty() {
549 parseIdentifier();
550}
551
552function parseImportMetaProperty() {
553 parseIdentifier();
554 _util.expect.call(void 0, _types3.TokenType.dot);
555 // import.meta
556 parseMetaProperty();
557}
558
559 function parseLiteral() {
560 _index3.next.call(void 0, );
561} exports.parseLiteral = parseLiteral;
562
563 function parseParenExpression() {
564 _util.expect.call(void 0, _types3.TokenType.parenL);
565 parseExpression();
566 _util.expect.call(void 0, _types3.TokenType.parenR);
567} exports.parseParenExpression = parseParenExpression;
568
569// Returns true if this was an arrow expression.
570function parseParenAndDistinguishExpression(canBeArrow) {
571 // Assume this is a normal parenthesized expression, but if we see an arrow, we'll bail and
572 // start over as a parameter list.
573 const snapshot = _base.state.snapshot();
574
575 const startTokenIndex = _base.state.tokens.length;
576 _util.expect.call(void 0, _types3.TokenType.parenL);
577
578 let first = true;
579
580 while (!_index3.match.call(void 0, _types3.TokenType.parenR) && !_base.state.error) {
581 if (first) {
582 first = false;
583 } else {
584 _util.expect.call(void 0, _types3.TokenType.comma);
585 if (_index3.match.call(void 0, _types3.TokenType.parenR)) {
586 break;
587 }
588 }
589
590 if (_index3.match.call(void 0, _types3.TokenType.ellipsis)) {
591 _lval.parseRest.call(void 0, false /* isBlockScope */);
592 parseParenItem();
593 break;
594 } else {
595 parseMaybeAssign(false, true);
596 }
597 }
598
599 _util.expect.call(void 0, _types3.TokenType.parenR);
600
601 if (canBeArrow && shouldParseArrow()) {
602 const wasArrow = parseArrow();
603 if (wasArrow) {
604 // It was an arrow function this whole time, so start over and parse it as params so that we
605 // get proper token annotations.
606 _base.state.restoreFromSnapshot(snapshot);
607 _base.state.scopeDepth++;
608 // We don't need to worry about functionStart for arrow functions, so just use something.
609 const functionStart = _base.state.start;
610 // Don't specify a context ID because arrow functions don't need a context ID.
611 _statement.parseFunctionParams.call(void 0, );
612 parseArrow();
613 parseArrowExpression(functionStart, startTokenIndex);
614 return true;
615 }
616 }
617
618 return false;
619}
620
621function shouldParseArrow() {
622 return _index3.match.call(void 0, _types3.TokenType.colon) || !_util.canInsertSemicolon.call(void 0, );
623}
624
625// Returns whether there was an arrow token.
626 function parseArrow() {
627 if (_base.isTypeScriptEnabled) {
628 return _typescript.tsParseArrow.call(void 0, );
629 } else if (_base.isFlowEnabled) {
630 return _flow.flowParseArrow.call(void 0, );
631 } else {
632 return _index3.eat.call(void 0, _types3.TokenType.arrow);
633 }
634} exports.parseArrow = parseArrow;
635
636function parseParenItem() {
637 if (_base.isTypeScriptEnabled || _base.isFlowEnabled) {
638 _types.typedParseParenItem.call(void 0, );
639 }
640}
641
642// New's precedence is slightly tricky. It must allow its argument to
643// be a `[]` or dot subscript expression, but not a call — at least,
644// not without wrapping it in parentheses. Thus, it uses the noCalls
645// argument to parseSubscripts to prevent it from consuming the
646// argument list.
647function parseNew() {
648 _util.expect.call(void 0, _types3.TokenType._new);
649 if (_index3.eat.call(void 0, _types3.TokenType.dot)) {
650 // new.target
651 parseMetaProperty();
652 return;
653 }
654 parseNoCallExpr();
655 _index3.eat.call(void 0, _types3.TokenType.questionDot);
656 parseNewArguments();
657}
658
659function parseNewArguments() {
660 if (_base.isTypeScriptEnabled) {
661 _typescript.tsStartParseNewArguments.call(void 0, );
662 } else if (_base.isFlowEnabled) {
663 _flow.flowStartParseNewArguments.call(void 0, );
664 }
665 if (_index3.eat.call(void 0, _types3.TokenType.parenL)) {
666 parseExprList(_types3.TokenType.parenR);
667 }
668}
669
670 function parseTemplate() {
671 // Finish `, read quasi
672 _index3.nextTemplateToken.call(void 0, );
673 // Finish quasi, read ${
674 _index3.nextTemplateToken.call(void 0, );
675 while (!_index3.match.call(void 0, _types3.TokenType.backQuote) && !_base.state.error) {
676 _util.expect.call(void 0, _types3.TokenType.dollarBraceL);
677 parseExpression();
678 // Finish }, read quasi
679 _index3.nextTemplateToken.call(void 0, );
680 // Finish quasi, read either ${ or `
681 _index3.nextTemplateToken.call(void 0, );
682 }
683 _index3.next.call(void 0, );
684} exports.parseTemplate = parseTemplate;
685
686// Parse an object literal or binding pattern.
687 function parseObj(isPattern, isBlockScope) {
688 // Attach a context ID to the object open and close brace and each object key.
689 const contextId = _base.getNextContextId.call(void 0, );
690 let first = true;
691
692 _index3.next.call(void 0, );
693 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
694
695 while (!_index3.eat.call(void 0, _types3.TokenType.braceR) && !_base.state.error) {
696 if (first) {
697 first = false;
698 } else {
699 _util.expect.call(void 0, _types3.TokenType.comma);
700 if (_index3.eat.call(void 0, _types3.TokenType.braceR)) {
701 break;
702 }
703 }
704
705 let isGenerator = false;
706 if (_index3.match.call(void 0, _types3.TokenType.ellipsis)) {
707 const previousIndex = _base.state.tokens.length;
708 _lval.parseSpread.call(void 0, );
709 if (isPattern) {
710 // Mark role when the only thing being spread over is an identifier.
711 if (_base.state.tokens.length === previousIndex + 2) {
712 _lval.markPriorBindingIdentifier.call(void 0, isBlockScope);
713 }
714 if (_index3.eat.call(void 0, _types3.TokenType.braceR)) {
715 break;
716 }
717 }
718 continue;
719 }
720
721 if (!isPattern) {
722 isGenerator = _index3.eat.call(void 0, _types3.TokenType.star);
723 }
724
725 if (!isPattern && _util.isContextual.call(void 0, _keywords.ContextualKeyword._async)) {
726 if (isGenerator) _util.unexpected.call(void 0, );
727
728 parseIdentifier();
729 if (
730 _index3.match.call(void 0, _types3.TokenType.colon) ||
731 _index3.match.call(void 0, _types3.TokenType.parenL) ||
732 _index3.match.call(void 0, _types3.TokenType.braceR) ||
733 _index3.match.call(void 0, _types3.TokenType.eq) ||
734 _index3.match.call(void 0, _types3.TokenType.comma)
735 ) {
736 // This is a key called "async" rather than an async function.
737 } else {
738 if (_index3.match.call(void 0, _types3.TokenType.star)) {
739 _index3.next.call(void 0, );
740 isGenerator = true;
741 }
742 parsePropertyName(contextId);
743 }
744 } else {
745 parsePropertyName(contextId);
746 }
747
748 parseObjPropValue(isGenerator, isPattern, isBlockScope, contextId);
749 }
750
751 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
752} exports.parseObj = parseObj;
753
754function isGetterOrSetterMethod(isPattern) {
755 // We go off of the next and don't bother checking if the node key is actually "get" or "set".
756 // This lets us avoid generating a node, and should only make the validation worse.
757 return (
758 !isPattern &&
759 (_index3.match.call(void 0, _types3.TokenType.string) || // get "string"() {}
760 _index3.match.call(void 0, _types3.TokenType.num) || // get 1() {}
761 _index3.match.call(void 0, _types3.TokenType.bracketL) || // get ["string"]() {}
762 _index3.match.call(void 0, _types3.TokenType.name) || // get foo() {}
763 !!(_base.state.type & _types3.TokenType.IS_KEYWORD)) // get debugger() {}
764 );
765}
766
767// Returns true if this was a method.
768function parseObjectMethod(
769 isGenerator,
770 isPattern,
771 objectContextId,
772) {
773 // We don't need to worry about modifiers because object methods can't have optional bodies, so
774 // the start will never be used.
775 const functionStart = _base.state.start;
776 if (_index3.match.call(void 0, _types3.TokenType.parenL)) {
777 if (isPattern) _util.unexpected.call(void 0, );
778 parseMethod(functionStart, isGenerator, /* isConstructor */ false);
779 return true;
780 }
781
782 if (isGetterOrSetterMethod(isPattern)) {
783 parsePropertyName(objectContextId);
784 parseMethod(functionStart, /* isGenerator */ false, /* isConstructor */ false);
785 return true;
786 }
787 return false;
788}
789
790function parseObjectProperty(isPattern, isBlockScope) {
791 if (_index3.eat.call(void 0, _types3.TokenType.colon)) {
792 if (isPattern) {
793 _lval.parseMaybeDefault.call(void 0, isBlockScope);
794 } else {
795 parseMaybeAssign(false);
796 }
797 return;
798 }
799
800 // Since there's no colon, we assume this is an object shorthand.
801
802 // If we're in a destructuring, we've now discovered that the key was actually an assignee, so
803 // we need to tag it as a declaration with the appropriate scope. Otherwise, we might need to
804 // transform it on access, so mark it as a normal object shorthand.
805 if (isPattern) {
806 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = isBlockScope
807 ? _index3.IdentifierRole.ObjectShorthandBlockScopedDeclaration
808 : _index3.IdentifierRole.ObjectShorthandFunctionScopedDeclaration;
809 } else {
810 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _index3.IdentifierRole.ObjectShorthand;
811 }
812
813 // Regardless of whether we know this to be a pattern or if we're in an ambiguous context, allow
814 // parsing as if there's a default value.
815 _lval.parseMaybeDefault.call(void 0, isBlockScope, true);
816}
817
818function parseObjPropValue(
819 isGenerator,
820 isPattern,
821 isBlockScope,
822 objectContextId,
823) {
824 if (_base.isTypeScriptEnabled) {
825 _typescript.tsStartParseObjPropValue.call(void 0, );
826 } else if (_base.isFlowEnabled) {
827 _flow.flowStartParseObjPropValue.call(void 0, );
828 }
829 const wasMethod = parseObjectMethod(isGenerator, isPattern, objectContextId);
830 if (!wasMethod) {
831 parseObjectProperty(isPattern, isBlockScope);
832 }
833}
834
835 function parsePropertyName(objectContextId) {
836 if (_base.isFlowEnabled) {
837 _flow.flowParseVariance.call(void 0, );
838 }
839 if (_index3.eat.call(void 0, _types3.TokenType.bracketL)) {
840 _base.state.tokens[_base.state.tokens.length - 1].contextId = objectContextId;
841 parseMaybeAssign();
842 _util.expect.call(void 0, _types3.TokenType.bracketR);
843 _base.state.tokens[_base.state.tokens.length - 1].contextId = objectContextId;
844 } else {
845 if (_index3.match.call(void 0, _types3.TokenType.num) || _index3.match.call(void 0, _types3.TokenType.string)) {
846 parseExprAtom();
847 } else {
848 parseMaybePrivateName();
849 }
850
851 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _index3.IdentifierRole.ObjectKey;
852 _base.state.tokens[_base.state.tokens.length - 1].contextId = objectContextId;
853 }
854} exports.parsePropertyName = parsePropertyName;
855
856// Parse object or class method.
857 function parseMethod(
858 functionStart,
859 isGenerator,
860 isConstructor,
861) {
862 const funcContextId = _base.getNextContextId.call(void 0, );
863
864 _base.state.scopeDepth++;
865 const startTokenIndex = _base.state.tokens.length;
866 const allowModifiers = isConstructor; // For TypeScript parameter properties
867 _statement.parseFunctionParams.call(void 0, allowModifiers, funcContextId);
868 parseFunctionBodyAndFinish(
869 functionStart,
870 isGenerator,
871 false /* allowExpressionBody */,
872 funcContextId,
873 );
874 const endTokenIndex = _base.state.tokens.length;
875 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, true));
876 _base.state.scopeDepth--;
877} exports.parseMethod = parseMethod;
878
879// Parse arrow function expression.
880// If the parameters are provided, they will be converted to an
881// assignable list.
882 function parseArrowExpression(functionStart, startTokenIndex) {
883 parseFunctionBody(functionStart, false /* isGenerator */, true);
884 const endTokenIndex = _base.state.tokens.length;
885 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, true));
886 _base.state.scopeDepth--;
887} exports.parseArrowExpression = parseArrowExpression;
888
889 function parseFunctionBodyAndFinish(
890 functionStart,
891 isGenerator,
892 allowExpressionBody = false,
893 funcContextId = 0,
894) {
895 if (_base.isTypeScriptEnabled) {
896 _typescript.tsParseFunctionBodyAndFinish.call(void 0, functionStart, isGenerator, allowExpressionBody, funcContextId);
897 } else if (_base.isFlowEnabled) {
898 _flow.flowParseFunctionBodyAndFinish.call(void 0, functionStart, isGenerator, allowExpressionBody, funcContextId);
899 } else {
900 parseFunctionBody(functionStart, isGenerator, allowExpressionBody, funcContextId);
901 }
902} exports.parseFunctionBodyAndFinish = parseFunctionBodyAndFinish;
903
904// Parse function body and check parameters.
905 function parseFunctionBody(
906 functionStart,
907 isGenerator,
908 allowExpression,
909 funcContextId = 0,
910) {
911 const isExpression = allowExpression && !_index3.match.call(void 0, _types3.TokenType.braceL);
912
913 if (isExpression) {
914 parseMaybeAssign();
915 } else {
916 _statement.parseBlock.call(void 0, true /* allowDirectives */, true /* isFunctionScope */, funcContextId);
917 }
918} exports.parseFunctionBody = parseFunctionBody;
919
920// Parses a comma-separated list of expressions, and returns them as
921// an array. `close` is the token type that ends the list, and
922// `allowEmpty` can be turned on to allow subsequent commas with
923// nothing in between them to be parsed as `null` (which is needed
924// for array literals).
925
926function parseExprList(close, allowEmpty = false) {
927 let first = true;
928 while (!_index3.eat.call(void 0, close) && !_base.state.error) {
929 if (first) {
930 first = false;
931 } else {
932 _util.expect.call(void 0, _types3.TokenType.comma);
933 if (_index3.eat.call(void 0, close)) break;
934 }
935 parseExprListItem(allowEmpty);
936 }
937}
938
939function parseExprListItem(allowEmpty) {
940 if (allowEmpty && _index3.match.call(void 0, _types3.TokenType.comma)) {
941 // Empty item; nothing more to parse for this item.
942 } else if (_index3.match.call(void 0, _types3.TokenType.ellipsis)) {
943 _lval.parseSpread.call(void 0, );
944 parseParenItem();
945 } else {
946 parseMaybeAssign(false, true);
947 }
948}
949
950// Parse the next token as an identifier.
951 function parseIdentifier() {
952 _index3.next.call(void 0, );
953 _base.state.tokens[_base.state.tokens.length - 1].type = _types3.TokenType.name;
954} exports.parseIdentifier = parseIdentifier;
955
956// Parses await expression inside async function.
957function parseAwait() {
958 parseMaybeUnary();
959}
960
961// Parses yield expression inside generator.
962function parseYield() {
963 _index3.next.call(void 0, );
964 if (!_index3.match.call(void 0, _types3.TokenType.semi) && !_util.canInsertSemicolon.call(void 0, )) {
965 _index3.eat.call(void 0, _types3.TokenType.star);
966 parseMaybeAssign();
967 }
968}