fix: raise SyntaxError for unparenthesized assert and assign (#13099)
* fix: raise `SyntaxError` for unparenthesized assert and assign * chore
This commit is contained in:
parent
7bc72bb451
commit
7fe3ebf4db
@ -2589,7 +2589,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
expr.type !== "ArrowFunctionExpression" ||
|
expr.type !== "ArrowFunctionExpression" ||
|
||||||
(expr.extra && expr.extra.parenthesized)
|
expr.extra?.parenthesized
|
||||||
) {
|
) {
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
@ -2743,8 +2743,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
this.checkLVal(expr.parameter, "parameter property", ...args);
|
this.checkLVal(expr.parameter, "parameter property", ...args);
|
||||||
return;
|
return;
|
||||||
case "TSAsExpression":
|
case "TSAsExpression":
|
||||||
case "TSNonNullExpression":
|
|
||||||
case "TSTypeAssertion":
|
case "TSTypeAssertion":
|
||||||
|
if (
|
||||||
|
/*bindingType*/ !args[0] &&
|
||||||
|
contextDescription !== "parenthesized expression" &&
|
||||||
|
!expr.extra?.parenthesized
|
||||||
|
) {
|
||||||
|
this.raise(expr.start, Errors.InvalidLhs, contextDescription);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
this.checkLVal(expr.expression, "parenthesized expression", ...args);
|
||||||
|
return;
|
||||||
|
case "TSNonNullExpression":
|
||||||
this.checkLVal(expr.expression, contextDescription, ...args);
|
this.checkLVal(expr.expression, contextDescription, ...args);
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -1 +1,2 @@
|
|||||||
(a as number) = 42;
|
(a as number) = 42;
|
||||||
|
({ a: (b as any) = 2000 } = x);
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"type": "File",
|
"type": "File",
|
||||||
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
|
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":31}},
|
||||||
"program": {
|
"program": {
|
||||||
"type": "Program",
|
"type": "Program",
|
||||||
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
|
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":31}},
|
||||||
"sourceType": "module",
|
"sourceType": "module",
|
||||||
"interpreter": null,
|
"interpreter": null,
|
||||||
"body": [
|
"body": [
|
||||||
@ -17,6 +17,10 @@
|
|||||||
"left": {
|
"left": {
|
||||||
"type": "TSAsExpression",
|
"type": "TSAsExpression",
|
||||||
"start":1,"end":12,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":12}},
|
"start":1,"end":12,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":12}},
|
||||||
|
"extra": {
|
||||||
|
"parenthesized": true,
|
||||||
|
"parenStart": 0
|
||||||
|
},
|
||||||
"expression": {
|
"expression": {
|
||||||
"type": "Identifier",
|
"type": "Identifier",
|
||||||
"start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"a"},
|
"start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"a"},
|
||||||
@ -25,10 +29,6 @@
|
|||||||
"typeAnnotation": {
|
"typeAnnotation": {
|
||||||
"type": "TSNumberKeyword",
|
"type": "TSNumberKeyword",
|
||||||
"start":6,"end":12,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":12}}
|
"start":6,"end":12,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":12}}
|
||||||
},
|
|
||||||
"extra": {
|
|
||||||
"parenthesized": true,
|
|
||||||
"parenStart": 0
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"right": {
|
"right": {
|
||||||
@ -41,6 +41,72 @@
|
|||||||
"value": 42
|
"value": 42
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ExpressionStatement",
|
||||||
|
"start":20,"end":51,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":31}},
|
||||||
|
"expression": {
|
||||||
|
"type": "AssignmentExpression",
|
||||||
|
"start":21,"end":49,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":29}},
|
||||||
|
"extra": {
|
||||||
|
"parenthesized": true,
|
||||||
|
"parenStart": 20
|
||||||
|
},
|
||||||
|
"operator": "=",
|
||||||
|
"left": {
|
||||||
|
"type": "ObjectPattern",
|
||||||
|
"start":21,"end":45,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":25}},
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"type": "ObjectProperty",
|
||||||
|
"start":23,"end":43,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":23}},
|
||||||
|
"method": false,
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":23,"end":24,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4},"identifierName":"a"},
|
||||||
|
"name": "a"
|
||||||
|
},
|
||||||
|
"computed": false,
|
||||||
|
"shorthand": false,
|
||||||
|
"value": {
|
||||||
|
"type": "AssignmentPattern",
|
||||||
|
"start":26,"end":43,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":23}},
|
||||||
|
"left": {
|
||||||
|
"type": "TSAsExpression",
|
||||||
|
"start":27,"end":35,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":15}},
|
||||||
|
"extra": {
|
||||||
|
"parenthesized": true,
|
||||||
|
"parenStart": 26
|
||||||
|
},
|
||||||
|
"expression": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":27,"end":28,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8},"identifierName":"b"},
|
||||||
|
"name": "b"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSAnyKeyword",
|
||||||
|
"start":32,"end":35,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":15}}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"right": {
|
||||||
|
"type": "NumericLiteral",
|
||||||
|
"start":39,"end":43,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":23}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": 2000,
|
||||||
|
"raw": "2000"
|
||||||
|
},
|
||||||
|
"value": 2000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"right": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":48,"end":49,"loc":{"start":{"line":2,"column":28},"end":{"line":2,"column":29},"identifierName":"x"},
|
||||||
|
"name": "x"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"directives": []
|
"directives": []
|
||||||
|
|||||||
@ -17,6 +17,10 @@
|
|||||||
"left": {
|
"left": {
|
||||||
"type": "TSAsExpression",
|
"type": "TSAsExpression",
|
||||||
"start":1,"end":19,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":19}},
|
"start":1,"end":19,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":19}},
|
||||||
|
"extra": {
|
||||||
|
"parenthesized": true,
|
||||||
|
"parenStart": 0
|
||||||
|
},
|
||||||
"expression": {
|
"expression": {
|
||||||
"type": "TSAsExpression",
|
"type": "TSAsExpression",
|
||||||
"start":1,"end":12,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":12}},
|
"start":1,"end":12,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":12}},
|
||||||
@ -33,10 +37,6 @@
|
|||||||
"typeAnnotation": {
|
"typeAnnotation": {
|
||||||
"type": "TSAnyKeyword",
|
"type": "TSAnyKeyword",
|
||||||
"start":16,"end":19,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":19}}
|
"start":16,"end":19,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":19}}
|
||||||
},
|
|
||||||
"extra": {
|
|
||||||
"parenthesized": true,
|
|
||||||
"parenStart": 0
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"right": {
|
"right": {
|
||||||
|
|||||||
@ -17,6 +17,10 @@
|
|||||||
"left": {
|
"left": {
|
||||||
"type": "TSTypeAssertion",
|
"type": "TSTypeAssertion",
|
||||||
"start":1,"end":11,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":11}},
|
"start":1,"end":11,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":11}},
|
||||||
|
"extra": {
|
||||||
|
"parenthesized": true,
|
||||||
|
"parenStart": 0
|
||||||
|
},
|
||||||
"typeAnnotation": {
|
"typeAnnotation": {
|
||||||
"type": "TSNumberKeyword",
|
"type": "TSNumberKeyword",
|
||||||
"start":2,"end":8,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":8}}
|
"start":2,"end":8,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":8}}
|
||||||
@ -25,10 +29,6 @@
|
|||||||
"type": "Identifier",
|
"type": "Identifier",
|
||||||
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"x"},
|
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"x"},
|
||||||
"name": "x"
|
"name": "x"
|
||||||
},
|
|
||||||
"extra": {
|
|
||||||
"parenthesized": true,
|
|
||||||
"parenStart": 0
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"right": {
|
"right": {
|
||||||
|
|||||||
@ -0,0 +1,2 @@
|
|||||||
|
foo as any = 10;
|
||||||
|
({ a: b as any = 2000 } = x);
|
||||||
110
packages/babel-parser/test/fixtures/typescript/cast/unparenthesized-assert-and-assign/output.json
vendored
Normal file
110
packages/babel-parser/test/fixtures/typescript/cast/unparenthesized-assert-and-assign/output.json
vendored
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":29}},
|
||||||
|
"errors": [
|
||||||
|
"SyntaxError: Invalid left-hand side in assignment expression (1:0)",
|
||||||
|
"SyntaxError: Invalid left-hand side in assignment expression (2:6)"
|
||||||
|
],
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":29}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ExpressionStatement",
|
||||||
|
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
|
||||||
|
"expression": {
|
||||||
|
"type": "AssignmentExpression",
|
||||||
|
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
|
||||||
|
"operator": "=",
|
||||||
|
"left": {
|
||||||
|
"type": "TSAsExpression",
|
||||||
|
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
|
||||||
|
"expression": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3},"identifierName":"foo"},
|
||||||
|
"name": "foo"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSAnyKeyword",
|
||||||
|
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"right": {
|
||||||
|
"type": "NumericLiteral",
|
||||||
|
"start":13,"end":15,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":15}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": 10,
|
||||||
|
"raw": "10"
|
||||||
|
},
|
||||||
|
"value": 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ExpressionStatement",
|
||||||
|
"start":17,"end":46,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":29}},
|
||||||
|
"expression": {
|
||||||
|
"type": "AssignmentExpression",
|
||||||
|
"start":18,"end":44,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":27}},
|
||||||
|
"extra": {
|
||||||
|
"parenthesized": true,
|
||||||
|
"parenStart": 17
|
||||||
|
},
|
||||||
|
"operator": "=",
|
||||||
|
"left": {
|
||||||
|
"type": "ObjectPattern",
|
||||||
|
"start":18,"end":40,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":23}},
|
||||||
|
"properties": [
|
||||||
|
{
|
||||||
|
"type": "ObjectProperty",
|
||||||
|
"start":20,"end":38,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":21}},
|
||||||
|
"method": false,
|
||||||
|
"key": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":20,"end":21,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4},"identifierName":"a"},
|
||||||
|
"name": "a"
|
||||||
|
},
|
||||||
|
"computed": false,
|
||||||
|
"shorthand": false,
|
||||||
|
"value": {
|
||||||
|
"type": "AssignmentPattern",
|
||||||
|
"start":23,"end":38,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":21}},
|
||||||
|
"left": {
|
||||||
|
"type": "TSAsExpression",
|
||||||
|
"start":23,"end":31,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":14}},
|
||||||
|
"expression": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":23,"end":24,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":7},"identifierName":"b"},
|
||||||
|
"name": "b"
|
||||||
|
},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSAnyKeyword",
|
||||||
|
"start":28,"end":31,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":14}}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"right": {
|
||||||
|
"type": "NumericLiteral",
|
||||||
|
"start":34,"end":38,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":21}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": 2000,
|
||||||
|
"raw": "2000"
|
||||||
|
},
|
||||||
|
"value": 2000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"right": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":43,"end":44,"loc":{"start":{"line":2,"column":26},"end":{"line":2,"column":27},"identifierName":"x"},
|
||||||
|
"name": "x"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1 @@
|
|||||||
|
<string>foo = '100';
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
|
||||||
|
"errors": [
|
||||||
|
"SyntaxError: Invalid left-hand side in assignment expression (1:0)"
|
||||||
|
],
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ExpressionStatement",
|
||||||
|
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
|
||||||
|
"expression": {
|
||||||
|
"type": "AssignmentExpression",
|
||||||
|
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
|
||||||
|
"operator": "=",
|
||||||
|
"left": {
|
||||||
|
"type": "TSTypeAssertion",
|
||||||
|
"start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSStringKeyword",
|
||||||
|
"start":1,"end":7,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":7}}
|
||||||
|
},
|
||||||
|
"expression": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11},"identifierName":"foo"},
|
||||||
|
"name": "foo"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"right": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start":14,"end":19,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":19}},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "100",
|
||||||
|
"raw": "'100'"
|
||||||
|
},
|
||||||
|
"value": "100"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user