TypeScript 4.0: Allow spread in the middle of tuples (#11753)
This commit is contained in:
parent
d7347fb8bd
commit
5b4b3a3e4a
@ -630,9 +630,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
/* skipFirstToken */ false,
|
/* skipFirstToken */ false,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Validate the elementTypes to ensure:
|
// Validate the elementTypes to ensure that no mandatory elements
|
||||||
// No mandatory elements may follow optional elements
|
// follow optional elements
|
||||||
// If there's a rest element, it must be at the end of the tuple
|
|
||||||
let seenOptionalElement = false;
|
let seenOptionalElement = false;
|
||||||
node.elementTypes.forEach(elementNode => {
|
node.elementTypes.forEach(elementNode => {
|
||||||
if (elementNode.type === "TSOptionalType") {
|
if (elementNode.type === "TSOptionalType") {
|
||||||
@ -651,12 +650,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
const restNode: N.TsRestType = this.startNode();
|
const restNode: N.TsRestType = this.startNode();
|
||||||
this.next(); // skips ellipsis
|
this.next(); // skips ellipsis
|
||||||
restNode.typeAnnotation = this.tsParseType();
|
restNode.typeAnnotation = this.tsParseType();
|
||||||
if (
|
|
||||||
this.match(tt.comma) &&
|
|
||||||
this.lookaheadCharCode() !== charCodes.rightSquareBracket
|
|
||||||
) {
|
|
||||||
this.raiseRestNotLast(this.state.start);
|
|
||||||
}
|
|
||||||
return this.finishNode(restNode, "TSRestType");
|
return this.finishNode(restNode, "TSRestType");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
let x: [...number[], string]
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
{
|
|
||||||
"sourceType": "module",
|
|
||||||
"plugins": ["typescript"],
|
|
||||||
"throws": "Rest element must be last element (1:19)"
|
|
||||||
}
|
|
||||||
1
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/input.ts
vendored
Normal file
1
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
let x: [...[number, string], string]
|
||||||
4
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/options.json
vendored
Normal file
4
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["typescript"]
|
||||||
|
}
|
||||||
62
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/output.json
vendored
Normal file
62
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-not-last/output.json
vendored
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
|
||||||
|
"sourceType": "module",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclaration",
|
||||||
|
"start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":36}},
|
||||||
|
"declarations": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclarator",
|
||||||
|
"start":4,"end":36,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":36}},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start":4,"end":36,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":36},"identifierName":"x"},
|
||||||
|
"name": "x",
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTypeAnnotation",
|
||||||
|
"start":5,"end":36,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":36}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTupleType",
|
||||||
|
"start":7,"end":36,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":36}},
|
||||||
|
"elementTypes": [
|
||||||
|
{
|
||||||
|
"type": "TSRestType",
|
||||||
|
"start":8,"end":27,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":27}},
|
||||||
|
"typeAnnotation": {
|
||||||
|
"type": "TSTupleType",
|
||||||
|
"start":11,"end":27,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":27}},
|
||||||
|
"elementTypes": [
|
||||||
|
{
|
||||||
|
"type": "TSNumberKeyword",
|
||||||
|
"start":12,"end":18,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":18}}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "TSStringKeyword",
|
||||||
|
"start":20,"end":26,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":26}}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "TSStringKeyword",
|
||||||
|
"start":29,"end":35,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":35}}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"init": null
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kind": "let"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user