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
refactor: introduce isObjectMethod methods
  • Loading branch information
JLHwung committed Nov 19, 2020
commit c962cb3d3fdea5d20ad3e11869e1d059180b4191
3 changes: 2 additions & 1 deletion packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ export default class ExpressionParser extends LValParser {
): void {
if (
prop.type === "SpreadElement" ||
prop.type === "ObjectMethod" ||
this.isObjectMethod(prop) ||
prop.computed ||
// $FlowIgnore
prop.shorthand
) {
return;
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/parser/lval.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,11 @@ export default class LValParser extends NodeUtils {

case "ObjectPattern":
for (let prop of expr.properties) {
if (prop.type === "ObjectProperty") prop = prop.value;
if (this.isObjectProperty(prop)) prop = prop.value;
// If we find here an ObjectMethod, it's because this was originally
// an ObjectExpression which has then been converted.
// toAssignable already reported this error with a nicer message.
else if (prop.type === "ObjectMethod") continue;
else if (this.isObjectMethod(prop)) continue;

this.checkLVal(
prop,
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-parser/src/parser/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ export default class UtilParser extends Tokenizer {
isObjectProperty(node: Node): boolean {
return node.type === "ObjectProperty";
}

isObjectMethod(node: Node): boolean {
return node.type === "ObjectMethod";
}
}

/**
Expand Down
50 changes: 4 additions & 46 deletions packages/babel-parser/src/plugins/estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type Parser from "../parser";
import type { ExpressionErrors } from "../parser/util";
import * as N from "../types";
import type { Position } from "../util/location";
import { type BindingTypes } from "../util/scopeflags";
import { Errors } from "../parser/error";

export default (superClass: Class<Parser>): Class<Parser> =>
Expand Down Expand Up @@ -106,51 +105,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
.params;
}

checkLVal(
expr: N.Expression,
contextDescription: string,
...args: [
BindingTypes | void,
?Set<string>,
boolean | void,
boolean | void,
]
): void {
switch (expr.type) {
case "ObjectPattern":
expr.properties.forEach(prop => {
// If we find here a method or accessor, it's because this was originally
// an ObjectExpression which has then been converted.
// toAssignable already reported this error with a nicer message.
if (this.isMethodOrAccessor(prop)) {
return;
}
this.checkLVal(
prop.type === "Property" ? prop.value : prop,
"object destructuring pattern",
...args,
);
});
break;
default:
super.checkLVal(expr, contextDescription, ...args);
}
}

isMethodOrAccessor(node: N.Node): boolean {
return node.method || node.kind === "get" || node.kind === "set";
}

checkProto(
prop: N.ObjectMember | N.SpreadElement,
...args: [boolean, { used: boolean }, ?ExpressionErrors]
): void {
if (this.isMethodOrAccessor(prop)) {
return;
}
super.checkProto(prop, ...args);
}

isValidDirective(stmt: N.Statement): boolean {
return (
stmt.type === "ExpressionStatement" &&
Expand Down Expand Up @@ -466,4 +420,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
isObjectProperty(node: N.Node): boolean {
return node.type === "Property" && node.kind === "init" && !node.method;
}

isObjectMethod(node: N.Node): boolean {
return node.method || node.kind === "get" || node.kind === "set";
}
};