Skip to content

Commit

Permalink
TypeScript 4.0: Allow spread in the middle of tuples (#11753)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored and JLHwung committed Jul 29, 2020
1 parent d7347fb commit 5b4b3a3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
11 changes: 2 additions & 9 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -630,9 +630,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
/* skipFirstToken */ false,
);

// Validate the elementTypes to ensure:
// No mandatory elements may follow optional elements
// If there's a rest element, it must be at the end of the tuple
// Validate the elementTypes to ensure that no mandatory elements
// follow optional elements
let seenOptionalElement = false;
node.elementTypes.forEach(elementNode => {
if (elementNode.type === "TSOptionalType") {
Expand All @@ -651,12 +650,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const restNode: N.TsRestType = this.startNode();
this.next(); // skips ellipsis
restNode.typeAnnotation = this.tsParseType();
if (
this.match(tt.comma) &&
this.lookaheadCharCode() !== charCodes.rightSquareBracket
) {
this.raiseRestNotLast(this.state.start);
}
return this.finishNode(restNode, "TSRestType");
}

Expand Down

This file was deleted.

This file was deleted.

@@ -0,0 +1 @@
let x: [...[number, string], string]
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript"]
}
@@ -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": []
}
}

0 comments on commit 5b4b3a3

Please sign in to comment.