From 6b0076090e10fe15344e98b0b4af78819b18cc09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Sun, 19 Jan 2020 19:00:29 -0500 Subject: [PATCH] refactor: simplify toAssignable on ParenthesizedExpression --- packages/babel-parser/src/parser/lval.js | 11 +- .../fixtures/core/uncategorised/567/input.js | 1 + .../core/uncategorised/567/options.json | 3 + .../core/uncategorised/567/output.json | 138 ++++++++++++++++++ 4 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/core/uncategorised/567/input.js create mode 100644 packages/babel-parser/test/fixtures/core/uncategorised/567/options.json create mode 100644 packages/babel-parser/test/fixtures/core/uncategorised/567/output.json diff --git a/packages/babel-parser/src/parser/lval.js b/packages/babel-parser/src/parser/lval.js index 958bab10d6a9..51de283f5be7 100644 --- a/packages/babel-parser/src/parser/lval.js +++ b/packages/babel-parser/src/parser/lval.js @@ -51,12 +51,9 @@ export default class LValParser extends NodeUtils { // When this one is updated, please check if also that one needs to be updated. toAssignable(node: Node): Node { - if ( - (this.options.createParenthesizedExpressions && - node.type === "ParenthesizedExpression") || - node.extra?.parenthesized - ) { - const parenthesized = unwrapParenthesizedExpression(node); + let parenthesized = undefined; + if (node.type === "ParenthesizedExpression" || node.extra?.parenthesized) { + parenthesized = unwrapParenthesizedExpression(node); if ( parenthesized.type !== "Identifier" && parenthesized.type !== "MemberExpression" @@ -125,7 +122,7 @@ export default class LValParser extends NodeUtils { break; case "ParenthesizedExpression": - node.expression = this.toAssignable(node.expression); + this.toAssignable(((parenthesized: any): Expression)); break; default: diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/567/input.js b/packages/babel-parser/test/fixtures/core/uncategorised/567/input.js new file mode 100644 index 000000000000..0c1c60bc67a9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/uncategorised/567/input.js @@ -0,0 +1 @@ +(!a) += 1 diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/567/options.json b/packages/babel-parser/test/fixtures/core/uncategorised/567/options.json new file mode 100644 index 000000000000..0861962d889d --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/uncategorised/567/options.json @@ -0,0 +1,3 @@ +{ + "createParenthesizedExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/core/uncategorised/567/output.json b/packages/babel-parser/test/fixtures/core/uncategorised/567/output.json new file mode 100644 index 000000000000..a6fd50b79053 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/uncategorised/567/output.json @@ -0,0 +1,138 @@ +{ + "type": "File", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "errors": [ + "SyntaxError: Invalid left-hand side in parenthesized expression (1:1)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "operator": "+=", + "left": { + "type": "ParenthesizedExpression", + "start": 0, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 4 + } + }, + "expression": { + "type": "UnaryExpression", + "start": 1, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "operator": "!", + "prefix": true, + "argument": { + "type": "Identifier", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "a" + }, + "name": "a" + } + } + }, + "right": { + "type": "NumericLiteral", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file