UNPKG

31.8 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 _index3.next.call(void 0, );
239 }
240 return false;
241} exports.parseMaybeUnary = parseMaybeUnary;
242
243// Parse call, dot, and `[]`-subscript expressions.
244// Returns true if this was an arrow function.
245 function parseExprSubscripts() {
246 const startPos = _base.state.start;
247 const wasArrow = parseExprAtom();
248 if (wasArrow) {
249 return true;
250 }
251 parseSubscripts(startPos);
252 return false;
253} exports.parseExprSubscripts = parseExprSubscripts;
254
255function parseSubscripts(startPos, noCalls = false) {
256 if (_base.isFlowEnabled) {
257 _flow.flowParseSubscripts.call(void 0, startPos, noCalls);
258 } else {
259 baseParseSubscripts(startPos, noCalls);
260 }
261}
262
263 function baseParseSubscripts(startPos, noCalls = false) {
264 const stopState = new StopState(false);
265 do {
266 parseSubscript(startPos, noCalls, stopState);
267 } while (!stopState.stop && !_base.state.error);
268} exports.baseParseSubscripts = baseParseSubscripts;
269
270function parseSubscript(startPos, noCalls, stopState) {
271 if (_base.isTypeScriptEnabled) {
272 _typescript.tsParseSubscript.call(void 0, startPos, noCalls, stopState);
273 } else if (_base.isFlowEnabled) {
274 _flow.flowParseSubscript.call(void 0, startPos, noCalls, stopState);
275 } else {
276 baseParseSubscript(startPos, noCalls, stopState);
277 }
278}
279
280/** Set 'state.stop = true' to indicate that we should stop parsing subscripts. */
281 function baseParseSubscript(startPos, noCalls, stopState) {
282 if (!noCalls && _index3.eat.call(void 0, _types3.TokenType.doubleColon)) {
283 parseNoCallExpr();
284 stopState.stop = true;
285 parseSubscripts(startPos, noCalls);
286 } else if (_index3.match.call(void 0, _types3.TokenType.questionDot)) {
287 if (noCalls && _index3.lookaheadType.call(void 0, ) === _types3.TokenType.parenL) {
288 stopState.stop = true;
289 return;
290 }
291 _index3.next.call(void 0, );
292
293 if (_index3.eat.call(void 0, _types3.TokenType.bracketL)) {
294 parseExpression();
295 _util.expect.call(void 0, _types3.TokenType.bracketR);
296 } else if (_index3.eat.call(void 0, _types3.TokenType.parenL)) {
297 parseCallExpressionArguments();
298 } else {
299 parseIdentifier();
300 }
301 } else if (_index3.eat.call(void 0, _types3.TokenType.dot)) {
302 parseMaybePrivateName();
303 } else if (_index3.eat.call(void 0, _types3.TokenType.bracketL)) {
304 parseExpression();
305 _util.expect.call(void 0, _types3.TokenType.bracketR);
306 } else if (!noCalls && _index3.match.call(void 0, _types3.TokenType.parenL)) {
307 if (atPossibleAsync()) {
308 // We see "async", but it's possible it's a usage of the name "async". Parse as if it's a
309 // function call, and if we see an arrow later, backtrack and re-parse as a parameter list.
310 const snapshot = _base.state.snapshot();
311 const startTokenIndex = _base.state.tokens.length;
312 _index3.next.call(void 0, );
313
314 const callContextId = _base.getNextContextId.call(void 0, );
315
316 _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
317 parseCallExpressionArguments();
318 _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
319
320 if (shouldParseAsyncArrow()) {
321 // We hit an arrow, so backtrack and start again parsing function parameters.
322 _base.state.restoreFromSnapshot(snapshot);
323 stopState.stop = true;
324 _base.state.scopeDepth++;
325
326 _statement.parseFunctionParams.call(void 0, );
327 parseAsyncArrowFromCallExpression(startPos, startTokenIndex);
328 }
329 } else {
330 _index3.next.call(void 0, );
331 const callContextId = _base.getNextContextId.call(void 0, );
332 _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
333 parseCallExpressionArguments();
334 _base.state.tokens[_base.state.tokens.length - 1].contextId = callContextId;
335 }
336 } else if (_index3.match.call(void 0, _types3.TokenType.backQuote)) {
337 // Tagged template expression.
338 parseTemplate();
339 } else {
340 stopState.stop = true;
341 }
342} exports.baseParseSubscript = baseParseSubscript;
343
344 function atPossibleAsync() {
345 // This was made less strict than the original version to avoid passing around nodes, but it
346 // should be safe to have rare false positives here.
347 return (
348 _base.state.tokens[_base.state.tokens.length - 1].contextualKeyword === _keywords.ContextualKeyword._async &&
349 !_util.canInsertSemicolon.call(void 0, )
350 );
351} exports.atPossibleAsync = atPossibleAsync;
352
353 function parseCallExpressionArguments() {
354 let first = true;
355 while (!_index3.eat.call(void 0, _types3.TokenType.parenR) && !_base.state.error) {
356 if (first) {
357 first = false;
358 } else {
359 _util.expect.call(void 0, _types3.TokenType.comma);
360 if (_index3.eat.call(void 0, _types3.TokenType.parenR)) {
361 break;
362 }
363 }
364
365 parseExprListItem(false);
366 }
367} exports.parseCallExpressionArguments = parseCallExpressionArguments;
368
369function shouldParseAsyncArrow() {
370 return _index3.match.call(void 0, _types3.TokenType.colon) || _index3.match.call(void 0, _types3.TokenType.arrow);
371}
372
373function parseAsyncArrowFromCallExpression(functionStart, startTokenIndex) {
374 if (_base.isTypeScriptEnabled) {
375 _typescript.tsStartParseAsyncArrowFromCallExpression.call(void 0, );
376 } else if (_base.isFlowEnabled) {
377 _flow.flowStartParseAsyncArrowFromCallExpression.call(void 0, );
378 }
379 _util.expect.call(void 0, _types3.TokenType.arrow);
380 parseArrowExpression(functionStart, startTokenIndex);
381}
382
383// Parse a no-call expression (like argument of `new` or `::` operators).
384
385function parseNoCallExpr() {
386 const startPos = _base.state.start;
387 parseExprAtom();
388 parseSubscripts(startPos, true);
389}
390
391// Parse an atomic expression — either a single token that is an
392// expression, an expression started by a keyword like `function` or
393// `new`, or an expression wrapped in punctuation like `()`, `[]`,
394// or `{}`.
395// Returns true if the parsed expression was an arrow function.
396 function parseExprAtom() {
397 if (_index3.match.call(void 0, _types3.TokenType.jsxText)) {
398 parseLiteral();
399 return false;
400 } else if (_index3.match.call(void 0, _types3.TokenType.lessThan) && _base.isJSXEnabled) {
401 _base.state.type = _types3.TokenType.jsxTagStart;
402 _index.jsxParseElement.call(void 0, );
403 _index3.next.call(void 0, );
404 return false;
405 }
406
407 const canBeArrow = _base.state.potentialArrowAt === _base.state.start;
408 switch (_base.state.type) {
409 case _types3.TokenType.slash:
410 case _types3.TokenType.assign:
411 _index3.retokenizeSlashAsRegex.call(void 0, );
412 // Fall through.
413
414 case _types3.TokenType._super:
415 case _types3.TokenType._this:
416 case _types3.TokenType.regexp:
417 case _types3.TokenType.num:
418 case _types3.TokenType.bigint:
419 case _types3.TokenType.string:
420 case _types3.TokenType._null:
421 case _types3.TokenType._true:
422 case _types3.TokenType._false:
423 _index3.next.call(void 0, );
424 return false;
425
426 case _types3.TokenType._import:
427 if (_index3.lookaheadType.call(void 0, ) === _types3.TokenType.dot) {
428 parseImportMetaProperty();
429 return false;
430 }
431 _index3.next.call(void 0, );
432 return false;
433
434 case _types3.TokenType.name: {
435 const startTokenIndex = _base.state.tokens.length;
436 const functionStart = _base.state.start;
437 const contextualKeyword = _base.state.contextualKeyword;
438 parseIdentifier();
439 if (contextualKeyword === _keywords.ContextualKeyword._await) {
440 parseAwait();
441 return false;
442 } else if (
443 contextualKeyword === _keywords.ContextualKeyword._async &&
444 _index3.match.call(void 0, _types3.TokenType._function) &&
445 !_util.canInsertSemicolon.call(void 0, )
446 ) {
447 _index3.next.call(void 0, );
448 _statement.parseFunction.call(void 0, functionStart, false, false);
449 return false;
450 } else if (
451 canBeArrow &&
452 !_util.canInsertSemicolon.call(void 0, ) &&
453 contextualKeyword === _keywords.ContextualKeyword._async &&
454 _index3.match.call(void 0, _types3.TokenType.name)
455 ) {
456 _base.state.scopeDepth++;
457 _lval.parseBindingIdentifier.call(void 0, false);
458 _util.expect.call(void 0, _types3.TokenType.arrow);
459 // let foo = async bar => {};
460 parseArrowExpression(functionStart, startTokenIndex);
461 return true;
462 }
463
464 if (canBeArrow && !_util.canInsertSemicolon.call(void 0, ) && _index3.match.call(void 0, _types3.TokenType.arrow)) {
465 _base.state.scopeDepth++;
466 _lval.markPriorBindingIdentifier.call(void 0, false);
467 _util.expect.call(void 0, _types3.TokenType.arrow);
468 parseArrowExpression(functionStart, startTokenIndex);
469 return true;
470 }
471
472 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _index3.IdentifierRole.Access;
473 return false;
474 }
475
476 case _types3.TokenType._do: {
477 _index3.next.call(void 0, );
478 _statement.parseBlock.call(void 0, false);
479 return false;
480 }
481
482 case _types3.TokenType.parenL: {
483 const wasArrow = parseParenAndDistinguishExpression(canBeArrow);
484 return wasArrow;
485 }
486
487 case _types3.TokenType.bracketL:
488 _index3.next.call(void 0, );
489 parseExprList(_types3.TokenType.bracketR, true);
490 return false;
491
492 case _types3.TokenType.braceL:
493 parseObj(false, false);
494 return false;
495
496 case _types3.TokenType._function:
497 parseFunctionExpression();
498 return false;
499
500 case _types3.TokenType.at:
501 _statement.parseDecorators.call(void 0, );
502 // Fall through.
503
504 case _types3.TokenType._class:
505 _statement.parseClass.call(void 0, false);
506 return false;
507
508 case _types3.TokenType._new:
509 parseNew();
510 return false;
511
512 case _types3.TokenType.backQuote:
513 parseTemplate();
514 return false;
515
516 case _types3.TokenType.doubleColon: {
517 _index3.next.call(void 0, );
518 parseNoCallExpr();
519 return false;
520 }
521
522 default:
523 _util.unexpected.call(void 0, );
524 return false;
525 }
526} exports.parseExprAtom = parseExprAtom;
527
528function parseMaybePrivateName() {
529 _index3.eat.call(void 0, _types3.TokenType.hash);
530 parseIdentifier();
531}
532
533function parseFunctionExpression() {
534 const functionStart = _base.state.start;
535 parseIdentifier();
536 if (_index3.eat.call(void 0, _types3.TokenType.dot)) {
537 // function.sent
538 parseMetaProperty();
539 }
540 _statement.parseFunction.call(void 0, functionStart, false);
541}
542
543function parseMetaProperty() {
544 parseIdentifier();
545}
546
547function parseImportMetaProperty() {
548 parseIdentifier();
549 _util.expect.call(void 0, _types3.TokenType.dot);
550 // import.meta
551 parseMetaProperty();
552}
553
554 function parseLiteral() {
555 _index3.next.call(void 0, );
556} exports.parseLiteral = parseLiteral;
557
558 function parseParenExpression() {
559 _util.expect.call(void 0, _types3.TokenType.parenL);
560 parseExpression();
561 _util.expect.call(void 0, _types3.TokenType.parenR);
562} exports.parseParenExpression = parseParenExpression;
563
564// Returns true if this was an arrow expression.
565function parseParenAndDistinguishExpression(canBeArrow) {
566 // Assume this is a normal parenthesized expression, but if we see an arrow, we'll bail and
567 // start over as a parameter list.
568 const snapshot = _base.state.snapshot();
569
570 const startTokenIndex = _base.state.tokens.length;
571 _util.expect.call(void 0, _types3.TokenType.parenL);
572
573 let first = true;
574
575 while (!_index3.match.call(void 0, _types3.TokenType.parenR) && !_base.state.error) {
576 if (first) {
577 first = false;
578 } else {
579 _util.expect.call(void 0, _types3.TokenType.comma);
580 if (_index3.match.call(void 0, _types3.TokenType.parenR)) {
581 break;
582 }
583 }
584
585 if (_index3.match.call(void 0, _types3.TokenType.ellipsis)) {
586 _lval.parseRest.call(void 0, false /* isBlockScope */);
587 parseParenItem();
588 break;
589 } else {
590 parseMaybeAssign(false, true);
591 }
592 }
593
594 _util.expect.call(void 0, _types3.TokenType.parenR);
595
596 if (canBeArrow && shouldParseArrow()) {
597 const wasArrow = parseArrow();
598 if (wasArrow) {
599 // It was an arrow function this whole time, so start over and parse it as params so that we
600 // get proper token annotations.
601 _base.state.restoreFromSnapshot(snapshot);
602 _base.state.scopeDepth++;
603 // We don't need to worry about functionStart for arrow functions, so just use something.
604 const functionStart = _base.state.start;
605 // Don't specify a context ID because arrow functions don't need a context ID.
606 _statement.parseFunctionParams.call(void 0, );
607 parseArrow();
608 parseArrowExpression(functionStart, startTokenIndex);
609 return true;
610 }
611 }
612
613 return false;
614}
615
616function shouldParseArrow() {
617 return _index3.match.call(void 0, _types3.TokenType.colon) || !_util.canInsertSemicolon.call(void 0, );
618}
619
620// Returns whether there was an arrow token.
621 function parseArrow() {
622 if (_base.isTypeScriptEnabled) {
623 return _typescript.tsParseArrow.call(void 0, );
624 } else if (_base.isFlowEnabled) {
625 return _flow.flowParseArrow.call(void 0, );
626 } else {
627 return _index3.eat.call(void 0, _types3.TokenType.arrow);
628 }
629} exports.parseArrow = parseArrow;
630
631function parseParenItem() {
632 if (_base.isTypeScriptEnabled || _base.isFlowEnabled) {
633 _types.typedParseParenItem.call(void 0, );
634 }
635}
636
637// New's precedence is slightly tricky. It must allow its argument to
638// be a `[]` or dot subscript expression, but not a call — at least,
639// not without wrapping it in parentheses. Thus, it uses the noCalls
640// argument to parseSubscripts to prevent it from consuming the
641// argument list.
642function parseNew() {
643 _util.expect.call(void 0, _types3.TokenType._new);
644 if (_index3.eat.call(void 0, _types3.TokenType.dot)) {
645 // new.target
646 parseMetaProperty();
647 return;
648 }
649 parseNoCallExpr();
650 _index3.eat.call(void 0, _types3.TokenType.questionDot);
651 parseNewArguments();
652}
653
654function parseNewArguments() {
655 if (_base.isTypeScriptEnabled) {
656 _typescript.tsStartParseNewArguments.call(void 0, );
657 } else if (_base.isFlowEnabled) {
658 _flow.flowStartParseNewArguments.call(void 0, );
659 }
660 if (_index3.eat.call(void 0, _types3.TokenType.parenL)) {
661 parseExprList(_types3.TokenType.parenR);
662 }
663}
664
665 function parseTemplate() {
666 // Finish `, read quasi
667 _index3.nextTemplateToken.call(void 0, );
668 // Finish quasi, read ${
669 _index3.nextTemplateToken.call(void 0, );
670 while (!_index3.match.call(void 0, _types3.TokenType.backQuote) && !_base.state.error) {
671 _util.expect.call(void 0, _types3.TokenType.dollarBraceL);
672 parseExpression();
673 // Finish }, read quasi
674 _index3.nextTemplateToken.call(void 0, );
675 // Finish quasi, read either ${ or `
676 _index3.nextTemplateToken.call(void 0, );
677 }
678 _index3.next.call(void 0, );
679} exports.parseTemplate = parseTemplate;
680
681// Parse an object literal or binding pattern.
682 function parseObj(isPattern, isBlockScope) {
683 // Attach a context ID to the object open and close brace and each object key.
684 const contextId = _base.getNextContextId.call(void 0, );
685 let first = true;
686
687 _index3.next.call(void 0, );
688 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
689
690 while (!_index3.eat.call(void 0, _types3.TokenType.braceR) && !_base.state.error) {
691 if (first) {
692 first = false;
693 } else {
694 _util.expect.call(void 0, _types3.TokenType.comma);
695 if (_index3.eat.call(void 0, _types3.TokenType.braceR)) {
696 break;
697 }
698 }
699
700 let isGenerator = false;
701 if (_index3.match.call(void 0, _types3.TokenType.ellipsis)) {
702 const previousIndex = _base.state.tokens.length;
703 _lval.parseSpread.call(void 0, );
704 if (isPattern) {
705 // Mark role when the only thing being spread over is an identifier.
706 if (_base.state.tokens.length === previousIndex + 2) {
707 _lval.markPriorBindingIdentifier.call(void 0, isBlockScope);
708 }
709 if (_index3.eat.call(void 0, _types3.TokenType.braceR)) {
710 break;
711 }
712 }
713 continue;
714 }
715
716 if (!isPattern) {
717 isGenerator = _index3.eat.call(void 0, _types3.TokenType.star);
718 }
719
720 if (!isPattern && _util.isContextual.call(void 0, _keywords.ContextualKeyword._async)) {
721 if (isGenerator) _util.unexpected.call(void 0, );
722
723 parseIdentifier();
724 if (
725 _index3.match.call(void 0, _types3.TokenType.colon) ||
726 _index3.match.call(void 0, _types3.TokenType.parenL) ||
727 _index3.match.call(void 0, _types3.TokenType.braceR) ||
728 _index3.match.call(void 0, _types3.TokenType.eq) ||
729 _index3.match.call(void 0, _types3.TokenType.comma)
730 ) {
731 // This is a key called "async" rather than an async function.
732 } else {
733 if (_index3.match.call(void 0, _types3.TokenType.star)) {
734 _index3.next.call(void 0, );
735 isGenerator = true;
736 }
737 parsePropertyName(contextId);
738 }
739 } else {
740 parsePropertyName(contextId);
741 }
742
743 parseObjPropValue(isGenerator, isPattern, isBlockScope, contextId);
744 }
745
746 _base.state.tokens[_base.state.tokens.length - 1].contextId = contextId;
747} exports.parseObj = parseObj;
748
749function isGetterOrSetterMethod(isPattern) {
750 // We go off of the next and don't bother checking if the node key is actually "get" or "set".
751 // This lets us avoid generating a node, and should only make the validation worse.
752 return (
753 !isPattern &&
754 (_index3.match.call(void 0, _types3.TokenType.string) || // get "string"() {}
755 _index3.match.call(void 0, _types3.TokenType.num) || // get 1() {}
756 _index3.match.call(void 0, _types3.TokenType.bracketL) || // get ["string"]() {}
757 _index3.match.call(void 0, _types3.TokenType.name) || // get foo() {}
758 !!(_base.state.type & _types3.TokenType.IS_KEYWORD)) // get debugger() {}
759 );
760}
761
762// Returns true if this was a method.
763function parseObjectMethod(
764 isGenerator,
765 isPattern,
766 objectContextId,
767) {
768 // We don't need to worry about modifiers because object methods can't have optional bodies, so
769 // the start will never be used.
770 const functionStart = _base.state.start;
771 if (_index3.match.call(void 0, _types3.TokenType.parenL)) {
772 if (isPattern) _util.unexpected.call(void 0, );
773 parseMethod(functionStart, isGenerator, /* isConstructor */ false);
774 return true;
775 }
776
777 if (isGetterOrSetterMethod(isPattern)) {
778 parsePropertyName(objectContextId);
779 parseMethod(functionStart, /* isGenerator */ false, /* isConstructor */ false);
780 return true;
781 }
782 return false;
783}
784
785function parseObjectProperty(isPattern, isBlockScope) {
786 if (_index3.eat.call(void 0, _types3.TokenType.colon)) {
787 if (isPattern) {
788 _lval.parseMaybeDefault.call(void 0, isBlockScope);
789 } else {
790 parseMaybeAssign(false);
791 }
792 return;
793 }
794
795 // Since there's no colon, we assume this is an object shorthand.
796
797 // If we're in a destructuring, we've now discovered that the key was actually an assignee, so
798 // we need to tag it as a declaration with the appropriate scope. Otherwise, we might need to
799 // transform it on access, so mark it as a normal object shorthand.
800 if (isPattern) {
801 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = isBlockScope
802 ? _index3.IdentifierRole.ObjectShorthandBlockScopedDeclaration
803 : _index3.IdentifierRole.ObjectShorthandFunctionScopedDeclaration;
804 } else {
805 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _index3.IdentifierRole.ObjectShorthand;
806 }
807
808 // Regardless of whether we know this to be a pattern or if we're in an ambiguous context, allow
809 // parsing as if there's a default value.
810 _lval.parseMaybeDefault.call(void 0, isBlockScope, true);
811}
812
813function parseObjPropValue(
814 isGenerator,
815 isPattern,
816 isBlockScope,
817 objectContextId,
818) {
819 if (_base.isTypeScriptEnabled) {
820 _typescript.tsStartParseObjPropValue.call(void 0, );
821 } else if (_base.isFlowEnabled) {
822 _flow.flowStartParseObjPropValue.call(void 0, );
823 }
824 const wasMethod = parseObjectMethod(isGenerator, isPattern, objectContextId);
825 if (!wasMethod) {
826 parseObjectProperty(isPattern, isBlockScope);
827 }
828}
829
830 function parsePropertyName(objectContextId) {
831 if (_base.isFlowEnabled) {
832 _flow.flowParseVariance.call(void 0, );
833 }
834 if (_index3.eat.call(void 0, _types3.TokenType.bracketL)) {
835 _base.state.tokens[_base.state.tokens.length - 1].contextId = objectContextId;
836 parseMaybeAssign();
837 _util.expect.call(void 0, _types3.TokenType.bracketR);
838 _base.state.tokens[_base.state.tokens.length - 1].contextId = objectContextId;
839 } else {
840 if (_index3.match.call(void 0, _types3.TokenType.num) || _index3.match.call(void 0, _types3.TokenType.string)) {
841 parseExprAtom();
842 } else {
843 parseMaybePrivateName();
844 }
845
846 _base.state.tokens[_base.state.tokens.length - 1].identifierRole = _index3.IdentifierRole.ObjectKey;
847 _base.state.tokens[_base.state.tokens.length - 1].contextId = objectContextId;
848 }
849} exports.parsePropertyName = parsePropertyName;
850
851// Parse object or class method.
852 function parseMethod(
853 functionStart,
854 isGenerator,
855 isConstructor,
856) {
857 const funcContextId = _base.getNextContextId.call(void 0, );
858
859 _base.state.scopeDepth++;
860 const startTokenIndex = _base.state.tokens.length;
861 const allowModifiers = isConstructor; // For TypeScript parameter properties
862 _statement.parseFunctionParams.call(void 0, allowModifiers, funcContextId);
863 parseFunctionBodyAndFinish(
864 functionStart,
865 isGenerator,
866 false /* allowExpressionBody */,
867 funcContextId,
868 );
869 const endTokenIndex = _base.state.tokens.length;
870 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, true));
871 _base.state.scopeDepth--;
872} exports.parseMethod = parseMethod;
873
874// Parse arrow function expression.
875// If the parameters are provided, they will be converted to an
876// assignable list.
877 function parseArrowExpression(functionStart, startTokenIndex) {
878 parseFunctionBody(functionStart, false /* isGenerator */, true);
879 const endTokenIndex = _base.state.tokens.length;
880 _base.state.scopes.push(new (0, _state.Scope)(startTokenIndex, endTokenIndex, true));
881 _base.state.scopeDepth--;
882} exports.parseArrowExpression = parseArrowExpression;
883
884 function parseFunctionBodyAndFinish(
885 functionStart,
886 isGenerator,
887 allowExpressionBody = false,
888 funcContextId = 0,
889) {
890 if (_base.isTypeScriptEnabled) {
891 _typescript.tsParseFunctionBodyAndFinish.call(void 0, functionStart, isGenerator, allowExpressionBody, funcContextId);
892 } else if (_base.isFlowEnabled) {
893 _flow.flowParseFunctionBodyAndFinish.call(void 0, functionStart, isGenerator, allowExpressionBody, funcContextId);
894 } else {
895 parseFunctionBody(functionStart, isGenerator, allowExpressionBody, funcContextId);
896 }
897} exports.parseFunctionBodyAndFinish = parseFunctionBodyAndFinish;
898
899// Parse function body and check parameters.
900 function parseFunctionBody(
901 functionStart,
902 isGenerator,
903 allowExpression,
904 funcContextId = 0,
905) {
906 const isExpression = allowExpression && !_index3.match.call(void 0, _types3.TokenType.braceL);
907
908 if (isExpression) {
909 parseMaybeAssign();
910 } else {
911 _statement.parseBlock.call(void 0, true /* allowDirectives */, true /* isFunctionScope */, funcContextId);
912 }
913} exports.parseFunctionBody = parseFunctionBody;
914
915// Parses a comma-separated list of expressions, and returns them as
916// an array. `close` is the token type that ends the list, and
917// `allowEmpty` can be turned on to allow subsequent commas with
918// nothing in between them to be parsed as `null` (which is needed
919// for array literals).
920
921function parseExprList(close, allowEmpty = false) {
922 let first = true;
923 while (!_index3.eat.call(void 0, close) && !_base.state.error) {
924 if (first) {
925 first = false;
926 } else {
927 _util.expect.call(void 0, _types3.TokenType.comma);
928 if (_index3.eat.call(void 0, close)) break;
929 }
930 parseExprListItem(allowEmpty);
931 }
932}
933
934function parseExprListItem(allowEmpty) {
935 if (allowEmpty && _index3.match.call(void 0, _types3.TokenType.comma)) {
936 // Empty item; nothing more to parse for this item.
937 } else if (_index3.match.call(void 0, _types3.TokenType.ellipsis)) {
938 _lval.parseSpread.call(void 0, );
939 parseParenItem();
940 } else {
941 parseMaybeAssign(false, true);
942 }
943}
944
945// Parse the next token as an identifier.
946 function parseIdentifier() {
947 _index3.next.call(void 0, );
948 _base.state.tokens[_base.state.tokens.length - 1].type = _types3.TokenType.name;
949} exports.parseIdentifier = parseIdentifier;
950
951// Parses await expression inside async function.
952function parseAwait() {
953 parseMaybeUnary();
954}
955
956// Parses yield expression inside generator.
957function parseYield() {
958 _index3.next.call(void 0, );
959 if (!_index3.match.call(void 0, _types3.TokenType.semi) && !_util.canInsertSemicolon.call(void 0, )) {
960 _index3.eat.call(void 0, _types3.TokenType.star);
961 parseMaybeAssign();
962 }
963}