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 4.7] Support optional variance annotations #14359
Merged
nicolo-ribaudo
merged 9 commits into
babel:main
from
magic-akari:feat/ts-VarianceAnnotations
May 17, 2022
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ddc567d
feat: support optional variance annotations
magic-akari f30cbf0
fix: flow check
magic-akari c6db874
fix: BABEL_8_BREAKING
magic-akari c13020b
chore: add test cases
magic-akari a626604
fix: TS1274
magic-akari e89330a
fix: more recoverable error
magic-akari a19b618
Merge branch 'main' into feat/ts-VarianceAnnotations
magic-akari 9aa70e2
fix: flowjs type
magic-akari 13cd945
chore: update babel 8 test case
magic-akari 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-generator/test/fixtures/typescript/variance-annotations-with-jsx/input.tsx
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 @@ | ||
// valid JSX | ||
<in T>() => {}</in>; | ||
|
||
// type variance annotations with a trailing comma | ||
<in T,>() => {}; |
4 changes: 4 additions & 0 deletions
4
packages/babel-generator/test/fixtures/typescript/variance-annotations-with-jsx/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,4 @@ | ||
{ | ||
"BABEL_8_BREAKING": false, | ||
"plugins": ["jsx", "typescript"] | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/babel-generator/test/fixtures/typescript/variance-annotations-with-jsx/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,4 @@ | ||
// valid JSX | ||
<in T>() => {}</in>; // type variance annotations with a trailing comma | ||
|
||
<in T,>() => {}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -707,6 +707,8 @@ export type ModuleExpression = NodeBase & { | |
// TypeScript access modifiers | ||
export type Accessibility = "public" | "protected" | "private"; | ||
|
||
export type VarianceAnnotations = "in" | "out"; | ||
|
||
export type PatternBase = HasDecorators & { | ||
// TODO: All not in spec | ||
// Flow/TypeScript only: | ||
|
@@ -1040,6 +1042,8 @@ export type TsTypeParameter = NodeBase & { | |
type: "TSTypeParameter", | ||
// TODO(Babel-8): remove string type support | ||
name: string | Identifier, | ||
in?: boolean, | ||
out?: boolean, | ||
Comment on lines
+1045
to
+1046
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% sold on using boolean flags as it locks us in with naming but I guess that this is the same thing we did with class property modifiers as well so it's not like we're breaking convention in the AST. Given the precedence - this is probably the best approach, so LGTM |
||
constraint?: TsType, | ||
default?: TsType, | ||
}; | ||
|
11 changes: 11 additions & 0 deletions
11
packages/babel-parser/test/fixtures/typescript/types/variance-annotations-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,11 @@ | ||
type Covariant<out T> = { | ||
x: T; | ||
} | ||
|
||
type Contravariant<in T> = { | ||
f: (x: T) => void; | ||
} | ||
|
||
type Invariant<in out T> = { | ||
f: (x: T) => T; | ||
} |
3 changes: 3 additions & 0 deletions
3
...ges/babel-parser/test/fixtures/typescript/types/variance-annotations-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 | ||
} |
215 changes: 215 additions & 0 deletions
215
...ages/babel-parser/test/fixtures/typescript/types/variance-annotations-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,215 @@ | ||
{ | ||
"type": "File", | ||
"start":0,"end":144,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":11,"column":1,"index":144}}, | ||
"program": { | ||
"type": "Program", | ||
"start":0,"end":144,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":11,"column":1,"index":144}}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start":0,"end":37,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":37}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":5,"end":14,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":14,"index":14},"identifierName":"Covariant"}, | ||
"name": "Covariant" | ||
}, | ||
"typeParameters": { | ||
"type": "TSTypeParameterDeclaration", | ||
"start":14,"end":21,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":21,"index":21}}, | ||
"params": [ | ||
{ | ||
"type": "TSTypeParameter", | ||
"start":15,"end":20,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":20,"index":20}}, | ||
"out": true, | ||
"name": "T" | ||
} | ||
] | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTypeLiteral", | ||
"start":24,"end":37,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":3,"column":1,"index":37}}, | ||
"members": [ | ||
{ | ||
"type": "TSPropertySignature", | ||
"start":30,"end":35,"loc":{"start":{"line":2,"column":4,"index":30},"end":{"line":2,"column":9,"index":35}}, | ||
"key": { | ||
"type": "Identifier", | ||
"start":30,"end":31,"loc":{"start":{"line":2,"column":4,"index":30},"end":{"line":2,"column":5,"index":31},"identifierName":"x"}, | ||
"name": "x" | ||
}, | ||
"computed": false, | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":31,"end":34,"loc":{"start":{"line":2,"column":5,"index":31},"end":{"line":2,"column":8,"index":34}}, | ||
"typeAnnotation": { | ||
"type": "TSTypeReference", | ||
"start":33,"end":34,"loc":{"start":{"line":2,"column":7,"index":33},"end":{"line":2,"column":8,"index":34}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":33,"end":34,"loc":{"start":{"line":2,"column":7,"index":33},"end":{"line":2,"column":8,"index":34},"identifierName":"T"}, | ||
"name": "T" | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start":39,"end":92,"loc":{"start":{"line":5,"column":0,"index":39},"end":{"line":7,"column":1,"index":92}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":44,"end":57,"loc":{"start":{"line":5,"column":5,"index":44},"end":{"line":5,"column":18,"index":57},"identifierName":"Contravariant"}, | ||
"name": "Contravariant" | ||
}, | ||
"typeParameters": { | ||
"type": "TSTypeParameterDeclaration", | ||
"start":57,"end":63,"loc":{"start":{"line":5,"column":18,"index":57},"end":{"line":5,"column":24,"index":63}}, | ||
"params": [ | ||
{ | ||
"type": "TSTypeParameter", | ||
"start":58,"end":62,"loc":{"start":{"line":5,"column":19,"index":58},"end":{"line":5,"column":23,"index":62}}, | ||
"in": true, | ||
"name": "T" | ||
} | ||
] | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTypeLiteral", | ||
"start":66,"end":92,"loc":{"start":{"line":5,"column":27,"index":66},"end":{"line":7,"column":1,"index":92}}, | ||
"members": [ | ||
{ | ||
"type": "TSPropertySignature", | ||
"start":72,"end":90,"loc":{"start":{"line":6,"column":4,"index":72},"end":{"line":6,"column":22,"index":90}}, | ||
"key": { | ||
"type": "Identifier", | ||
"start":72,"end":73,"loc":{"start":{"line":6,"column":4,"index":72},"end":{"line":6,"column":5,"index":73},"identifierName":"f"}, | ||
"name": "f" | ||
}, | ||
"computed": false, | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":73,"end":89,"loc":{"start":{"line":6,"column":5,"index":73},"end":{"line":6,"column":21,"index":89}}, | ||
"typeAnnotation": { | ||
"type": "TSFunctionType", | ||
"start":75,"end":89,"loc":{"start":{"line":6,"column":7,"index":75},"end":{"line":6,"column":21,"index":89}}, | ||
"parameters": [ | ||
{ | ||
"type": "Identifier", | ||
"start":76,"end":80,"loc":{"start":{"line":6,"column":8,"index":76},"end":{"line":6,"column":12,"index":80},"identifierName":"x"}, | ||
"name": "x", | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":77,"end":80,"loc":{"start":{"line":6,"column":9,"index":77},"end":{"line":6,"column":12,"index":80}}, | ||
"typeAnnotation": { | ||
"type": "TSTypeReference", | ||
"start":79,"end":80,"loc":{"start":{"line":6,"column":11,"index":79},"end":{"line":6,"column":12,"index":80}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":79,"end":80,"loc":{"start":{"line":6,"column":11,"index":79},"end":{"line":6,"column":12,"index":80},"identifierName":"T"}, | ||
"name": "T" | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":82,"end":89,"loc":{"start":{"line":6,"column":14,"index":82},"end":{"line":6,"column":21,"index":89}}, | ||
"typeAnnotation": { | ||
"type": "TSVoidKeyword", | ||
"start":85,"end":89,"loc":{"start":{"line":6,"column":17,"index":85},"end":{"line":6,"column":21,"index":89}} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"type": "TSTypeAliasDeclaration", | ||
"start":94,"end":144,"loc":{"start":{"line":9,"column":0,"index":94},"end":{"line":11,"column":1,"index":144}}, | ||
"id": { | ||
"type": "Identifier", | ||
"start":99,"end":108,"loc":{"start":{"line":9,"column":5,"index":99},"end":{"line":9,"column":14,"index":108},"identifierName":"Invariant"}, | ||
"name": "Invariant" | ||
}, | ||
"typeParameters": { | ||
"type": "TSTypeParameterDeclaration", | ||
"start":108,"end":118,"loc":{"start":{"line":9,"column":14,"index":108},"end":{"line":9,"column":24,"index":118}}, | ||
"params": [ | ||
{ | ||
"type": "TSTypeParameter", | ||
"start":109,"end":117,"loc":{"start":{"line":9,"column":15,"index":109},"end":{"line":9,"column":23,"index":117}}, | ||
"in": true, | ||
"out": true, | ||
"name": "T" | ||
} | ||
] | ||
}, | ||
"typeAnnotation": { | ||
"type": "TSTypeLiteral", | ||
"start":121,"end":144,"loc":{"start":{"line":9,"column":27,"index":121},"end":{"line":11,"column":1,"index":144}}, | ||
"members": [ | ||
{ | ||
"type": "TSPropertySignature", | ||
"start":127,"end":142,"loc":{"start":{"line":10,"column":4,"index":127},"end":{"line":10,"column":19,"index":142}}, | ||
"key": { | ||
"type": "Identifier", | ||
"start":127,"end":128,"loc":{"start":{"line":10,"column":4,"index":127},"end":{"line":10,"column":5,"index":128},"identifierName":"f"}, | ||
"name": "f" | ||
}, | ||
"computed": false, | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":128,"end":141,"loc":{"start":{"line":10,"column":5,"index":128},"end":{"line":10,"column":18,"index":141}}, | ||
"typeAnnotation": { | ||
"type": "TSFunctionType", | ||
"start":130,"end":141,"loc":{"start":{"line":10,"column":7,"index":130},"end":{"line":10,"column":18,"index":141}}, | ||
"parameters": [ | ||
{ | ||
"type": "Identifier", | ||
"start":131,"end":135,"loc":{"start":{"line":10,"column":8,"index":131},"end":{"line":10,"column":12,"index":135},"identifierName":"x"}, | ||
"name": "x", | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":132,"end":135,"loc":{"start":{"line":10,"column":9,"index":132},"end":{"line":10,"column":12,"index":135}}, | ||
"typeAnnotation": { | ||
"type": "TSTypeReference", | ||
"start":134,"end":135,"loc":{"start":{"line":10,"column":11,"index":134},"end":{"line":10,"column":12,"index":135}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":134,"end":135,"loc":{"start":{"line":10,"column":11,"index":134},"end":{"line":10,"column":12,"index":135},"identifierName":"T"}, | ||
"name": "T" | ||
} | ||
} | ||
} | ||
} | ||
], | ||
"typeAnnotation": { | ||
"type": "TSTypeAnnotation", | ||
"start":137,"end":141,"loc":{"start":{"line":10,"column":14,"index":137},"end":{"line":10,"column":18,"index":141}}, | ||
"typeAnnotation": { | ||
"type": "TSTypeReference", | ||
"start":140,"end":141,"loc":{"start":{"line":10,"column":17,"index":140},"end":{"line":10,"column":18,"index":141}}, | ||
"typeName": { | ||
"type": "Identifier", | ||
"start":140,"end":141,"loc":{"start":{"line":10,"column":17,"index":140},"end":{"line":10,"column":18,"index":141},"identifierName":"T"}, | ||
"name": "T" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/babel-parser/test/fixtures/typescript/types/variance-annotations-error/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,4 @@ | ||
type T20<public T> = T; // Error | ||
type T21<in out in T> = T; // Error | ||
type T22<in out out T> = T; // Error | ||
type T23<out in T> = T; // Error |
3 changes: 3 additions & 0 deletions
3
packages/babel-parser/test/fixtures/typescript/types/variance-annotations-error/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 | ||
} |
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.
Can you add a generator test case to
packages/babel-generator/test/fixtures/typescript
?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'm investigating ci failures in main branch, but I don't know much about ts and jsx.
<in T>() => {}</in>
will fail when BABEL_8_BREAKING is enabled.Can someone tell me if this is a problem with the parser or with the test?