Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Typescript: Validate tuple type element positions (#8828)
* feat: validate the positions of rest elements and optional elements in tuple types Adds a validation step to the parser which raises syntax errors if a rest param is not at the end of a tuple, or if a mandatory param follows an optional parameter * Fix spread after optional case; add test case
- Loading branch information
Showing
7 changed files
with
282 additions
and
0 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-parser/test/fixtures/typescript/types/tuple-optional-invalid/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 @@ | ||
let x: [string?, number] |
7 changes: 7 additions & 0 deletions
7
packages/babel-parser/test/fixtures/typescript/types/tuple-optional-invalid/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,7 @@ | ||
{ | ||
"sourceType": "module", | ||
"plugins": [ | ||
"typescript" | ||
], | ||
"throws": "A required element cannot follow an optional element. (1:17)" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-after-optional/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 @@ | ||
function foo(...args: [number, string?, ...number[]]) {} |
242 changes: 242 additions & 0 deletions
242
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-after-optional/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,242 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 56, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 56 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 56, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 56 | ||
} | ||
}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "FunctionDeclaration", | ||
"start": 0, | ||
"end": 56, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 56 | ||
} | ||
}, | ||
"id": { | ||
"type": "Identifier", | ||
"start": 9, | ||
"end": 12, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 9 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 12 | ||
}, | ||
"identifierName": "foo" | ||
}, | ||
"name": "foo" | ||
}, | ||
"generator": false, | ||
"async": false, | ||
"params": [ | ||
{ | ||
"type": "RestElement", | ||
"start": 13, | ||
"end": 52, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 13 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 52 | ||
} | ||
}, | ||
"argument": { | ||
"type": "Identifier", | ||
"start": 16, | ||
"end": 20, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 16 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 20 | ||
}, | ||
"identifierName": "args" | ||
}, | ||
"name": "args" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start": 20, | ||
"end": 52, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 20 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 52 | ||
} | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTupleType", | ||
"start": 22, | ||
"end": 52, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 22 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 52 | ||
} | ||
}, | ||
"elementTypes": [ | ||
{ | ||
"type": "TSNumberKeyword", | ||
"start": 23, | ||
"end": 29, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 23 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 29 | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "TSOptionalType", | ||
"start": 31, | ||
"end": 38, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 31 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 38 | ||
} | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSStringKeyword", | ||
"start": 31, | ||
"end": 37, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 31 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 37 | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "TSRestType", | ||
"start": 40, | ||
"end": 51, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 40 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 51 | ||
} | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSArrayType", | ||
"start": 43, | ||
"end": 51, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 43 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 51 | ||
} | ||
}, | ||
"elementType": { | ||
"type": "TSNumberKeyword", | ||
"start": 43, | ||
"end": 49, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 43 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 49 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"body": { | ||
"type": "BlockStatement", | ||
"start": 54, | ||
"end": 56, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 54 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 56 | ||
} | ||
}, | ||
"body": [], | ||
"directives": [] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-invalid/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 @@ | ||
let x: [...number[], string] |
7 changes: 7 additions & 0 deletions
7
packages/babel-parser/test/fixtures/typescript/types/tuple-rest-invalid/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,7 @@ | ||
{ | ||
"sourceType": "module", | ||
"plugins": [ | ||
"typescript" | ||
], | ||
"throws": "A rest element must be last in a tuple type. (1:8)" | ||
} |