UNPKG

58.6 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _types = require("../tokenizer/types");
9
10var _context = require("../tokenizer/context");
11
12var N = _interopRequireWildcard(require("../types"));
13
14var _lval = _interopRequireDefault(require("./lval"));
15
16var _identifier = require("../util/identifier");
17
18var _scopeflags = require("../util/scopeflags");
19
20var _util = require("./util");
21
22var _productionParameter = require("../util/production-parameter");
23
24var _expressionScope = require("../util/expression-scope.js");
25
26var _error = require("./error");
27
28function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
30function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
31
32function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
33
34class ExpressionParser extends _lval.default {
35 checkProto(prop, isRecord, protoRef, refExpressionErrors) {
36 if (prop.type === "SpreadElement" || prop.type === "ObjectMethod" || prop.computed || prop.shorthand) {
37 return;
38 }
39
40 const key = prop.key;
41 const name = key.type === "Identifier" ? key.name : key.value;
42
43 if (name === "__proto__") {
44 if (isRecord) {
45 this.raise(key.start, _error.Errors.RecordNoProto);
46 return;
47 }
48
49 if (protoRef.used) {
50 if (refExpressionErrors) {
51 if (refExpressionErrors.doubleProto === -1) {
52 refExpressionErrors.doubleProto = key.start;
53 }
54 } else {
55 this.raise(key.start, _error.Errors.DuplicateProto);
56 }
57 }
58
59 protoRef.used = true;
60 }
61 }
62
63 shouldExitDescending(expr, potentialArrowAt) {
64 return expr.type === "ArrowFunctionExpression" && expr.start === potentialArrowAt;
65 }
66
67 getExpression() {
68 let paramFlags = _productionParameter.PARAM;
69
70 if (this.hasPlugin("topLevelAwait") && this.inModule) {
71 paramFlags |= _productionParameter.PARAM_AWAIT;
72 }
73
74 this.scope.enter(_scopeflags.SCOPE_PROGRAM);
75 this.prodParam.enter(paramFlags);
76 this.nextToken();
77 const expr = this.parseExpression();
78
79 if (!this.match(_types.types.eof)) {
80 this.unexpected();
81 }
82
83 expr.comments = this.state.comments;
84 expr.errors = this.state.errors;
85 return expr;
86 }
87
88 parseExpression(disallowIn, refExpressionErrors) {
89 if (disallowIn) {
90 return this.disallowInAnd(() => this.parseExpressionBase(refExpressionErrors));
91 }
92
93 return this.allowInAnd(() => this.parseExpressionBase(refExpressionErrors));
94 }
95
96 parseExpressionBase(refExpressionErrors) {
97 const startPos = this.state.start;
98 const startLoc = this.state.startLoc;
99 const expr = this.parseMaybeAssign(refExpressionErrors);
100
101 if (this.match(_types.types.comma)) {
102 const node = this.startNodeAt(startPos, startLoc);
103 node.expressions = [expr];
104
105 while (this.eat(_types.types.comma)) {
106 node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
107 }
108
109 this.toReferencedList(node.expressions);
110 return this.finishNode(node, "SequenceExpression");
111 }
112
113 return expr;
114 }
115
116 parseMaybeAssignDisallowIn(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
117 return this.disallowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos));
118 }
119
120 parseMaybeAssignAllowIn(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
121 return this.allowInAnd(() => this.parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos));
122 }
123
124 parseMaybeAssign(refExpressionErrors, afterLeftParse, refNeedsArrowPos) {
125 const startPos = this.state.start;
126 const startLoc = this.state.startLoc;
127
128 if (this.isContextual("yield")) {
129 if (this.prodParam.hasYield) {
130 this.state.exprAllowed = true;
131 let left = this.parseYield();
132
133 if (afterLeftParse) {
134 left = afterLeftParse.call(this, left, startPos, startLoc);
135 }
136
137 return left;
138 }
139 }
140
141 let ownExpressionErrors;
142
143 if (refExpressionErrors) {
144 ownExpressionErrors = false;
145 } else {
146 refExpressionErrors = new _util.ExpressionErrors();
147 ownExpressionErrors = true;
148 }
149
150 if (this.match(_types.types.parenL) || this.match(_types.types.name)) {
151 this.state.potentialArrowAt = this.state.start;
152 }
153
154 let left = this.parseMaybeConditional(refExpressionErrors, refNeedsArrowPos);
155
156 if (afterLeftParse) {
157 left = afterLeftParse.call(this, left, startPos, startLoc);
158 }
159
160 if (this.state.type.isAssign) {
161 const node = this.startNodeAt(startPos, startLoc);
162 const operator = this.state.value;
163 node.operator = operator;
164
165 if (this.match(_types.types.eq)) {
166 node.left = this.toAssignable(left);
167 refExpressionErrors.doubleProto = -1;
168 } else {
169 node.left = left;
170 }
171
172 if (refExpressionErrors.shorthandAssign >= node.left.start) {
173 refExpressionErrors.shorthandAssign = -1;
174 }
175
176 this.checkLVal(left, undefined, undefined, "assignment expression");
177 this.next();
178 node.right = this.parseMaybeAssign();
179 return this.finishNode(node, "AssignmentExpression");
180 } else if (ownExpressionErrors) {
181 this.checkExpressionErrors(refExpressionErrors, true);
182 }
183
184 return left;
185 }
186
187 parseMaybeConditional(refExpressionErrors, refNeedsArrowPos) {
188 const startPos = this.state.start;
189 const startLoc = this.state.startLoc;
190 const potentialArrowAt = this.state.potentialArrowAt;
191 const expr = this.parseExprOps(refExpressionErrors);
192
193 if (this.shouldExitDescending(expr, potentialArrowAt)) {
194 return expr;
195 }
196
197 return this.parseConditional(expr, startPos, startLoc, refNeedsArrowPos);
198 }
199
200 parseConditional(expr, startPos, startLoc, refNeedsArrowPos) {
201 if (this.eat(_types.types.question)) {
202 const node = this.startNodeAt(startPos, startLoc);
203 node.test = expr;
204 node.consequent = this.parseMaybeAssignAllowIn();
205 this.expect(_types.types.colon);
206 node.alternate = this.parseMaybeAssign();
207 return this.finishNode(node, "ConditionalExpression");
208 }
209
210 return expr;
211 }
212
213 parseExprOps(refExpressionErrors) {
214 const startPos = this.state.start;
215 const startLoc = this.state.startLoc;
216 const potentialArrowAt = this.state.potentialArrowAt;
217 const expr = this.parseMaybeUnary(refExpressionErrors);
218
219 if (this.shouldExitDescending(expr, potentialArrowAt)) {
220 return expr;
221 }
222
223 return this.parseExprOp(expr, startPos, startLoc, -1);
224 }
225
226 parseExprOp(left, leftStartPos, leftStartLoc, minPrec) {
227 let prec = this.state.type.binop;
228
229 if (prec != null && (this.prodParam.hasIn || !this.match(_types.types._in))) {
230 if (prec > minPrec) {
231 const op = this.state.type;
232
233 if (op === _types.types.pipeline) {
234 this.expectPlugin("pipelineOperator");
235
236 if (this.state.inFSharpPipelineDirectBody) {
237 return left;
238 }
239
240 this.state.inPipeline = true;
241 this.checkPipelineAtInfixOperator(left, leftStartPos);
242 }
243
244 const node = this.startNodeAt(leftStartPos, leftStartLoc);
245 node.left = left;
246 node.operator = this.state.value;
247
248 if (op === _types.types.exponent && left.type === "UnaryExpression" && (this.options.createParenthesizedExpressions || !(left.extra && left.extra.parenthesized))) {
249 this.raise(left.argument.start, _error.Errors.UnexpectedTokenUnaryExponentiation);
250 }
251
252 const logical = op === _types.types.logicalOR || op === _types.types.logicalAND;
253 const coalesce = op === _types.types.nullishCoalescing;
254
255 if (coalesce) {
256 prec = _types.types.logicalAND.binop;
257 }
258
259 this.next();
260
261 if (op === _types.types.pipeline && this.getPluginOption("pipelineOperator", "proposal") === "minimal") {
262 if (this.match(_types.types.name) && this.state.value === "await" && this.prodParam.hasAwait) {
263 throw this.raise(this.state.start, _error.Errors.UnexpectedAwaitAfterPipelineBody);
264 }
265 }
266
267 node.right = this.parseExprOpRightExpr(op, prec);
268 this.finishNode(node, logical || coalesce ? "LogicalExpression" : "BinaryExpression");
269 const nextOp = this.state.type;
270
271 if (coalesce && (nextOp === _types.types.logicalOR || nextOp === _types.types.logicalAND) || logical && nextOp === _types.types.nullishCoalescing) {
272 throw this.raise(this.state.start, _error.Errors.MixingCoalesceWithLogical);
273 }
274
275 return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec);
276 }
277 }
278
279 return left;
280 }
281
282 parseExprOpRightExpr(op, prec) {
283 const startPos = this.state.start;
284 const startLoc = this.state.startLoc;
285
286 switch (op) {
287 case _types.types.pipeline:
288 switch (this.getPluginOption("pipelineOperator", "proposal")) {
289 case "smart":
290 return this.withTopicPermittingContext(() => {
291 return this.parseSmartPipelineBody(this.parseExprOpBaseRightExpr(op, prec), startPos, startLoc);
292 });
293
294 case "fsharp":
295 return this.withSoloAwaitPermittingContext(() => {
296 return this.parseFSharpPipelineBody(prec);
297 });
298 }
299
300 default:
301 return this.parseExprOpBaseRightExpr(op, prec);
302 }
303 }
304
305 parseExprOpBaseRightExpr(op, prec) {
306 const startPos = this.state.start;
307 const startLoc = this.state.startLoc;
308 return this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec);
309 }
310
311 parseMaybeUnary(refExpressionErrors) {
312 if (this.isContextual("await") && this.isAwaitAllowed()) {
313 return this.parseAwait();
314 }
315
316 const update = this.match(_types.types.incDec);
317 const node = this.startNode();
318
319 if (this.state.type.prefix) {
320 node.operator = this.state.value;
321 node.prefix = true;
322
323 if (this.match(_types.types._throw)) {
324 this.expectPlugin("throwExpressions");
325 }
326
327 const isDelete = this.match(_types.types._delete);
328 this.next();
329 node.argument = this.parseMaybeUnary();
330 this.checkExpressionErrors(refExpressionErrors, true);
331
332 if (this.state.strict && isDelete) {
333 const arg = node.argument;
334
335 if (arg.type === "Identifier") {
336 this.raise(node.start, _error.Errors.StrictDelete);
337 } else if ((arg.type === "MemberExpression" || arg.type === "OptionalMemberExpression") && arg.property.type === "PrivateName") {
338 this.raise(node.start, _error.Errors.DeletePrivateField);
339 }
340 }
341
342 if (!update) {
343 return this.finishNode(node, "UnaryExpression");
344 }
345 }
346
347 return this.parseUpdate(node, update, refExpressionErrors);
348 }
349
350 parseUpdate(node, update, refExpressionErrors) {
351 if (update) {
352 this.checkLVal(node.argument, undefined, undefined, "prefix operation");
353 return this.finishNode(node, "UpdateExpression");
354 }
355
356 const startPos = this.state.start;
357 const startLoc = this.state.startLoc;
358 let expr = this.parseExprSubscripts(refExpressionErrors);
359 if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
360
361 while (this.state.type.postfix && !this.canInsertSemicolon()) {
362 const node = this.startNodeAt(startPos, startLoc);
363 node.operator = this.state.value;
364 node.prefix = false;
365 node.argument = expr;
366 this.checkLVal(expr, undefined, undefined, "postfix operation");
367 this.next();
368 expr = this.finishNode(node, "UpdateExpression");
369 }
370
371 return expr;
372 }
373
374 parseExprSubscripts(refExpressionErrors) {
375 const startPos = this.state.start;
376 const startLoc = this.state.startLoc;
377 const potentialArrowAt = this.state.potentialArrowAt;
378 const expr = this.parseExprAtom(refExpressionErrors);
379
380 if (this.shouldExitDescending(expr, potentialArrowAt)) {
381 return expr;
382 }
383
384 return this.parseSubscripts(expr, startPos, startLoc);
385 }
386
387 parseSubscripts(base, startPos, startLoc, noCalls) {
388 const state = {
389 optionalChainMember: false,
390 maybeAsyncArrow: this.atPossibleAsyncArrow(base),
391 stop: false
392 };
393
394 do {
395 base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
396 state.maybeAsyncArrow = false;
397 } while (!state.stop);
398
399 return base;
400 }
401
402 parseSubscript(base, startPos, startLoc, noCalls, state) {
403 if (!noCalls && this.eat(_types.types.doubleColon)) {
404 return this.parseBind(base, startPos, startLoc, noCalls, state);
405 } else if (this.match(_types.types.backQuote)) {
406 return this.parseTaggedTemplateExpression(base, startPos, startLoc, state);
407 }
408
409 let optional = false;
410
411 if (this.match(_types.types.questionDot)) {
412 state.optionalChainMember = optional = true;
413
414 if (noCalls && this.lookaheadCharCode() === 40) {
415 state.stop = true;
416 return base;
417 }
418
419 this.next();
420 }
421
422 if (!noCalls && this.match(_types.types.parenL)) {
423 return this.parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional);
424 } else if (optional || this.match(_types.types.bracketL) || this.eat(_types.types.dot)) {
425 return this.parseMember(base, startPos, startLoc, state, optional);
426 } else {
427 state.stop = true;
428 return base;
429 }
430 }
431
432 parseMember(base, startPos, startLoc, state, optional) {
433 const node = this.startNodeAt(startPos, startLoc);
434 const computed = this.eat(_types.types.bracketL);
435 node.object = base;
436 node.computed = computed;
437 const property = computed ? this.parseExpression() : this.parseMaybePrivateName(true);
438
439 if (property.type === "PrivateName") {
440 if (node.object.type === "Super") {
441 this.raise(startPos, _error.Errors.SuperPrivateField);
442 }
443
444 this.classScope.usePrivateName(property.id.name, property.start);
445 }
446
447 node.property = property;
448
449 if (computed) {
450 this.expect(_types.types.bracketR);
451 }
452
453 if (state.optionalChainMember) {
454 node.optional = optional;
455 return this.finishNode(node, "OptionalMemberExpression");
456 } else {
457 return this.finishNode(node, "MemberExpression");
458 }
459 }
460
461 parseBind(base, startPos, startLoc, noCalls, state) {
462 const node = this.startNodeAt(startPos, startLoc);
463 node.object = base;
464 node.callee = this.parseNoCallExpr();
465 state.stop = true;
466 return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
467 }
468
469 parseCoverCallAndAsyncArrowHead(base, startPos, startLoc, state, optional) {
470 const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
471 this.state.maybeInArrowParameters = true;
472 this.next();
473 let node = this.startNodeAt(startPos, startLoc);
474 node.callee = base;
475
476 if (state.maybeAsyncArrow) {
477 this.expressionScope.enter((0, _expressionScope.newAsyncArrowScope)());
478 }
479
480 if (state.optionalChainMember) {
481 node.optional = optional;
482 }
483
484 if (optional) {
485 node.arguments = this.parseCallExpressionArguments(_types.types.parenR, false);
486 } else {
487 node.arguments = this.parseCallExpressionArguments(_types.types.parenR, state.maybeAsyncArrow, base.type === "Import", base.type !== "Super", node);
488 }
489
490 this.finishCallExpression(node, state.optionalChainMember);
491
492 if (state.maybeAsyncArrow && this.shouldParseAsyncArrow() && !optional) {
493 state.stop = true;
494 this.expressionScope.validateAsPattern();
495 this.expressionScope.exit();
496 node = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node);
497 } else {
498 if (state.maybeAsyncArrow) {
499 this.expressionScope.exit();
500 }
501
502 this.toReferencedArguments(node);
503 }
504
505 this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
506 return node;
507 }
508
509 toReferencedArguments(node, isParenthesizedExpr) {
510 this.toReferencedListDeep(node.arguments, isParenthesizedExpr);
511 }
512
513 parseTaggedTemplateExpression(base, startPos, startLoc, state) {
514 const node = this.startNodeAt(startPos, startLoc);
515 node.tag = base;
516 node.quasi = this.parseTemplate(true);
517
518 if (state.optionalChainMember) {
519 this.raise(startPos, _error.Errors.OptionalChainingNoTemplate);
520 }
521
522 return this.finishNode(node, "TaggedTemplateExpression");
523 }
524
525 atPossibleAsyncArrow(base) {
526 return base.type === "Identifier" && base.name === "async" && this.state.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.state.potentialArrowAt;
527 }
528
529 finishCallExpression(node, optional) {
530 if (node.callee.type === "Import") {
531 if (node.arguments.length === 2) {
532 if (!this.hasPlugin("moduleAttributes")) {
533 this.expectPlugin("importAssertions");
534 }
535 }
536
537 if (node.arguments.length === 0 || node.arguments.length > 2) {
538 this.raise(node.start, _error.Errors.ImportCallArity, this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? "one or two arguments" : "one argument");
539 } else {
540 for (let _i = 0, _node$arguments = node.arguments; _i < _node$arguments.length; _i++) {
541 const arg = _node$arguments[_i];
542
543 if (arg.type === "SpreadElement") {
544 this.raise(arg.start, _error.Errors.ImportCallSpreadArgument);
545 }
546 }
547 }
548 }
549
550 return this.finishNode(node, optional ? "OptionalCallExpression" : "CallExpression");
551 }
552
553 parseCallExpressionArguments(close, possibleAsyncArrow, dynamicImport, allowPlaceholder, nodeForExtra) {
554 const elts = [];
555 let innerParenStart;
556 let first = true;
557 const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
558 this.state.inFSharpPipelineDirectBody = false;
559
560 while (!this.eat(close)) {
561 if (first) {
562 first = false;
563 } else {
564 this.expect(_types.types.comma);
565
566 if (this.match(close)) {
567 if (dynamicImport && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes")) {
568 this.raise(this.state.lastTokStart, _error.Errors.ImportCallArgumentTrailingComma);
569 }
570
571 if (nodeForExtra) {
572 this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
573 }
574
575 this.next();
576 break;
577 }
578 }
579
580 if (this.match(_types.types.parenL) && !innerParenStart) {
581 innerParenStart = this.state.start;
582 }
583
584 elts.push(this.parseExprListItem(false, possibleAsyncArrow ? new _util.ExpressionErrors() : undefined, possibleAsyncArrow ? {
585 start: 0
586 } : undefined, allowPlaceholder));
587 }
588
589 if (possibleAsyncArrow && innerParenStart && this.shouldParseAsyncArrow()) {
590 this.unexpected();
591 }
592
593 this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
594 return elts;
595 }
596
597 shouldParseAsyncArrow() {
598 return this.match(_types.types.arrow) && !this.canInsertSemicolon();
599 }
600
601 parseAsyncArrowFromCallExpression(node, call) {
602 this.expect(_types.types.arrow);
603 this.parseArrowExpression(node, call.arguments, true, call.extra?.trailingComma);
604 return node;
605 }
606
607 parseNoCallExpr() {
608 const startPos = this.state.start;
609 const startLoc = this.state.startLoc;
610 return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true);
611 }
612
613 parseExprAtom(refExpressionErrors) {
614 if (this.state.type === _types.types.slash) this.readRegexp();
615 const canBeArrow = this.state.potentialArrowAt === this.state.start;
616 let node;
617
618 switch (this.state.type) {
619 case _types.types._super:
620 return this.parseSuper();
621
622 case _types.types._import:
623 node = this.startNode();
624 this.next();
625
626 if (this.match(_types.types.dot)) {
627 return this.parseImportMetaProperty(node);
628 }
629
630 if (!this.match(_types.types.parenL)) {
631 this.raise(this.state.lastTokStart, _error.Errors.UnsupportedImport);
632 }
633
634 return this.finishNode(node, "Import");
635
636 case _types.types._this:
637 node = this.startNode();
638 this.next();
639 return this.finishNode(node, "ThisExpression");
640
641 case _types.types.name:
642 {
643 const containsEsc = this.state.containsEsc;
644 const id = this.parseIdentifier();
645
646 if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
647 if (this.match(_types.types._function)) {
648 const last = this.state.context.length - 1;
649
650 if (this.state.context[last] !== _context.types.functionStatement) {
651 throw new Error("Internal error");
652 }
653
654 this.state.context[last] = _context.types.functionExpression;
655 this.next();
656 return this.parseFunction(this.startNodeAtNode(id), undefined, true);
657 } else if (this.match(_types.types.name)) {
658 return this.parseAsyncArrowUnaryFunction(id);
659 }
660 }
661
662 if (canBeArrow && this.match(_types.types.arrow) && !this.canInsertSemicolon()) {
663 this.next();
664 return this.parseArrowExpression(this.startNodeAtNode(id), [id], false);
665 }
666
667 return id;
668 }
669
670 case _types.types._do:
671 {
672 return this.parseDo();
673 }
674
675 case _types.types.regexp:
676 {
677 const value = this.state.value;
678 node = this.parseLiteral(value.value, "RegExpLiteral");
679 node.pattern = value.pattern;
680 node.flags = value.flags;
681 return node;
682 }
683
684 case _types.types.num:
685 return this.parseLiteral(this.state.value, "NumericLiteral");
686
687 case _types.types.bigint:
688 return this.parseLiteral(this.state.value, "BigIntLiteral");
689
690 case _types.types.decimal:
691 return this.parseLiteral(this.state.value, "DecimalLiteral");
692
693 case _types.types.string:
694 return this.parseLiteral(this.state.value, "StringLiteral");
695
696 case _types.types._null:
697 node = this.startNode();
698 this.next();
699 return this.finishNode(node, "NullLiteral");
700
701 case _types.types._true:
702 case _types.types._false:
703 return this.parseBooleanLiteral();
704
705 case _types.types.parenL:
706 return this.parseParenAndDistinguishExpression(canBeArrow);
707
708 case _types.types.bracketBarL:
709 case _types.types.bracketHashL:
710 {
711 return this.parseArrayLike(this.state.type === _types.types.bracketBarL ? _types.types.bracketBarR : _types.types.bracketR, false, true, refExpressionErrors);
712 }
713
714 case _types.types.bracketL:
715 {
716 return this.parseArrayLike(_types.types.bracketR, true, false, refExpressionErrors);
717 }
718
719 case _types.types.braceBarL:
720 case _types.types.braceHashL:
721 {
722 return this.parseObjectLike(this.state.type === _types.types.braceBarL ? _types.types.braceBarR : _types.types.braceR, false, true, refExpressionErrors);
723 }
724
725 case _types.types.braceL:
726 {
727 return this.parseObjectLike(_types.types.braceR, false, false, refExpressionErrors);
728 }
729
730 case _types.types._function:
731 return this.parseFunctionOrFunctionSent();
732
733 case _types.types.at:
734 this.parseDecorators();
735
736 case _types.types._class:
737 node = this.startNode();
738 this.takeDecorators(node);
739 return this.parseClass(node, false);
740
741 case _types.types._new:
742 return this.parseNewOrNewTarget();
743
744 case _types.types.backQuote:
745 return this.parseTemplate(false);
746
747 case _types.types.doubleColon:
748 {
749 node = this.startNode();
750 this.next();
751 node.object = null;
752 const callee = node.callee = this.parseNoCallExpr();
753
754 if (callee.type === "MemberExpression") {
755 return this.finishNode(node, "BindExpression");
756 } else {
757 throw this.raise(callee.start, _error.Errors.UnsupportedBind);
758 }
759 }
760
761 case _types.types.hash:
762 {
763 if (this.state.inPipeline) {
764 node = this.startNode();
765
766 if (this.getPluginOption("pipelineOperator", "proposal") !== "smart") {
767 this.raise(node.start, _error.Errors.PrimaryTopicRequiresSmartPipeline);
768 }
769
770 this.next();
771
772 if (!this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
773 this.raise(node.start, _error.Errors.PrimaryTopicNotAllowed);
774 }
775
776 this.registerTopicReference();
777 return this.finishNode(node, "PipelinePrimaryTopicReference");
778 }
779
780 const nextCh = this.input.codePointAt(this.state.end);
781
782 if ((0, _identifier.isIdentifierStart)(nextCh) || nextCh === 92) {
783 const start = this.state.start;
784 node = this.parseMaybePrivateName(true);
785
786 if (this.match(_types.types._in)) {
787 this.expectPlugin("privateIn");
788 this.classScope.usePrivateName(node.id.name, node.start);
789 } else if (this.hasPlugin("privateIn")) {
790 this.raise(this.state.start, _error.Errors.PrivateInExpectedIn, node.id.name);
791 } else {
792 throw this.unexpected(start);
793 }
794
795 return node;
796 }
797 }
798
799 case _types.types.relational:
800 {
801 if (this.state.value === "<") {
802 const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
803
804 if ((0, _identifier.isIdentifierStart)(lookaheadCh) || lookaheadCh === 62) {
805 this.expectOnePlugin(["jsx", "flow", "typescript"]);
806 }
807 }
808 }
809
810 default:
811 throw this.unexpected();
812 }
813 }
814
815 parseAsyncArrowUnaryFunction(id) {
816 const node = this.startNodeAtNode(id);
817 this.prodParam.enter((0, _productionParameter.functionFlags)(true, this.prodParam.hasYield));
818 const params = [this.parseIdentifier()];
819 this.prodParam.exit();
820
821 if (this.hasPrecedingLineBreak()) {
822 this.raise(this.state.pos, _error.Errors.LineTerminatorBeforeArrow);
823 }
824
825 this.expect(_types.types.arrow);
826 this.parseArrowExpression(node, params, true);
827 return node;
828 }
829
830 parseDo() {
831 this.expectPlugin("doExpressions");
832 const node = this.startNode();
833 this.next();
834 const oldLabels = this.state.labels;
835 this.state.labels = [];
836 node.body = this.parseBlock();
837 this.state.labels = oldLabels;
838 return this.finishNode(node, "DoExpression");
839 }
840
841 parseSuper() {
842 const node = this.startNode();
843 this.next();
844
845 if (this.match(_types.types.parenL) && !this.scope.allowDirectSuper && !this.options.allowSuperOutsideMethod) {
846 this.raise(node.start, _error.Errors.SuperNotAllowed);
847 } else if (!this.scope.allowSuper && !this.options.allowSuperOutsideMethod) {
848 this.raise(node.start, _error.Errors.UnexpectedSuper);
849 }
850
851 if (!this.match(_types.types.parenL) && !this.match(_types.types.bracketL) && !this.match(_types.types.dot)) {
852 this.raise(node.start, _error.Errors.UnsupportedSuper);
853 }
854
855 return this.finishNode(node, "Super");
856 }
857
858 parseBooleanLiteral() {
859 const node = this.startNode();
860 node.value = this.match(_types.types._true);
861 this.next();
862 return this.finishNode(node, "BooleanLiteral");
863 }
864
865 parseMaybePrivateName(isPrivateNameAllowed) {
866 const isPrivate = this.match(_types.types.hash);
867
868 if (isPrivate) {
869 this.expectOnePlugin(["classPrivateProperties", "classPrivateMethods"]);
870
871 if (!isPrivateNameAllowed) {
872 this.raise(this.state.pos, _error.Errors.UnexpectedPrivateField);
873 }
874
875 const node = this.startNode();
876 this.next();
877 this.assertNoSpace("Unexpected space between # and identifier");
878 node.id = this.parseIdentifier(true);
879 return this.finishNode(node, "PrivateName");
880 } else {
881 return this.parseIdentifier(true);
882 }
883 }
884
885 parseFunctionOrFunctionSent() {
886 const node = this.startNode();
887 this.next();
888
889 if (this.prodParam.hasYield && this.match(_types.types.dot)) {
890 const meta = this.createIdentifier(this.startNodeAtNode(node), "function");
891 this.next();
892 return this.parseMetaProperty(node, meta, "sent");
893 }
894
895 return this.parseFunction(node);
896 }
897
898 parseMetaProperty(node, meta, propertyName) {
899 node.meta = meta;
900
901 if (meta.name === "function" && propertyName === "sent") {
902 if (this.isContextual(propertyName)) {
903 this.expectPlugin("functionSent");
904 } else if (!this.hasPlugin("functionSent")) {
905 this.unexpected();
906 }
907 }
908
909 const containsEsc = this.state.containsEsc;
910 node.property = this.parseIdentifier(true);
911
912 if (node.property.name !== propertyName || containsEsc) {
913 this.raise(node.property.start, _error.Errors.UnsupportedMetaProperty, meta.name, propertyName);
914 }
915
916 return this.finishNode(node, "MetaProperty");
917 }
918
919 parseImportMetaProperty(node) {
920 const id = this.createIdentifier(this.startNodeAtNode(node), "import");
921 this.next();
922
923 if (this.isContextual("meta")) {
924 if (!this.inModule) {
925 this.raiseWithData(id.start, {
926 code: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED"
927 }, _error.Errors.ImportMetaOutsideModule);
928 }
929
930 this.sawUnambiguousESM = true;
931 }
932
933 return this.parseMetaProperty(node, id, "meta");
934 }
935
936 parseLiteral(value, type, startPos, startLoc) {
937 startPos = startPos || this.state.start;
938 startLoc = startLoc || this.state.startLoc;
939 const node = this.startNodeAt(startPos, startLoc);
940 this.addExtra(node, "rawValue", value);
941 this.addExtra(node, "raw", this.input.slice(startPos, this.state.end));
942 node.value = value;
943 this.next();
944 return this.finishNode(node, type);
945 }
946
947 parseParenAndDistinguishExpression(canBeArrow) {
948 const startPos = this.state.start;
949 const startLoc = this.state.startLoc;
950 let val;
951 this.next();
952 this.expressionScope.enter((0, _expressionScope.newArrowHeadScope)());
953 const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
954 const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
955 this.state.maybeInArrowParameters = true;
956 this.state.inFSharpPipelineDirectBody = false;
957 const innerStartPos = this.state.start;
958 const innerStartLoc = this.state.startLoc;
959 const exprList = [];
960 const refExpressionErrors = new _util.ExpressionErrors();
961 const refNeedsArrowPos = {
962 start: 0
963 };
964 let first = true;
965 let spreadStart;
966 let optionalCommaStart;
967
968 while (!this.match(_types.types.parenR)) {
969 if (first) {
970 first = false;
971 } else {
972 this.expect(_types.types.comma, refNeedsArrowPos.start || null);
973
974 if (this.match(_types.types.parenR)) {
975 optionalCommaStart = this.state.start;
976 break;
977 }
978 }
979
980 if (this.match(_types.types.ellipsis)) {
981 const spreadNodeStartPos = this.state.start;
982 const spreadNodeStartLoc = this.state.startLoc;
983 spreadStart = this.state.start;
984 exprList.push(this.parseParenItem(this.parseRestBinding(), spreadNodeStartPos, spreadNodeStartLoc));
985 this.checkCommaAfterRest(41);
986 break;
987 } else {
988 exprList.push(this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem, refNeedsArrowPos));
989 }
990 }
991
992 const innerEndPos = this.state.lastTokEnd;
993 const innerEndLoc = this.state.lastTokEndLoc;
994 this.expect(_types.types.parenR);
995 this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
996 this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
997 let arrowNode = this.startNodeAt(startPos, startLoc);
998
999 if (canBeArrow && this.shouldParseArrow() && (arrowNode = this.parseArrow(arrowNode))) {
1000 this.expressionScope.validateAsPattern();
1001 this.expressionScope.exit();
1002
1003 for (let _i2 = 0; _i2 < exprList.length; _i2++) {
1004 const param = exprList[_i2];
1005
1006 if (param.extra && param.extra.parenthesized) {
1007 this.unexpected(param.extra.parenStart);
1008 }
1009 }
1010
1011 this.parseArrowExpression(arrowNode, exprList, false);
1012 return arrowNode;
1013 }
1014
1015 this.expressionScope.exit();
1016
1017 if (!exprList.length) {
1018 this.unexpected(this.state.lastTokStart);
1019 }
1020
1021 if (optionalCommaStart) this.unexpected(optionalCommaStart);
1022 if (spreadStart) this.unexpected(spreadStart);
1023 this.checkExpressionErrors(refExpressionErrors, true);
1024 if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
1025 this.toReferencedListDeep(exprList, true);
1026
1027 if (exprList.length > 1) {
1028 val = this.startNodeAt(innerStartPos, innerStartLoc);
1029 val.expressions = exprList;
1030 this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
1031 } else {
1032 val = exprList[0];
1033 }
1034
1035 if (!this.options.createParenthesizedExpressions) {
1036 this.addExtra(val, "parenthesized", true);
1037 this.addExtra(val, "parenStart", startPos);
1038 return val;
1039 }
1040
1041 const parenExpression = this.startNodeAt(startPos, startLoc);
1042 parenExpression.expression = val;
1043 this.finishNode(parenExpression, "ParenthesizedExpression");
1044 return parenExpression;
1045 }
1046
1047 shouldParseArrow() {
1048 return !this.canInsertSemicolon();
1049 }
1050
1051 parseArrow(node) {
1052 if (this.eat(_types.types.arrow)) {
1053 return node;
1054 }
1055 }
1056
1057 parseParenItem(node, startPos, startLoc) {
1058 return node;
1059 }
1060
1061 parseNewOrNewTarget() {
1062 const node = this.startNode();
1063 this.next();
1064
1065 if (this.match(_types.types.dot)) {
1066 const meta = this.createIdentifier(this.startNodeAtNode(node), "new");
1067 this.next();
1068 const metaProp = this.parseMetaProperty(node, meta, "target");
1069
1070 if (!this.scope.inNonArrowFunction && !this.scope.inClass) {
1071 let error = _error.Errors.UnexpectedNewTarget;
1072
1073 if (this.hasPlugin("classProperties")) {
1074 error += " or class properties";
1075 }
1076
1077 this.raise(metaProp.start, error);
1078 }
1079
1080 return metaProp;
1081 }
1082
1083 return this.parseNew(node);
1084 }
1085
1086 parseNew(node) {
1087 node.callee = this.parseNoCallExpr();
1088
1089 if (node.callee.type === "Import") {
1090 this.raise(node.callee.start, _error.Errors.ImportCallNotNewExpression);
1091 } else if (node.callee.type === "OptionalMemberExpression" || node.callee.type === "OptionalCallExpression") {
1092 this.raise(this.state.lastTokEnd, _error.Errors.OptionalChainingNoNew);
1093 } else if (this.eat(_types.types.questionDot)) {
1094 this.raise(this.state.start, _error.Errors.OptionalChainingNoNew);
1095 }
1096
1097 this.parseNewArguments(node);
1098 return this.finishNode(node, "NewExpression");
1099 }
1100
1101 parseNewArguments(node) {
1102 if (this.eat(_types.types.parenL)) {
1103 const args = this.parseExprList(_types.types.parenR);
1104 this.toReferencedList(args);
1105 node.arguments = args;
1106 } else {
1107 node.arguments = [];
1108 }
1109 }
1110
1111 parseTemplateElement(isTagged) {
1112 const elem = this.startNode();
1113
1114 if (this.state.value === null) {
1115 if (!isTagged) {
1116 this.raise(this.state.start + 1, _error.Errors.InvalidEscapeSequenceTemplate);
1117 }
1118 }
1119
1120 elem.value = {
1121 raw: this.input.slice(this.state.start, this.state.end).replace(/\r\n?/g, "\n"),
1122 cooked: this.state.value
1123 };
1124 this.next();
1125 elem.tail = this.match(_types.types.backQuote);
1126 return this.finishNode(elem, "TemplateElement");
1127 }
1128
1129 parseTemplate(isTagged) {
1130 const node = this.startNode();
1131 this.next();
1132 node.expressions = [];
1133 let curElt = this.parseTemplateElement(isTagged);
1134 node.quasis = [curElt];
1135
1136 while (!curElt.tail) {
1137 this.expect(_types.types.dollarBraceL);
1138 node.expressions.push(this.parseTemplateSubstitution());
1139 this.expect(_types.types.braceR);
1140 node.quasis.push(curElt = this.parseTemplateElement(isTagged));
1141 }
1142
1143 this.next();
1144 return this.finishNode(node, "TemplateLiteral");
1145 }
1146
1147 parseTemplateSubstitution() {
1148 return this.parseExpression();
1149 }
1150
1151 parseObjectLike(close, isPattern, isRecord, refExpressionErrors) {
1152 if (isRecord) {
1153 this.expectPlugin("recordAndTuple");
1154 }
1155
1156 const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
1157 this.state.inFSharpPipelineDirectBody = false;
1158 const propHash = Object.create(null);
1159 let first = true;
1160 const node = this.startNode();
1161 node.properties = [];
1162 this.next();
1163
1164 while (!this.match(close)) {
1165 if (first) {
1166 first = false;
1167 } else {
1168 this.expect(_types.types.comma);
1169
1170 if (this.match(close)) {
1171 this.addExtra(node, "trailingComma", this.state.lastTokStart);
1172 break;
1173 }
1174 }
1175
1176 const prop = this.parsePropertyDefinition(isPattern, refExpressionErrors);
1177
1178 if (!isPattern) {
1179 this.checkProto(prop, isRecord, propHash, refExpressionErrors);
1180 }
1181
1182 if (isRecord && prop.type !== "ObjectProperty" && prop.type !== "SpreadElement") {
1183 this.raise(prop.start, _error.Errors.InvalidRecordProperty);
1184 }
1185
1186 if (prop.shorthand) {
1187 this.addExtra(prop, "shorthand", true);
1188 }
1189
1190 node.properties.push(prop);
1191 }
1192
1193 this.state.exprAllowed = false;
1194 this.next();
1195 this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
1196 let type = "ObjectExpression";
1197
1198 if (isPattern) {
1199 type = "ObjectPattern";
1200 } else if (isRecord) {
1201 type = "RecordExpression";
1202 }
1203
1204 return this.finishNode(node, type);
1205 }
1206
1207 maybeAsyncOrAccessorProp(prop) {
1208 return !prop.computed && prop.key.type === "Identifier" && (this.isLiteralPropertyName() || this.match(_types.types.bracketL) || this.match(_types.types.star));
1209 }
1210
1211 parsePropertyDefinition(isPattern, refExpressionErrors) {
1212 let decorators = [];
1213
1214 if (this.match(_types.types.at)) {
1215 if (this.hasPlugin("decorators")) {
1216 this.raise(this.state.start, _error.Errors.UnsupportedPropertyDecorator);
1217 }
1218
1219 while (this.match(_types.types.at)) {
1220 decorators.push(this.parseDecorator());
1221 }
1222 }
1223
1224 const prop = this.startNode();
1225 let isGenerator = false;
1226 let isAsync = false;
1227 let isAccessor = false;
1228 let startPos;
1229 let startLoc;
1230
1231 if (this.match(_types.types.ellipsis)) {
1232 if (decorators.length) this.unexpected();
1233
1234 if (isPattern) {
1235 this.next();
1236 prop.argument = this.parseIdentifier();
1237 this.checkCommaAfterRest(125);
1238 return this.finishNode(prop, "RestElement");
1239 }
1240
1241 return this.parseSpread();
1242 }
1243
1244 if (decorators.length) {
1245 prop.decorators = decorators;
1246 decorators = [];
1247 }
1248
1249 prop.method = false;
1250
1251 if (isPattern || refExpressionErrors) {
1252 startPos = this.state.start;
1253 startLoc = this.state.startLoc;
1254 }
1255
1256 if (!isPattern) {
1257 isGenerator = this.eat(_types.types.star);
1258 }
1259
1260 const containsEsc = this.state.containsEsc;
1261 const key = this.parsePropertyName(prop, false);
1262
1263 if (!isPattern && !isGenerator && !containsEsc && this.maybeAsyncOrAccessorProp(prop)) {
1264 const keyName = key.name;
1265
1266 if (keyName === "async" && !this.hasPrecedingLineBreak()) {
1267 isAsync = true;
1268 isGenerator = this.eat(_types.types.star);
1269 this.parsePropertyName(prop, false);
1270 }
1271
1272 if (keyName === "get" || keyName === "set") {
1273 isAccessor = true;
1274 prop.kind = keyName;
1275
1276 if (this.match(_types.types.star)) {
1277 isGenerator = true;
1278 this.raise(this.state.pos, _error.Errors.AccessorIsGenerator, keyName);
1279 this.next();
1280 }
1281
1282 this.parsePropertyName(prop, false);
1283 }
1284 }
1285
1286 this.parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors);
1287 return prop;
1288 }
1289
1290 getGetterSetterExpectedParamCount(method) {
1291 return method.kind === "get" ? 0 : 1;
1292 }
1293
1294 getObjectOrClassMethodParams(method) {
1295 return method.params;
1296 }
1297
1298 checkGetterSetterParams(method) {
1299 const paramCount = this.getGetterSetterExpectedParamCount(method);
1300 const params = this.getObjectOrClassMethodParams(method);
1301 const start = method.start;
1302
1303 if (params.length !== paramCount) {
1304 if (method.kind === "get") {
1305 this.raise(start, _error.Errors.BadGetterArity);
1306 } else {
1307 this.raise(start, _error.Errors.BadSetterArity);
1308 }
1309 }
1310
1311 if (method.kind === "set" && params[params.length - 1]?.type === "RestElement") {
1312 this.raise(start, _error.Errors.BadSetterRestParameter);
1313 }
1314 }
1315
1316 parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) {
1317 if (isAccessor) {
1318 this.parseMethod(prop, isGenerator, false, false, false, "ObjectMethod");
1319 this.checkGetterSetterParams(prop);
1320 return prop;
1321 }
1322
1323 if (isAsync || isGenerator || this.match(_types.types.parenL)) {
1324 if (isPattern) this.unexpected();
1325 prop.kind = "method";
1326 prop.method = true;
1327 return this.parseMethod(prop, isGenerator, isAsync, false, false, "ObjectMethod");
1328 }
1329 }
1330
1331 parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors) {
1332 prop.shorthand = false;
1333
1334 if (this.eat(_types.types.colon)) {
1335 prop.value = isPattern ? this.parseMaybeDefault(this.state.start, this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors);
1336 return this.finishNode(prop, "ObjectProperty");
1337 }
1338
1339 if (!prop.computed && prop.key.type === "Identifier") {
1340 this.checkReservedWord(prop.key.name, prop.key.start, true, false);
1341
1342 if (isPattern) {
1343 prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
1344 } else if (this.match(_types.types.eq) && refExpressionErrors) {
1345 if (refExpressionErrors.shorthandAssign === -1) {
1346 refExpressionErrors.shorthandAssign = this.state.start;
1347 }
1348
1349 prop.value = this.parseMaybeDefault(startPos, startLoc, prop.key.__clone());
1350 } else {
1351 prop.value = prop.key.__clone();
1352 }
1353
1354 prop.shorthand = true;
1355 return this.finishNode(prop, "ObjectProperty");
1356 }
1357 }
1358
1359 parseObjPropValue(prop, startPos, startLoc, isGenerator, isAsync, isPattern, isAccessor, refExpressionErrors) {
1360 const node = this.parseObjectMethod(prop, isGenerator, isAsync, isPattern, isAccessor) || this.parseObjectProperty(prop, startPos, startLoc, isPattern, refExpressionErrors);
1361 if (!node) this.unexpected();
1362 return node;
1363 }
1364
1365 parsePropertyName(prop, isPrivateNameAllowed) {
1366 if (this.eat(_types.types.bracketL)) {
1367 prop.computed = true;
1368 prop.key = this.parseMaybeAssignAllowIn();
1369 this.expect(_types.types.bracketR);
1370 } else {
1371 const oldInPropertyName = this.state.inPropertyName;
1372 this.state.inPropertyName = true;
1373 prop.key = this.match(_types.types.num) || this.match(_types.types.string) || this.match(_types.types.bigint) || this.match(_types.types.decimal) ? this.parseExprAtom() : this.parseMaybePrivateName(isPrivateNameAllowed);
1374
1375 if (prop.key.type !== "PrivateName") {
1376 prop.computed = false;
1377 }
1378
1379 this.state.inPropertyName = oldInPropertyName;
1380 }
1381
1382 return prop.key;
1383 }
1384
1385 initFunction(node, isAsync) {
1386 node.id = null;
1387 node.generator = false;
1388 node.async = !!isAsync;
1389 }
1390
1391 parseMethod(node, isGenerator, isAsync, isConstructor, allowDirectSuper, type, inClassScope = false) {
1392 this.initFunction(node, isAsync);
1393 node.generator = !!isGenerator;
1394 const allowModifiers = isConstructor;
1395 this.scope.enter(_scopeflags.SCOPE_FUNCTION | _scopeflags.SCOPE_SUPER | (inClassScope ? _scopeflags.SCOPE_CLASS : 0) | (allowDirectSuper ? _scopeflags.SCOPE_DIRECT_SUPER : 0));
1396 this.prodParam.enter((0, _productionParameter.functionFlags)(isAsync, node.generator));
1397 this.parseFunctionParams(node, allowModifiers);
1398 this.parseFunctionBodyAndFinish(node, type, true);
1399 this.prodParam.exit();
1400 this.scope.exit();
1401 return node;
1402 }
1403
1404 parseArrayLike(close, canBePattern, isTuple, refExpressionErrors) {
1405 if (isTuple) {
1406 this.expectPlugin("recordAndTuple");
1407 }
1408
1409 const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
1410 this.state.inFSharpPipelineDirectBody = false;
1411 const node = this.startNode();
1412 this.next();
1413 node.elements = this.parseExprList(close, !isTuple, refExpressionErrors, node);
1414 this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
1415 return this.finishNode(node, isTuple ? "TupleExpression" : "ArrayExpression");
1416 }
1417
1418 parseArrowExpression(node, params, isAsync, trailingCommaPos) {
1419 this.scope.enter(_scopeflags.SCOPE_FUNCTION | _scopeflags.SCOPE_ARROW);
1420 let flags = (0, _productionParameter.functionFlags)(isAsync, false);
1421
1422 if (!this.match(_types.types.bracketL) && this.prodParam.hasIn) {
1423 flags |= _productionParameter.PARAM_IN;
1424 }
1425
1426 this.prodParam.enter(flags);
1427 this.initFunction(node, isAsync);
1428 const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
1429
1430 if (params) {
1431 this.state.maybeInArrowParameters = true;
1432 this.setArrowFunctionParameters(node, params, trailingCommaPos);
1433 }
1434
1435 this.state.maybeInArrowParameters = false;
1436 this.parseFunctionBody(node, true);
1437 this.prodParam.exit();
1438 this.scope.exit();
1439 this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
1440 return this.finishNode(node, "ArrowFunctionExpression");
1441 }
1442
1443 setArrowFunctionParameters(node, params, trailingCommaPos) {
1444 node.params = this.toAssignableList(params, trailingCommaPos);
1445 }
1446
1447 parseFunctionBodyAndFinish(node, type, isMethod = false) {
1448 this.parseFunctionBody(node, false, isMethod);
1449 this.finishNode(node, type);
1450 }
1451
1452 parseFunctionBody(node, allowExpression, isMethod = false) {
1453 const isExpression = allowExpression && !this.match(_types.types.braceL);
1454 this.expressionScope.enter((0, _expressionScope.newExpressionScope)());
1455
1456 if (isExpression) {
1457 node.body = this.parseMaybeAssign();
1458 this.checkParams(node, false, allowExpression, false);
1459 } else {
1460 const oldStrict = this.state.strict;
1461 const oldLabels = this.state.labels;
1462 this.state.labels = [];
1463 this.prodParam.enter(this.prodParam.currentFlags() | _productionParameter.PARAM_RETURN);
1464 node.body = this.parseBlock(true, false, hasStrictModeDirective => {
1465 const nonSimple = !this.isSimpleParamList(node.params);
1466
1467 if (hasStrictModeDirective && nonSimple) {
1468 const errorPos = (node.kind === "method" || node.kind === "constructor") && !!node.key ? node.key.end : node.start;
1469 this.raise(errorPos, _error.Errors.IllegalLanguageModeDirective);
1470 }
1471
1472 const strictModeChanged = !oldStrict && this.state.strict;
1473 this.checkParams(node, !this.state.strict && !allowExpression && !isMethod && !nonSimple, allowExpression, strictModeChanged);
1474
1475 if (this.state.strict && node.id) {
1476 this.checkLVal(node.id, _scopeflags.BIND_OUTSIDE, undefined, "function name", undefined, strictModeChanged);
1477 }
1478 });
1479 this.prodParam.exit();
1480 this.expressionScope.exit();
1481 this.state.labels = oldLabels;
1482 }
1483 }
1484
1485 isSimpleParamList(params) {
1486 for (let i = 0, len = params.length; i < len; i++) {
1487 if (params[i].type !== "Identifier") return false;
1488 }
1489
1490 return true;
1491 }
1492
1493 checkParams(node, allowDuplicates, isArrowFunction, strictModeChanged = true) {
1494 const nameHash = Object.create(null);
1495
1496 for (let i = 0; i < node.params.length; i++) {
1497 this.checkLVal(node.params[i], _scopeflags.BIND_VAR, allowDuplicates ? null : nameHash, "function parameter list", undefined, strictModeChanged);
1498 }
1499 }
1500
1501 parseExprList(close, allowEmpty, refExpressionErrors, nodeForExtra) {
1502 const elts = [];
1503 let first = true;
1504
1505 while (!this.eat(close)) {
1506 if (first) {
1507 first = false;
1508 } else {
1509 this.expect(_types.types.comma);
1510
1511 if (this.match(close)) {
1512 if (nodeForExtra) {
1513 this.addExtra(nodeForExtra, "trailingComma", this.state.lastTokStart);
1514 }
1515
1516 this.next();
1517 break;
1518 }
1519 }
1520
1521 elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
1522 }
1523
1524 return elts;
1525 }
1526
1527 parseExprListItem(allowEmpty, refExpressionErrors, refNeedsArrowPos, allowPlaceholder) {
1528 let elt;
1529
1530 if (this.match(_types.types.comma)) {
1531 if (!allowEmpty) {
1532 this.raise(this.state.pos, _error.Errors.UnexpectedToken, ",");
1533 }
1534
1535 elt = null;
1536 } else if (this.match(_types.types.ellipsis)) {
1537 const spreadNodeStartPos = this.state.start;
1538 const spreadNodeStartLoc = this.state.startLoc;
1539 elt = this.parseParenItem(this.parseSpread(refExpressionErrors, refNeedsArrowPos), spreadNodeStartPos, spreadNodeStartLoc);
1540 } else if (this.match(_types.types.question)) {
1541 this.expectPlugin("partialApplication");
1542
1543 if (!allowPlaceholder) {
1544 this.raise(this.state.start, _error.Errors.UnexpectedArgumentPlaceholder);
1545 }
1546
1547 const node = this.startNode();
1548 this.next();
1549 elt = this.finishNode(node, "ArgumentPlaceholder");
1550 } else {
1551 elt = this.parseMaybeAssignAllowIn(refExpressionErrors, this.parseParenItem, refNeedsArrowPos);
1552 }
1553
1554 return elt;
1555 }
1556
1557 parseIdentifier(liberal) {
1558 const node = this.startNode();
1559 const name = this.parseIdentifierName(node.start, liberal);
1560 return this.createIdentifier(node, name);
1561 }
1562
1563 createIdentifier(node, name) {
1564 node.name = name;
1565 node.loc.identifierName = name;
1566 return this.finishNode(node, "Identifier");
1567 }
1568
1569 parseIdentifierName(pos, liberal) {
1570 let name;
1571 const {
1572 start,
1573 type
1574 } = this.state;
1575
1576 if (type === _types.types.name) {
1577 name = this.state.value;
1578 } else if (type.keyword) {
1579 name = type.keyword;
1580 const curContext = this.curContext();
1581
1582 if ((type === _types.types._class || type === _types.types._function) && (curContext === _context.types.functionStatement || curContext === _context.types.functionExpression)) {
1583 this.state.context.pop();
1584 }
1585 } else {
1586 throw this.unexpected();
1587 }
1588
1589 if (liberal) {
1590 this.state.type = _types.types.name;
1591 } else {
1592 this.checkReservedWord(name, start, !!type.keyword, false);
1593 }
1594
1595 this.next();
1596 return name;
1597 }
1598
1599 checkReservedWord(word, startLoc, checkKeywords, isBinding) {
1600 if (this.prodParam.hasYield && word === "yield") {
1601 this.raise(startLoc, _error.Errors.YieldBindingIdentifier);
1602 return;
1603 }
1604
1605 if (word === "await") {
1606 if (this.prodParam.hasAwait) {
1607 this.raise(startLoc, _error.Errors.AwaitBindingIdentifier);
1608 return;
1609 } else {
1610 this.expressionScope.recordAsyncArrowParametersError(startLoc, _error.Errors.AwaitBindingIdentifier);
1611 }
1612 }
1613
1614 if (this.scope.inClass && !this.scope.inNonArrowFunction && word === "arguments") {
1615 this.raise(startLoc, _error.Errors.ArgumentsInClass);
1616 return;
1617 }
1618
1619 if (checkKeywords && (0, _identifier.isKeyword)(word)) {
1620 this.raise(startLoc, _error.Errors.UnexpectedKeyword, word);
1621 return;
1622 }
1623
1624 const reservedTest = !this.state.strict ? _identifier.isReservedWord : isBinding ? _identifier.isStrictBindReservedWord : _identifier.isStrictReservedWord;
1625
1626 if (reservedTest(word, this.inModule)) {
1627 if (!this.prodParam.hasAwait && word === "await") {
1628 this.raise(startLoc, this.hasPlugin("topLevelAwait") ? _error.Errors.AwaitNotInAsyncContext : _error.Errors.AwaitNotInAsyncFunction);
1629 } else {
1630 this.raise(startLoc, _error.Errors.UnexpectedReservedWord, word);
1631 }
1632 }
1633 }
1634
1635 isAwaitAllowed() {
1636 if (this.scope.inFunction) return this.prodParam.hasAwait;
1637 if (this.options.allowAwaitOutsideFunction) return true;
1638
1639 if (this.hasPlugin("topLevelAwait")) {
1640 return this.inModule && this.prodParam.hasAwait;
1641 }
1642
1643 return false;
1644 }
1645
1646 parseAwait() {
1647 const node = this.startNode();
1648 this.next();
1649 this.expressionScope.recordParameterInitializerError(node.start, _error.Errors.AwaitExpressionFormalParameter);
1650
1651 if (this.eat(_types.types.star)) {
1652 this.raise(node.start, _error.Errors.ObsoleteAwaitStar);
1653 }
1654
1655 if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) {
1656 if (this.hasPrecedingLineBreak() || this.match(_types.types.plusMin) || this.match(_types.types.parenL) || this.match(_types.types.bracketL) || this.match(_types.types.backQuote) || this.match(_types.types.regexp) || this.match(_types.types.slash) || this.hasPlugin("v8intrinsic") && this.match(_types.types.modulo)) {
1657 this.ambiguousScriptDifferentAst = true;
1658 } else {
1659 this.sawUnambiguousESM = true;
1660 }
1661 }
1662
1663 if (!this.state.soloAwait) {
1664 node.argument = this.parseMaybeUnary();
1665 }
1666
1667 return this.finishNode(node, "AwaitExpression");
1668 }
1669
1670 parseYield() {
1671 const node = this.startNode();
1672 this.expressionScope.recordParameterInitializerError(node.start, _error.Errors.YieldInParameter);
1673 this.next();
1674
1675 if (this.match(_types.types.semi) || !this.match(_types.types.star) && !this.state.type.startsExpr || this.hasPrecedingLineBreak()) {
1676 node.delegate = false;
1677 node.argument = null;
1678 } else {
1679 node.delegate = this.eat(_types.types.star);
1680 node.argument = this.parseMaybeAssign();
1681 }
1682
1683 return this.finishNode(node, "YieldExpression");
1684 }
1685
1686 checkPipelineAtInfixOperator(left, leftStartPos) {
1687 if (this.getPluginOption("pipelineOperator", "proposal") === "smart") {
1688 if (left.type === "SequenceExpression") {
1689 this.raise(leftStartPos, _error.Errors.PipelineHeadSequenceExpression);
1690 }
1691 }
1692 }
1693
1694 parseSmartPipelineBody(childExpression, startPos, startLoc) {
1695 this.checkSmartPipelineBodyEarlyErrors(childExpression, startPos);
1696 return this.parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc);
1697 }
1698
1699 checkSmartPipelineBodyEarlyErrors(childExpression, startPos) {
1700 if (this.match(_types.types.arrow)) {
1701 throw this.raise(this.state.start, _error.Errors.PipelineBodyNoArrow);
1702 } else if (childExpression.type === "SequenceExpression") {
1703 this.raise(startPos, _error.Errors.PipelineBodySequenceExpression);
1704 }
1705 }
1706
1707 parseSmartPipelineBodyInStyle(childExpression, startPos, startLoc) {
1708 const bodyNode = this.startNodeAt(startPos, startLoc);
1709 const isSimpleReference = this.isSimpleReference(childExpression);
1710
1711 if (isSimpleReference) {
1712 bodyNode.callee = childExpression;
1713 } else {
1714 if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
1715 this.raise(startPos, _error.Errors.PipelineTopicUnused);
1716 }
1717
1718 bodyNode.expression = childExpression;
1719 }
1720
1721 return this.finishNode(bodyNode, isSimpleReference ? "PipelineBareFunction" : "PipelineTopicExpression");
1722 }
1723
1724 isSimpleReference(expression) {
1725 switch (expression.type) {
1726 case "MemberExpression":
1727 return !expression.computed && this.isSimpleReference(expression.object);
1728
1729 case "Identifier":
1730 return true;
1731
1732 default:
1733 return false;
1734 }
1735 }
1736
1737 withTopicPermittingContext(callback) {
1738 const outerContextTopicState = this.state.topicContext;
1739 this.state.topicContext = {
1740 maxNumOfResolvableTopics: 1,
1741 maxTopicIndex: null
1742 };
1743
1744 try {
1745 return callback();
1746 } finally {
1747 this.state.topicContext = outerContextTopicState;
1748 }
1749 }
1750
1751 withTopicForbiddingContext(callback) {
1752 const outerContextTopicState = this.state.topicContext;
1753 this.state.topicContext = {
1754 maxNumOfResolvableTopics: 0,
1755 maxTopicIndex: null
1756 };
1757
1758 try {
1759 return callback();
1760 } finally {
1761 this.state.topicContext = outerContextTopicState;
1762 }
1763 }
1764
1765 withSoloAwaitPermittingContext(callback) {
1766 const outerContextSoloAwaitState = this.state.soloAwait;
1767 this.state.soloAwait = true;
1768
1769 try {
1770 return callback();
1771 } finally {
1772 this.state.soloAwait = outerContextSoloAwaitState;
1773 }
1774 }
1775
1776 allowInAnd(callback) {
1777 const flags = this.prodParam.currentFlags();
1778 const prodParamToSet = _productionParameter.PARAM_IN & ~flags;
1779
1780 if (prodParamToSet) {
1781 this.prodParam.enter(flags | _productionParameter.PARAM_IN);
1782
1783 try {
1784 return callback();
1785 } finally {
1786 this.prodParam.exit();
1787 }
1788 }
1789
1790 return callback();
1791 }
1792
1793 disallowInAnd(callback) {
1794 const flags = this.prodParam.currentFlags();
1795 const prodParamToClear = _productionParameter.PARAM_IN & flags;
1796
1797 if (prodParamToClear) {
1798 this.prodParam.enter(flags & ~_productionParameter.PARAM_IN);
1799
1800 try {
1801 return callback();
1802 } finally {
1803 this.prodParam.exit();
1804 }
1805 }
1806
1807 return callback();
1808 }
1809
1810 registerTopicReference() {
1811 this.state.topicContext.maxTopicIndex = 0;
1812 }
1813
1814 primaryTopicReferenceIsAllowedInCurrentTopicContext() {
1815 return this.state.topicContext.maxNumOfResolvableTopics >= 1;
1816 }
1817
1818 topicReferenceWasUsedInCurrentTopicContext() {
1819 return this.state.topicContext.maxTopicIndex != null && this.state.topicContext.maxTopicIndex >= 0;
1820 }
1821
1822 parseFSharpPipelineBody(prec) {
1823 const startPos = this.state.start;
1824 const startLoc = this.state.startLoc;
1825 this.state.potentialArrowAt = this.state.start;
1826 const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
1827 this.state.inFSharpPipelineDirectBody = true;
1828 const ret = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, prec);
1829 this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
1830 return ret;
1831 }
1832
1833}
1834
1835exports.default = ExpressionParser;
\No newline at end of file