Allow TypeScript type assertions in array destructuring (#10592)
* Add test * Add fix * Fix test, destructure with as assertion * Add angle-bracket assertion case * Use isBinding to make typeCastToParameter decision
This commit is contained in:
parent
b2767c7d8a
commit
4cb5e0a013
@ -2483,7 +2483,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
}
|
||||
|
||||
toAssignableList(exprList: N.Expression[]): $ReadOnlyArray<N.Pattern> {
|
||||
toAssignableList(
|
||||
exprList: N.Expression[],
|
||||
isBinding: ?boolean,
|
||||
): $ReadOnlyArray<N.Pattern> {
|
||||
for (let i = 0; i < exprList.length; i++) {
|
||||
const expr = exprList[i];
|
||||
if (!expr) continue;
|
||||
@ -2493,10 +2496,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
break;
|
||||
case "TSAsExpression":
|
||||
case "TSTypeAssertion":
|
||||
if (!isBinding) {
|
||||
exprList[i] = this.typeCastToParameter(expr);
|
||||
} else {
|
||||
this.raise(
|
||||
expr.start,
|
||||
"Unexpected type cast in parameter position.",
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
2
packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts
vendored
Normal file
2
packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/input.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
[a as number] = [42];
|
||||
[<number>a] = [42];
|
||||
269
packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json
vendored
Normal file
269
packages/babel-parser/test/fixtures/typescript/cast/destructure-and-assign/output.json
vendored
Normal file
@ -0,0 +1,269 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 41,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 19
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 41,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 19
|
||||
}
|
||||
},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 21,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 21
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "AssignmentExpression",
|
||||
"start": 0,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"operator": "=",
|
||||
"left": {
|
||||
"type": "ArrayPattern",
|
||||
"start": 0,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"elements": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start": 1,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
},
|
||||
"identifierName": "a"
|
||||
},
|
||||
"name": "a",
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
"start": 6,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"right": {
|
||||
"type": "ArrayExpression",
|
||||
"start": 16,
|
||||
"end": 20,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 16
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 20
|
||||
}
|
||||
},
|
||||
"elements": [
|
||||
{
|
||||
"type": "NumericLiteral",
|
||||
"start": 17,
|
||||
"end": 19,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 17
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 19
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": 42,
|
||||
"raw": "42"
|
||||
},
|
||||
"value": 42
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 22,
|
||||
"end": 41,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 19
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "AssignmentExpression",
|
||||
"start": 22,
|
||||
"end": 40,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"operator": "=",
|
||||
"left": {
|
||||
"type": "ArrayPattern",
|
||||
"start": 22,
|
||||
"end": 33,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 11
|
||||
}
|
||||
},
|
||||
"elements": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start": 31,
|
||||
"end": 30,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 9
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 8
|
||||
},
|
||||
"identifierName": "a"
|
||||
},
|
||||
"name": "a",
|
||||
"typeAnnotation": {
|
||||
"type": "TSNumberKeyword",
|
||||
"start": 24,
|
||||
"end": 30,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 8
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"right": {
|
||||
"type": "ArrayExpression",
|
||||
"start": 36,
|
||||
"end": 40,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 14
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"elements": [
|
||||
{
|
||||
"type": "NumericLiteral",
|
||||
"start": 37,
|
||||
"end": 39,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 15
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 17
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": 42,
|
||||
"raw": "42"
|
||||
},
|
||||
"value": 42
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user