Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: disallow await before exponential
  • Loading branch information
JLHwung committed Mar 25, 2021
commit 94bef1ac78137b3a233c4297a8218304f47ded58
20 changes: 13 additions & 7 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,15 @@ export default class ExpressionParser extends LValParser {
);
}

checkExponentialAfterUnary(node: N.AwaitExpression | N.UnaryExpression) {
if (this.match(tt.exponent)) {
this.raise(
node.argument.start,
Errors.UnexpectedTokenUnaryExponentiation,
);
}
}

// Parse unary operators, both prefix and postfix.
// https://tc39.es/ecma262/#prod-UnaryExpression
parseMaybeUnary(
Expand All @@ -507,7 +516,9 @@ export default class ExpressionParser extends LValParser {

if (isAwait && this.isAwaitAllowed()) {
this.next();
return this.parseAwait(startPos, startLoc);
const expr = this.parseAwait(startPos, startLoc);
if (!sawUnary) this.checkExponentialAfterUnary(expr);
return expr;
}
if (
this.isContextual("module") &&
Expand Down Expand Up @@ -543,12 +554,7 @@ export default class ExpressionParser extends LValParser {
}

if (!update) {
if (!sawUnary && this.match(tt.exponent)) {
this.raise(
node.argument.start,
Errors.UnexpectedTokenUnaryExponentiation,
);
}
if (!sawUnary) this.checkExponentialAfterUnary(node);
return this.finishNode(node, "UnaryExpression");
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export type YieldExpression = NodeBase & {

export type AwaitExpression = NodeBase & {
type: "AwaitExpression",
argument: ?Expression,
argument: Expression,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While yield is a yield expression, await is never valid and thus the argument here can not be null.

};

export type ArrayExpression = NodeBase & {
Expand Down