Skip to content

Commit

Permalink
validate parentheses in the left-hand side of assignment expressions (#…
Browse files Browse the repository at this point in the history
…10576)

* invalid left-hand assignments based on parenthesis

* validate against nested ParenthesizedExpressions
  • Loading branch information
boweihan authored and JLHwung committed Dec 10, 2019
1 parent de1fa90 commit 20e43ad
Show file tree
Hide file tree
Showing 48 changed files with 2,811 additions and 27 deletions.
26 changes: 0 additions & 26 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -41,12 +41,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 @@ -213,26 +207,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
20 changes: 20 additions & 0 deletions packages/babel-parser/src/parser/lval.js
Expand Up @@ -22,6 +22,12 @@ import {
import { NodeUtils } from "./node";
import { type BindingTypes, BIND_NONE } from "../util/scopeflags";

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

export default class LValParser extends NodeUtils {
// Forward-declaration: defined in expression.js
+parseIdentifier: (liberal?: boolean) => Identifier;
Expand Down Expand Up @@ -49,6 +55,20 @@ export default class LValParser extends NodeUtils {
contextDescription: string,
): Node {
if (node) {
if (
(this.options.createParenthesizedExpressions &&
node.type === "ParenthesizedExpression") ||
node.extra?.parenthesized
) {
const parenthesized = unwrapParenthesizedExpression(node);
if (
parenthesized.type !== "Identifier" &&
parenthesized.type !== "MemberExpression"
) {
this.raise(node.start, "Invalid parenthesized assignment pattern");
}
}

switch (node.type) {
case "Identifier":
case "ObjectPattern":
Expand Down
@@ -0,0 +1 @@
(a = 1) = t
@@ -0,0 +1,5 @@
{
"plugins": [
"estree"
]
}
@@ -0,0 +1,138 @@
{
"type": "File",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"errors": [
"SyntaxError: Invalid parenthesized assignment pattern (1:1)"
],
"program": {
"type": "Program",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"expression": {
"type": "AssignmentExpression",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"operator": "=",
"left": {
"type": "AssignmentPattern",
"start": 1,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 6
}
},
"left": {
"type": "Identifier",
"start": 1,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 2
},
"identifierName": "a"
},
"name": "a"
},
"right": {
"type": "Literal",
"start": 5,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 6
}
},
"value": 1,
"raw": "1"
},
"extra": {
"parenthesized": true,
"parenStart": 0
}
},
"right": {
"type": "Identifier",
"start": 10,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
},
"identifierName": "t"
},
"name": "t"
}
}
}
]
}
}
@@ -0,0 +1 @@
[(a = 1)] = t
@@ -0,0 +1,5 @@
{
"plugins": [
"estree"
]
}
@@ -0,0 +1,155 @@
{
"type": "File",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"errors": [
"SyntaxError: Invalid parenthesized assignment pattern (1:2)"
],
"program": {
"type": "Program",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"expression": {
"type": "AssignmentExpression",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"operator": "=",
"left": {
"type": "ArrayPattern",
"start": 0,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
},
"elements": [
{
"type": "AssignmentPattern",
"start": 2,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 7
}
},
"left": {
"type": "Identifier",
"start": 2,
"end": 3,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 3
},
"identifierName": "a"
},
"name": "a"
},
"right": {
"type": "Literal",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
}
},
"value": 1,
"raw": "1"
},
"extra": {
"parenthesized": true,
"parenStart": 1
}
}
]
},
"right": {
"type": "Identifier",
"start": 12,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "t"
},
"name": "t"
}
}
}
]
}
}
@@ -0,0 +1 @@
[({ a: [b = 2]})] = t

0 comments on commit 20e43ad

Please sign in to comment.