New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ts] Allow keywords in tuple labels #15423
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06c48c0
[ts] fix: keywords in labeled tuples (#13702)
Harpica f5465ea
fix: babel 8 tests
Harpica 1026aa3
fix: types could be treated as identif. in tuples
Harpica 6f3897d
refactor: raise error and add comments
Harpica 73503f5
Review by JLHwung
nicolo-ribaudo d50e5c7
Recover from `[name:type?]`
nicolo-ribaudo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions
5
packages/babel-parser/test/fixtures/typescript/types/tuple-keyword-labeled-babel-7/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,5 @@ | ||
type FuncWithDescriptionBabel7 = [ | ||
function: (...args: any[]) => any, | ||
string: string, | ||
void?: number | ||
] |
3 changes: 3 additions & 0 deletions
3
...es/babel-parser/test/fixtures/typescript/types/tuple-keyword-labeled-babel-7/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,3 @@ | ||
{ | ||
"BABEL_8_BREAKING": false | ||
} |
101 changes: 101 additions & 0 deletions
101
...ges/babel-parser/test/fixtures/typescript/types/tuple-keyword-labeled-babel-7/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,101 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":107,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":107}}, | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":107,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":107}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start":0,"end":107,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":107}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":5,"end":30,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":30,"index":30},"identifierName":"FuncWithDescriptionBabel7"}, | ||
"name": "FuncWithDescriptionBabel7" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTupleType", | ||
"start":33,"end":107,"loc":{"start":{"line":1,"column":33,"index":33},"end":{"line":5,"column":1,"index":107}}, | ||
"elementTypes": [ | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":37,"end":70,"loc":{"start":{"line":2,"column":2,"index":37},"end":{"line":2,"column":35,"index":70}}, | ||
"optional": false, | ||
"label": { | ||
"type": "Identifier", | ||
"start":37,"end":45,"loc":{"start":{"line":2,"column":2,"index":37},"end":{"line":2,"column":10,"index":45},"identifierName":"function"}, | ||
"name": "function" | ||
}, | ||
"elementType": { | ||
"type": "TSFunctionType", | ||
"start":47,"end":70,"loc":{"start":{"line":2,"column":12,"index":47},"end":{"line":2,"column":35,"index":70}}, | ||
"parameters": [ | ||
{ | ||
"type": "RestElement", | ||
"start":48,"end":62,"loc":{"start":{"line":2,"column":13,"index":48},"end":{"line":2,"column":27,"index":62}}, | ||
"argument": { | ||
"type": "Identifier", | ||
"start":51,"end":55,"loc":{"start":{"line":2,"column":16,"index":51},"end":{"line":2,"column":20,"index":55},"identifierName":"args"}, | ||
"name": "args" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":55,"end":62,"loc":{"start":{"line":2,"column":20,"index":55},"end":{"line":2,"column":27,"index":62}}, | ||
"typeAnnotation": { | ||
"type": "TSArrayType", | ||
"start":57,"end":62,"loc":{"start":{"line":2,"column":22,"index":57},"end":{"line":2,"column":27,"index":62}}, | ||
"elementType": { | ||
"type": "TSAnyKeyword", | ||
"start":57,"end":60,"loc":{"start":{"line":2,"column":22,"index":57},"end":{"line":2,"column":25,"index":60}} | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":64,"end":70,"loc":{"start":{"line":2,"column":29,"index":64},"end":{"line":2,"column":35,"index":70}}, | ||
"typeAnnotation": { | ||
"type": "TSAnyKeyword", | ||
"start":67,"end":70,"loc":{"start":{"line":2,"column":32,"index":67},"end":{"line":2,"column":35,"index":70}} | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":74,"end":88,"loc":{"start":{"line":3,"column":2,"index":74},"end":{"line":3,"column":16,"index":88}}, | ||
"optional": false, | ||
"label": { | ||
"type": "Identifier", | ||
"start":74,"end":80,"loc":{"start":{"line":3,"column":2,"index":74},"end":{"line":3,"column":8,"index":80},"identifierName":"string"}, | ||
"name": "string" | ||
}, | ||
"elementType": { | ||
"type": "TSStringKeyword", | ||
"start":82,"end":88,"loc":{"start":{"line":3,"column":10,"index":82},"end":{"line":3,"column":16,"index":88}} | ||
} | ||
}, | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":92,"end":105,"loc":{"start":{"line":4,"column":2,"index":92},"end":{"line":4,"column":15,"index":105}}, | ||
"optional": true, | ||
"label": { | ||
"type": "Identifier", | ||
"start":92,"end":96,"loc":{"start":{"line":4,"column":2,"index":92},"end":{"line":4,"column":6,"index":96},"identifierName":"void"}, | ||
"name": "void" | ||
}, | ||
"elementType": { | ||
"type": "TSNumberKeyword", | ||
"start":99,"end":105,"loc":{"start":{"line":4,"column":9,"index":99},"end":{"line":4,"column":15,"index":105}} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-parser/test/fixtures/typescript/types/tuple-keyword-labeled/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,5 @@ | ||
type FuncWithDescription = [ | ||
function: (...args: any[]) => any, | ||
string: string, | ||
void?: number | ||
] |
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/typescript/types/tuple-keyword-labeled/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,3 @@ | ||
{ | ||
"BABEL_8_BREAKING": true | ||
} |
101 changes: 101 additions & 0 deletions
101
packages/babel-parser/test/fixtures/typescript/types/tuple-keyword-labeled/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,101 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":101,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":101}}, | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":101,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":101}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start":0,"end":101,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":5,"column":1,"index":101}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":5,"end":24,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":24,"index":24},"identifierName":"FuncWithDescription"}, | ||
"name": "FuncWithDescription" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTupleType", | ||
"start":27,"end":101,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":5,"column":1,"index":101}}, | ||
"elementTypes": [ | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":31,"end":64,"loc":{"start":{"line":2,"column":2,"index":31},"end":{"line":2,"column":35,"index":64}}, | ||
"optional": false, | ||
"label": { | ||
"type": "Identifier", | ||
"start":31,"end":39,"loc":{"start":{"line":2,"column":2,"index":31},"end":{"line":2,"column":10,"index":39},"identifierName":"function"}, | ||
"name": "function" | ||
}, | ||
"elementType": { | ||
"type": "TSFunctionType", | ||
"start":41,"end":64,"loc":{"start":{"line":2,"column":12,"index":41},"end":{"line":2,"column":35,"index":64}}, | ||
"params": [ | ||
{ | ||
"type": "RestElement", | ||
"start":42,"end":56,"loc":{"start":{"line":2,"column":13,"index":42},"end":{"line":2,"column":27,"index":56}}, | ||
"argument": { | ||
"type": "Identifier", | ||
"start":45,"end":49,"loc":{"start":{"line":2,"column":16,"index":45},"end":{"line":2,"column":20,"index":49},"identifierName":"args"}, | ||
"name": "args" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":49,"end":56,"loc":{"start":{"line":2,"column":20,"index":49},"end":{"line":2,"column":27,"index":56}}, | ||
"typeAnnotation": { | ||
"type": "TSArrayType", | ||
"start":51,"end":56,"loc":{"start":{"line":2,"column":22,"index":51},"end":{"line":2,"column":27,"index":56}}, | ||
"elementType": { | ||
"type": "TSAnyKeyword", | ||
"start":51,"end":54,"loc":{"start":{"line":2,"column":22,"index":51},"end":{"line":2,"column":25,"index":54}} | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"returnType": { | ||
"type": "TSTypeAnnotation", | ||
"start":58,"end":64,"loc":{"start":{"line":2,"column":29,"index":58},"end":{"line":2,"column":35,"index":64}}, | ||
"typeAnnotation": { | ||
"type": "TSAnyKeyword", | ||
"start":61,"end":64,"loc":{"start":{"line":2,"column":32,"index":61},"end":{"line":2,"column":35,"index":64}} | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":68,"end":82,"loc":{"start":{"line":3,"column":2,"index":68},"end":{"line":3,"column":16,"index":82}}, | ||
"optional": false, | ||
"label": { | ||
"type": "Identifier", | ||
"start":68,"end":74,"loc":{"start":{"line":3,"column":2,"index":68},"end":{"line":3,"column":8,"index":74},"identifierName":"string"}, | ||
"name": "string" | ||
}, | ||
"elementType": { | ||
"type": "TSStringKeyword", | ||
"start":76,"end":82,"loc":{"start":{"line":3,"column":10,"index":76},"end":{"line":3,"column":16,"index":82}} | ||
} | ||
}, | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":86,"end":99,"loc":{"start":{"line":4,"column":2,"index":86},"end":{"line":4,"column":15,"index":99}}, | ||
"optional": true, | ||
"label": { | ||
"type": "Identifier", | ||
"start":86,"end":90,"loc":{"start":{"line":4,"column":2,"index":86},"end":{"line":4,"column":6,"index":90},"identifierName":"void"}, | ||
"name": "void" | ||
}, | ||
"elementType": { | ||
"type": "TSNumberKeyword", | ||
"start":93,"end":99,"loc":{"start":{"line":4,"column":9,"index":93},"end":{"line":4,"column":15,"index":99}} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,5 @@ | |
"sourceType": "module", | ||
"plugins": [ | ||
"typescript" | ||
], | ||
"throws": "Unexpected token, expected \",\" (1:14)" | ||
} | ||
] | ||
} |
50 changes: 50 additions & 0 deletions
50
...es/babel-parser/test/fixtures/typescript/types/tuple-labeled-invalid-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,50 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, | ||
"errors": [ | ||
"SyntaxError: A labeled tuple optional element must be declared using a question mark after the name and before the colon (`name?: type`), rather than after the type (`name: type?`). (1:14)" | ||
], | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":5,"end":6,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":6,"index":6},"identifierName":"T"}, | ||
"name": "T" | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTupleType", | ||
"start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16}}, | ||
"elementTypes": [ | ||
{ | ||
"type": "TSNamedTupleMember", | ||
"start":10,"end":15,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":15,"index":15}}, | ||
"optional": true, | ||
"label": { | ||
"type": "Identifier", | ||
"start":10,"end":11,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":11,"index":11},"identifierName":"x"}, | ||
"name": "x" | ||
}, | ||
"elementType": { | ||
"type": "TSTypeReference", | ||
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14},"identifierName":"A"}, | ||
"name": "A" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When can this be followed by a colon?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case we have invalid label in tuple followed by colon, which fails
tokenIsKeywordOrIdentifier
check?I'm sorry to bother you, this issue seemed easier than it really is for me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about the case described in "tuple-invalid-label-1" test:
type T = [x.y: A];
which should throw errorTSErrors.InvalidTupleMemberLabel
to be passedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok 👍 Maybe add a comment in the code with that example, explaining that it can only happen with invalid input.
Also, probably we can move the
TSErrors.InvalidTupleMemberLabel
inside this block so that it's throw in the same place as where we parse the invalid code.