Skip to content

Commit

Permalink
invalid left-hand assignments based on parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
boweihan committed Nov 3, 2019
1 parent c7d8b8a commit cd4bf07
Show file tree
Hide file tree
Showing 47 changed files with 663 additions and 41 deletions.
26 changes: 0 additions & 26 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ import {
SCOPE_PROGRAM,
} from "../util/scopeflags";

const unwrapParenthesizedExpression = node => {
return node.type === "ParenthesizedExpression"
? unwrapParenthesizedExpression(node.expression)
: node;
};

export default class ExpressionParser extends LValParser {
// Forward-declaration: defined in statement.js
+parseBlock: (
Expand Down Expand Up @@ -211,26 +205,6 @@ export default class ExpressionParser extends LValParser {

this.checkLVal(left, undefined, undefined, "assignment expression");

const maybePattern = unwrapParenthesizedExpression(left);

let patternErrorMsg;
if (maybePattern.type === "ObjectPattern") {
patternErrorMsg = "`({a}) = 0` use `({a} = 0)`";
} else if (maybePattern.type === "ArrayPattern") {
patternErrorMsg = "`([a]) = 0` use `([a] = 0)`";
}

if (
patternErrorMsg &&
((left.extra && left.extra.parenthesized) ||
left.type === "ParenthesizedExpression")
) {
this.raise(
maybePattern.start,
`You're trying to assign to a parenthesized expression, eg. instead of ${patternErrorMsg}`,
);
}

this.next();
node.right = this.parseMaybeAssign(noIn);
return this.finishNode(node, "AssignmentExpression");
Expand Down
21 changes: 21 additions & 0 deletions packages/babel-parser/src/parser/lval.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,33 @@ export default class LValParser extends NodeUtils {
// Convert existing expression atom to assignable pattern
// if possible.

validateParenthesizedExpression(node: Node): void {
let parenthesized;
if (
this.options.createParenthesizedExpressions &&
node.type === "ParenthesizedExpression"
) {
parenthesized = node.expression;
} else if (node.extra?.parenthesized) {
parenthesized = node;
}

if (
parenthesized &&
parenthesized.type !== "Identifier" &&
parenthesized.type !== "MemberExpression"
) {
this.raise(node.start, "Invalid parenthesized assignment pattern");
}
}

toAssignable(
node: Node,
isBinding: ?boolean,
contextDescription: string,
): Node {
if (node) {
this.validateParenthesizedExpression(node);
switch (node.type) {
case "Identifier":
case "ObjectPattern":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(a = 1) = t
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Invalid parenthesized assignment pattern (1:1)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[(a = 1)] = t
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Invalid parenthesized assignment pattern (1:2)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[({ a: [b = 2]})] = t
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Invalid parenthesized assignment pattern (1:2)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{b: [([a = 1])]}] = t
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Invalid parenthesized assignment pattern (1:7)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[([x])] = t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"throws": "Invalid parenthesized assignment pattern (1:2)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[(a.x)] = t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"type": "File",
"start": 0,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"expression": {
"type": "AssignmentExpression",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"operator": "=",
"left": {
"type": "ArrayPattern",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
},
"elements": [
{
"type": "MemberExpression",
"start": 2,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 5
}
},
"object": {
"type": "Identifier",
"start": 2,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 3
},
"identifierName": "a"
},
"name": "a"
},
"property": {
"type": "Identifier",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
},
"identifierName": "x"
},
"name": "x"
},
"computed": false,
"extra": {
"parenthesized": true,
"parenStart": 1
}
}
]
},
"right": {
"type": "Identifier",
"start": 10,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
},
"identifierName": "t"
},
"name": "t"
}
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[(x)] = t;
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"type": "File",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"program": {
"type": "Program",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"expression": {
"type": "AssignmentExpression",
"start": 0,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
},
"operator": "=",
"left": {
"type": "ArrayPattern",
"start": 0,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
},
"elements": [
{
"type": "Identifier",
"start": 2,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 3
},
"identifierName": "x"
},
"name": "x",
"extra": {
"parenthesized": true,
"parenStart": 1
}
}
]
},
"right": {
"type": "Identifier",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "t"
},
"name": "t"
}
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"throws": "Invalid left-hand side in assignment expression (1:1)"
"throws": "Invalid parenthesized assignment pattern (1:1)"
}

0 comments on commit cd4bf07

Please sign in to comment.