Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure babel parser throws exactly same recoverable errors when estree plugin is enabled #12375

Merged
merged 13 commits into from
Dec 3, 2020
Merged
Prev Previous commit
Next Next commit
fix: throw new a?.() on estree
  • Loading branch information
JLHwung committed Nov 19, 2020
commit 5ddb9513a2dee45c1a5b94019b06e5bab13b460a
8 changes: 2 additions & 6 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,12 +614,12 @@ export default class ExpressionParser extends LValParser {

let optional = false;
if (this.match(tt.questionDot)) {
state.optionalChainMember = optional = true;
if (noCalls && this.lookaheadCharCode() === charCodes.leftParenthesis) {
// stop at `?.` when parsing `new a?.()`
state.stop = true;
return base;
}
state.optionalChainMember = optional = true;
this.next();
}

Expand Down Expand Up @@ -1495,13 +1495,9 @@ export default class ExpressionParser extends LValParser {
// https://tc39.es/ecma262/#prod-NewExpression
parseNew(node: N.Expression): N.NewExpression {
node.callee = this.parseNoCallExpr();

if (node.callee.type === "Import") {
this.raise(node.callee.start, Errors.ImportCallNotNewExpression);
} else if (
node.callee.type === "OptionalMemberExpression" ||
node.callee.type === "OptionalCallExpression"
) {
} else if (this.isOptionalChain(node.callee)) {
this.raise(this.state.lastTokEnd, Errors.OptionalChainingNoNew);
} else if (this.eat(tt.questionDot)) {
this.raise(this.state.start, Errors.OptionalChainingNoNew);
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-parser/src/parser/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ export default class UtilParser extends Tokenizer {
this.isPrivateName(node.property)
);
}

isOptionalChain(node: Node): boolean {
return (
node.type === "OptionalMemberExpression" ||
node.type === "OptionalCallExpression"
);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-parser/src/plugins/estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
return super.hasPropertyAsPrivateName(node);
}

isOptionalChain(node) {
return node.type === "ChainExpression";
}
};