Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TypeScript 4.0: Support labeled tuple elements (#11754)
* TypeScript 4.0: Support labeled tuple elements * More tests * Disallow mixing labeled and unlabeled elements * Update AST shape * Enable test after rebase * Allow labeled spread types * Fix flow * Add types and generator suport * Update packages/babel-parser/src/plugins/typescript/index.js * Prettier
- Loading branch information
1 parent
9e6663f
commit eba4c3b
Showing
31 changed files
with
736 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/babel-generator/test/fixtures/typescript/tuple-labeled/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type T = [x: A, y?: B, ...z: C]; |
1 change: 1 addition & 0 deletions
1
packages/babel-generator/test/fixtures/typescript/tuple-labeled/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type T = [x: A, y?: B, ...z: C]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/typescript/types/tuple-invalid-label-1/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type T = [x.y: A]; |
6 changes: 6 additions & 0 deletions
6
packages/babel-parser/test/fixtures/typescript/types/tuple-invalid-label-1/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"sourceType": "module", | ||
"plugins": [ | ||
"typescript" | ||
] | ||
} |
63 changes: 63 additions & 0 deletions
63
packages/babel-parser/test/fixtures/typescript/types/tuple-invalid-label-1/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, | ||
"errors": [ | ||
"SyntaxError: Tuple members must be labeled with a simple identifier. (1:10)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, | ||
"name": "T" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTupleType", | ||
"start":9,"end":17,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":17}}, | ||
"elementTypes": [ | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":10,"end":16,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":16}}, | ||
"optional": false, | ||
"label": { | ||
"type": "TSTypeReference", | ||
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13}}, | ||
"typeName": { | ||
"type": "TSQualifiedName", | ||
"start":10,"end":13,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":13}}, | ||
"left": { | ||
"type": "Identifier", | ||
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"x"}, | ||
"name": "x" | ||
}, | ||
"right": { | ||
"type": "Identifier", | ||
"start":12,"end":13,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":13},"identifierName":"y"}, | ||
"name": "y" | ||
} | ||
} | ||
}, | ||
"elementType": { | ||
"type": "TSTypeReference", | ||
"start":15,"end":16,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":16}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":15,"end":16,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":16},"identifierName":"A"}, | ||
"name": "A" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/typescript/types/tuple-invalid-label-2/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type T = [x<y>: A]; |
6 changes: 6 additions & 0 deletions
6
packages/babel-parser/test/fixtures/typescript/types/tuple-invalid-label-2/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"sourceType": "module", | ||
"plugins": [ | ||
"typescript" | ||
] | ||
} |
69 changes: 69 additions & 0 deletions
69
packages/babel-parser/test/fixtures/typescript/types/tuple-invalid-label-2/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, | ||
"errors": [ | ||
"SyntaxError: Tuple members must be labeled with a simple identifier. (1:10)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, | ||
"name": "T" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTupleType", | ||
"start":9,"end":18,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":18}}, | ||
"elementTypes": [ | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":10,"end":17,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":17}}, | ||
"optional": false, | ||
"label": { | ||
"type": "TSTypeReference", | ||
"start":10,"end":14,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":14}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"x"}, | ||
"name": "x" | ||
}, | ||
"typeParameters": { | ||
"type": "TSTypeParameterInstantiation", | ||
"start":11,"end":14,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":14}}, | ||
"params": [ | ||
{ | ||
"type": "TSTypeReference", | ||
"start":12,"end":13,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":13}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":12,"end":13,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":13},"identifierName":"y"}, | ||
"name": "y" | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"elementType": { | ||
"type": "TSTypeReference", | ||
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17},"identifierName":"A"}, | ||
"name": "A" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/typescript/types/tuple-labeled-after-unlabeled/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type T = [A, y: B]; |
59 changes: 59 additions & 0 deletions
59
...ges/babel-parser/test/fixtures/typescript/types/tuple-labeled-after-unlabeled/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, | ||
"errors": [ | ||
"SyntaxError: Tuple members must all have names or all not have names. (1:13)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":5,"end":6,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":6},"identifierName":"T"}, | ||
"name": "T" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTupleType", | ||
"start":9,"end":18,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":18}}, | ||
"elementTypes": [ | ||
{ | ||
"type": "TSTypeReference", | ||
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"A"}, | ||
"name": "A" | ||
} | ||
}, | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":13,"end":17,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":17}}, | ||
"optional": false, | ||
"label": { | ||
"type": "Identifier", | ||
"start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14},"identifierName":"y"}, | ||
"name": "y" | ||
}, | ||
"elementType": { | ||
"type": "TSTypeReference", | ||
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17},"identifierName":"B"}, | ||
"name": "B" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/typescript/types/tuple-labeled-before-unlabeled/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type T = [x: A, B]; |
Oops, something went wrong.