From 4f727139ecb06b34403c2c17f19e30dcdc037829 Mon Sep 17 00:00:00 2001 From: Federico Ciardi Date: Mon, 22 Mar 2021 17:00:03 +0100 Subject: [PATCH] fix(ts): Allow parenthesized "assert and assign" (#12933) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo <7000710+nicolo-ribaudo@users.noreply.github.com> --- .../src/plugins/typescript/index.js | 15 +++++ .../parenthesized-object-assign/input.ts | 1 + .../parenthesized-object-assign/options.json | 3 + .../parenthesized-object-assign/output.json | 39 +++++++++++++ .../input.ts | 1 + .../options.json | 3 + .../output.json | 55 +++++++++++++++++++ .../cast/parenthesized-as-and-assign/input.ts | 1 + .../parenthesized-as-and-assign/options.json | 3 + .../parenthesized-as-and-assign/output.json | 48 ++++++++++++++++ .../parenthesized-assert-and-assign/input.ts | 1 + .../options.json | 3 + .../output.json | 43 +++++++++++++++ .../input.ts | 1 + .../options.json | 3 + .../output.json | 39 +++++++++++++ .../input.ts | 1 + .../options.json | 3 + .../output.json | 43 +++++++++++++++ 19 files changed, 306 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index b0e72093ab..56e3efc99f 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2701,6 +2701,8 @@ export default (superClass: Class): Class => return super.toAssignable(this.typeCastToParameter(node), isLHS); case "TSParameterProperty": return super.toAssignable(node, isLHS); + case "ParenthesizedExpression": + return this.toAssignableParenthesizedExpression(node, isLHS); case "TSAsExpression": case "TSNonNullExpression": case "TSTypeAssertion": @@ -2711,6 +2713,19 @@ export default (superClass: Class): Class => } } + toAssignableParenthesizedExpression(node: N.Node, isLHS: boolean) { + switch (node.expression.type) { + case "TSAsExpression": + case "TSNonNullExpression": + case "TSTypeAssertion": + case "ParenthesizedExpression": + node.expression = this.toAssignable(node.expression, isLHS); + return node; + default: + return super.toAssignable(node, isLHS); + } + } + checkLVal( expr: N.Expression, contextDescription: string, diff --git a/packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/input.ts b/packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/input.ts new file mode 100644 index 0000000000..3de0602808 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/input.ts @@ -0,0 +1 @@ +({}) = x diff --git a/packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/options.json b/packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/options.json new file mode 100644 index 0000000000..0861962d88 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/options.json @@ -0,0 +1,3 @@ +{ + "createParenthesizedExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/output.json b/packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/output.json new file mode 100644 index 0000000000..69e8d31055 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/output.json @@ -0,0 +1,39 @@ +{ + "type": "File", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "errors": [ + "SyntaxError: Invalid parenthesized assignment pattern (1:0)" + ], + "program": { + "type": "Program", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "expression": { + "type": "AssignmentExpression", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "operator": "=", + "left": { + "type": "ParenthesizedExpression", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "expression": { + "type": "ObjectPattern", + "start":1,"end":3,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":3}}, + "properties": [] + } + }, + "right": { + "type": "Identifier", + "start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"x"}, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/input.ts new file mode 100644 index 0000000000..1b43aa2f0b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/input.ts @@ -0,0 +1 @@ +((a as any) as string) = null diff --git a/packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/options.json b/packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/options.json new file mode 100644 index 0000000000..0861962d88 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/options.json @@ -0,0 +1,3 @@ +{ + "createParenthesizedExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/output.json b/packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/output.json new file mode 100644 index 0000000000..e9f0308f1a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/nested-parenthesized-assert-and-assign/output.json @@ -0,0 +1,55 @@ +{ + "type": "File", + "start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}}, + "program": { + "type": "Program", + "start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}}, + "expression": { + "type": "AssignmentExpression", + "start":0,"end":29,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":29}}, + "operator": "=", + "left": { + "type": "ParenthesizedExpression", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}}, + "expression": { + "type": "TSAsExpression", + "start":1,"end":21,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":21}}, + "expression": { + "type": "ParenthesizedExpression", + "start":1,"end":11,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":11}}, + "expression": { + "type": "TSAsExpression", + "start":2,"end":10,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":10}}, + "expression": { + "type": "Identifier", + "start":2,"end":3,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":3},"identifierName":"a"}, + "name": "a" + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}} + } + } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start":15,"end":21,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":21}} + } + } + }, + "right": { + "type": "NullLiteral", + "start":25,"end":29,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":29}} + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/input.ts new file mode 100644 index 0000000000..f5f2dc3a49 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/input.ts @@ -0,0 +1 @@ +(a as number) = 42; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/options.json b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/options.json new file mode 100644 index 0000000000..0861962d88 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/options.json @@ -0,0 +1,3 @@ +{ + "createParenthesizedExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/output.json b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/output.json new file mode 100644 index 0000000000..f956864c2f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/output.json @@ -0,0 +1,48 @@ +{ + "type": "File", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "program": { + "type": "Program", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, + "expression": { + "type": "AssignmentExpression", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, + "operator": "=", + "left": { + "type": "ParenthesizedExpression", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}}, + "expression": { + "type": "TSAsExpression", + "start":1,"end":12,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":12}}, + "expression": { + "type": "Identifier", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"a"}, + "name": "a" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start":6,"end":12,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":12}} + } + } + }, + "right": { + "type": "NumericLiteral", + "start":16,"end":18,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":18}}, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/input.ts new file mode 100644 index 0000000000..77a751318c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/input.ts @@ -0,0 +1 @@ +(a as any) = null diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/options.json b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/options.json new file mode 100644 index 0000000000..0861962d88 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/options.json @@ -0,0 +1,3 @@ +{ + "createParenthesizedExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/output.json b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/output.json new file mode 100644 index 0000000000..60758859d5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/output.json @@ -0,0 +1,43 @@ +{ + "type": "File", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "program": { + "type": "Program", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "expression": { + "type": "AssignmentExpression", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "operator": "=", + "left": { + "type": "ParenthesizedExpression", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "expression": { + "type": "TSAsExpression", + "start":1,"end":9,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":9}}, + "expression": { + "type": "Identifier", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"a"}, + "name": "a" + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9}} + } + } + }, + "right": { + "type": "NullLiteral", + "start":13,"end":17,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":17}} + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/input.ts new file mode 100644 index 0000000000..43b45b9d1a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/input.ts @@ -0,0 +1 @@ +(x!) = null; diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/options.json b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/options.json new file mode 100644 index 0000000000..0861962d88 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/options.json @@ -0,0 +1,3 @@ +{ + "createParenthesizedExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/output.json b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/output.json new file mode 100644 index 0000000000..26b30ca535 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-null-assertion-and-assign/output.json @@ -0,0 +1,39 @@ +{ + "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": "module", + "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": "ParenthesizedExpression", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "expression": { + "type": "TSNonNullExpression", + "start":1,"end":3,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":3}}, + "expression": { + "type": "Identifier", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"x"}, + "name": "x" + } + } + }, + "right": { + "type": "NullLiteral", + "start":7,"end":11,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":11}} + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/input.ts new file mode 100644 index 0000000000..35383cdf22 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/input.ts @@ -0,0 +1 @@ +(x) = null diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/options.json b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/options.json new file mode 100644 index 0000000000..0861962d88 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/options.json @@ -0,0 +1,3 @@ +{ + "createParenthesizedExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/output.json b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/output.json new file mode 100644 index 0000000000..b5be66b2ed --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parenthesized-type-assertion-and-assign/output.json @@ -0,0 +1,43 @@ +{ + "type": "File", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, + "program": { + "type": "Program", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, + "expression": { + "type": "AssignmentExpression", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, + "operator": "=", + "left": { + "type": "ParenthesizedExpression", + "start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}}, + "expression": { + "type": "TSTypeAssertion", + "start":1,"end":10,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":10}}, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start":2,"end":8,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":8}} + }, + "expression": { + "type": "Identifier", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"x"}, + "name": "x" + } + } + }, + "right": { + "type": "NullLiteral", + "start":14,"end":18,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":18}} + } + } + } + ], + "directives": [] + } +} \ No newline at end of file