fix(ts): Allow parenthesized "assert and assign" (#12933)
Co-authored-by: Nicolò Ribaudo <7000710+nicolo-ribaudo@users.noreply.github.com>
This commit is contained in:
parent
69e22c325d
commit
4f727139ec
@ -2701,6 +2701,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
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<Parser>): Class<Parser> =>
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
1
packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
({}) = x
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"createParenthesizedExpressions": true
|
||||
}
|
||||
39
packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/output.json
vendored
Normal file
39
packages/babel-parser/test/fixtures/typescript/assign/parenthesized-object-assign/output.json
vendored
Normal file
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
((a as any) as string) = null
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"createParenthesizedExpressions": true
|
||||
}
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
(a as number) = 42;
|
||||
3
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"createParenthesizedExpressions": true
|
||||
}
|
||||
48
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/output.json
vendored
Normal file
48
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-as-and-assign/output.json
vendored
Normal file
@ -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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
(a as any) = null
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"createParenthesizedExpressions": true
|
||||
}
|
||||
43
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/output.json
vendored
Normal file
43
packages/babel-parser/test/fixtures/typescript/cast/parenthesized-assert-and-assign/output.json
vendored
Normal file
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
(x!) = null;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"createParenthesizedExpressions": true
|
||||
}
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
(<number>x) = null
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"createParenthesizedExpressions": true
|
||||
}
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user