Skip to content

Commit

Permalink
Remove duplicates messages about invalid lhs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 24, 2019
1 parent d29d917 commit 6664b40
Show file tree
Hide file tree
Showing 37 changed files with 76 additions and 69 deletions.
10 changes: 9 additions & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -205,7 +205,13 @@ export default class ExpressionParser extends LValParser {
this.expectPlugin("logicalAssignment");
}
node.left = this.match(tt.eq)
? this.toAssignable(left, undefined, "assignment expression")
? this.toAssignable(
left,
undefined,
"assignment expression",
// ignore invalid lhs expressions, since they will be reported by checkLVal
true,
)
: left;
refShorthandDefaultPos.start = 0; // reset because shorthand default was used correctly

Expand Down Expand Up @@ -1835,6 +1841,8 @@ export default class ExpressionParser extends LValParser {
params,
true,
"arrow function parameters",
// Ignore invalid bindings, since they will be checked by checkParams
true,
);
}

Expand Down
55 changes: 40 additions & 15 deletions packages/babel-parser/src/parser/lval.js
Expand Up @@ -41,6 +41,7 @@ export default class LValParser extends NodeUtils {
node: Node,
isBinding: ?boolean,
contextDescription: string,
ignoreInvalid: boolean = false,
): Node {
if (node) {
switch (node.type) {
Expand All @@ -59,26 +60,41 @@ export default class LValParser extends NodeUtils {
) {
const prop = node.properties[i];
const isLast = i === last;
this.toAssignableObjectExpressionProp(prop, isBinding, isLast);
this.toAssignableObjectExpressionProp(
prop,
isBinding,
isLast,
ignoreInvalid,
);
}
break;

case "ObjectProperty":
this.toAssignable(node.value, isBinding, contextDescription);
this.toAssignable(
node.value,
isBinding,
contextDescription,
ignoreInvalid,
);
break;

case "SpreadElement": {
this.checkToRestConversion(node);

node.type = "RestElement";
const arg = node.argument;
this.toAssignable(arg, isBinding, contextDescription);
this.toAssignable(arg, isBinding, contextDescription, ignoreInvalid);
break;
}

case "ArrayExpression":
node.type = "ArrayPattern";
this.toAssignableList(node.elements, isBinding, contextDescription);
this.toAssignableList(
node.elements,
isBinding,
contextDescription,
ignoreInvalid,
);
break;

case "AssignmentExpression":
Expand All @@ -98,20 +114,22 @@ export default class LValParser extends NodeUtils {
node.expression,
isBinding,
contextDescription,
ignoreInvalid,
);
break;

case "MemberExpression":
if (!isBinding) break;

default: {
const message =
"Invalid left-hand side" +
(contextDescription
? " in " + contextDescription
: /* istanbul ignore next */ "expression");
this.raise(node.start, message);
}
default:
if (!ignoreInvalid) {
const message =
"Invalid left-hand side" +
(contextDescription
? " in " + contextDescription
: /* istanbul ignore next */ "expression");
this.raise(node.start, message);
}
}
}
return node;
Expand All @@ -121,6 +139,7 @@ export default class LValParser extends NodeUtils {
prop: Node,
isBinding: ?boolean,
isLast: boolean,
ignoreInvalid?: boolean = false,
) {
if (prop.type === "ObjectMethod") {
const error =
Expand All @@ -132,7 +151,12 @@ export default class LValParser extends NodeUtils {
} else if (prop.type === "SpreadElement" && !isLast) {
this.raiseRestNotLast(prop.start);
} else {
this.toAssignable(prop, isBinding, "object destructuring pattern");
this.toAssignable(
prop,
isBinding,
"object destructuring pattern",
ignoreInvalid,
);
}
}

Expand All @@ -142,6 +166,7 @@ export default class LValParser extends NodeUtils {
exprList: Expression[],
isBinding: ?boolean,
contextDescription: string,
ignoreInvalid?: boolean = false,
): $ReadOnlyArray<Pattern> {
let end = exprList.length;
if (end) {
Expand All @@ -151,7 +176,7 @@ export default class LValParser extends NodeUtils {
} else if (last && last.type === "SpreadElement") {
last.type = "RestElement";
const arg = last.argument;
this.toAssignable(arg, isBinding, contextDescription);
this.toAssignable(arg, isBinding, contextDescription, ignoreInvalid);
if (
arg.type !== "Identifier" &&
arg.type !== "MemberExpression" &&
Expand All @@ -166,7 +191,7 @@ export default class LValParser extends NodeUtils {
for (let i = 0; i < end; i++) {
const elt = exprList[i];
if (elt) {
this.toAssignable(elt, isBinding, contextDescription);
this.toAssignable(elt, isBinding, contextDescription, ignoreInvalid);
if (elt.type === "RestElement") {
this.raiseRestNotLast(elt.start);
}
Expand Down
18 changes: 10 additions & 8 deletions packages/babel-parser/src/plugins/estree.js
Expand Up @@ -359,21 +359,23 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node: N.Node,
isBinding: ?boolean,
contextDescription: string,
ignoreInvalid?: boolean,
): N.Node {
if (isSimpleProperty(node)) {
this.toAssignable(node.value, isBinding, contextDescription);
this.toAssignable(
node.value,
isBinding,
contextDescription,
ignoreInvalid,
);

return node;
}

return super.toAssignable(node, isBinding, contextDescription);
return super.toAssignable(...arguments);
}

toAssignableObjectExpressionProp(
prop: N.Node,
isBinding: ?boolean,
isLast: boolean,
) {
toAssignableObjectExpressionProp(prop: N.Node) {
if (prop.kind === "get" || prop.kind === "set") {
throw this.raise(
prop.key.start,
Expand All @@ -385,7 +387,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
"Object pattern can't contain methods",
);
} else {
super.toAssignableObjectExpressionProp(prop, isBinding, isLast);
super.toAssignableObjectExpressionProp(...arguments);
}
}
};
12 changes: 10 additions & 2 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -1978,15 +1978,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node: N.Node,
isBinding: ?boolean,
contextDescription: string,
ignoreInvalid?: boolean,
): N.Node {
if (node.type === "TypeCastExpression") {
return super.toAssignable(
this.typeCastToParameter(node),
isBinding,
contextDescription,
ignoreInvalid,
);
} else {
return super.toAssignable(node, isBinding, contextDescription);
return super.toAssignable(...arguments);
}
}

Expand All @@ -1995,14 +1997,20 @@ export default (superClass: Class<Parser>): Class<Parser> =>
exprList: N.Expression[],
isBinding: ?boolean,
contextDescription: string,
ignoreInvalid?: boolean,
): $ReadOnlyArray<N.Pattern> {
for (let i = 0; i < exprList.length; i++) {
const expr = exprList[i];
if (expr && expr.type === "TypeCastExpression") {
exprList[i] = this.typeCastToParameter(expr);
}
}
return super.toAssignableList(exprList, isBinding, contextDescription);
return super.toAssignableList(
exprList,
isBinding,
contextDescription,
ignoreInvalid,
);
}

// this is a list of nodes, from something like a call expression, we need to filter the
Expand Down
16 changes: 7 additions & 9 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -2233,27 +2233,29 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node: N.Node,
isBinding: ?boolean,
contextDescription: string,
ignoreInvalid?: boolean,
): N.Node {
switch (node.type) {
case "TSTypeCastExpression":
return super.toAssignable(
this.typeCastToParameter(node),
isBinding,
contextDescription,
ignoreInvalid,
);
case "TSParameterProperty":
return super.toAssignable(node, isBinding, contextDescription);
case "TSAsExpression":
case "TSNonNullExpression":
case "TSTypeAssertion":
node.expression = this.toAssignable(
node.expression,
isBinding,
contextDescription,
ignoreInvalid,
);
return node;
case "TSParameterProperty":
default:
return super.toAssignable(node, isBinding, contextDescription);
return super.toAssignable(...arguments);
}
}

Expand Down Expand Up @@ -2361,11 +2363,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}

toAssignableList(
exprList: N.Expression[],
isBinding: ?boolean,
contextDescription: string,
): $ReadOnlyArray<N.Pattern> {
toAssignableList(exprList: N.Expression[]): $ReadOnlyArray<N.Pattern> {
for (let i = 0; i < exprList.length; i++) {
const expr = exprList[i];
if (!expr) continue;
Expand All @@ -2382,7 +2380,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
break;
}
}
return super.toAssignableList(exprList, isBinding, contextDescription);
return super.toAssignableList(...arguments);
}

typeCastToParameter(node: N.TsTypeCastExpression): N.Node {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in assignment expression (1:0)",
"SyntaxError: Invalid left-hand side in assignment expression (1:0)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in assignment expression (1:0)",
"SyntaxError: Invalid left-hand side in assignment expression (1:0)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in assignment expression (1:1)",
"SyntaxError: Invalid left-hand side in assignment expression (1:1)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in assignment expression (1:0)",
"SyntaxError: Invalid left-hand side in assignment expression (1:0)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in assignment expression (1:0)",
"SyntaxError: Invalid left-hand side in assignment expression (1:0)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in assignment expression (1:1)",
"SyntaxError: Invalid left-hand side in array destructuring pattern (1:1)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in object destructuring pattern (1:7)",
"SyntaxError: Invalid left-hand side in object destructuring pattern (1:7)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in arrow function parameters (1:1)",
"SyntaxError: Binding invalid left-hand side in function paramter list (1:1)"
],
"program": {
Expand Down
Expand Up @@ -13,8 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in arrow function parameters (1:1)",
"SyntaxError: Invalid left-hand side in arrow function parameters (1:5)",
"SyntaxError: Binding invalid left-hand side in function paramter list (1:1)",
"SyntaxError: Binding invalid left-hand side in function paramter list (1:5)"
],
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in arrow function parameters (1:3)",
"SyntaxError: Binding invalid left-hand side in array destructuring pattern (1:3)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in arrow function parameters (1:2)",
"SyntaxError: Binding member expression (1:2)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,6 @@
}
},
"errors": [
"SyntaxError: Invalid left-hand side in arrow function parameters (1:2)",
"SyntaxError: Binding member expression (1:2)"
],
"program": {
Expand Down
Expand Up @@ -14,7 +14,6 @@
},
"errors": [
"SyntaxError: Yield cannot be used as name inside a generator function (2:8)",
"SyntaxError: Invalid left-hand side in arrow function parameters (2:8)",
"SyntaxError: Binding invalid left-hand side in function paramter list (2:8)"
],
"program": {
Expand Down

0 comments on commit 6664b40

Please sign in to comment.