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

[parser] validation for parentheses in the left-hand side of assignment expressions #10576

Merged
merged 2 commits into from Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed to handle nested parenthesis when createParenthesizedExpressions is enabled. i.e. [(((x)))]

Copy link
Contributor

@JLHwung JLHwung Nov 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For nested parenthesis, toAssignable will be called with parenthesizee recursively. Therefore we may check if node.expression is one of Identifier, MemberExpression and ParenthesizedExpression.

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