Skip to content

Commit

Permalink
validate against nested ParenthesizedExpressions
Browse files Browse the repository at this point in the history
  • Loading branch information
boweihan authored and nicolo-ribaudo committed Dec 10, 2019
1 parent 9724926 commit b1ca931
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 21 deletions.
41 changes: 20 additions & 21 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 All @@ -43,33 +49,26 @@ export default class LValParser extends NodeUtils {
// NOTE: There is a corresponding "isAssignable" method in flow.js.
// When this one is updated, please check if also that one needs to be updated.

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);
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 @@
[(((x)))] = t;
@@ -0,0 +1,121 @@
{
"type": "File",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"program": {
"type": "Program",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"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": "Identifier",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
},
"identifierName": "x"
},
"name": "x",
"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"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
[(((x)))] = t;
@@ -0,0 +1,3 @@
{
"createParenthesizedExpressions": true
}
162 changes: 162 additions & 0 deletions packages/babel-parser/test/fixtures/core/uncategorised/566/output.json
@@ -0,0 +1,162 @@
{
"type": "File",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"program": {
"type": "Program",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 14
}
},
"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": "ParenthesizedExpression",
"start": 1,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 8
}
},
"expression": {
"type": "ParenthesizedExpression",
"start": 2,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 7
}
},
"expression": {
"type": "ParenthesizedExpression",
"start": 3,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 6
}
},
"expression": {
"type": "Identifier",
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
},
"identifierName": "x"
},
"name": "x"
}
}
}
}
]
},
"right": {
"type": "Identifier",
"start": 12,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "t"
},
"name": "t"
}
}
}
],
"directives": []
}
}

0 comments on commit b1ca931

Please sign in to comment.