UNPKG

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