Skip to content

Commit

Permalink
refactor: introduce isObjectMethod methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Nov 19, 2020
1 parent 3cc4e5e commit c962cb3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 49 deletions.
3 changes: 2 additions & 1 deletion packages/babel-parser/src/parser/expression.js
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
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
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
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";
}
};

0 comments on commit c962cb3

Please sign in to comment.