Skip to content
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
merged 9 commits into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/babel-generator/src/generators/typescript.ts
Expand Up @@ -25,6 +25,16 @@ export function TSTypeParameterInstantiation(
export { TSTypeParameterInstantiation as TSTypeParameterDeclaration };

export function TSTypeParameter(this: Printer, node: t.TSTypeParameter) {
if (node.in) {
this.word("in");
this.space();
}

if (node.out) {
this.word("out");
this.space();
}

Copy link
Contributor

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?

Copy link
Member

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?

this.word(
!process.env.BABEL_8_BREAKING
? (node.name as unknown as string)
Expand Down
41 changes: 38 additions & 3 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -47,7 +47,8 @@ type TsModifier =
| "declare"
| "static"
| "override"
| N.Accessibility;
| N.Accessibility
| N.VarianceAnnotations;

function nonNull<T>(x: ?T): T {
if (x == null) {
Expand Down Expand Up @@ -153,6 +154,10 @@ const TSErrors = ParseErrorEnum`typescript`(_ => ({
InvalidModifierOnTypeMember: _<{| modifier: TsModifier |}>(
({ modifier }) => `'${modifier}' modifier cannot appear on a type member.`,
),
InvalidModifierOnTypeParameter: _<{| modifier: TsModifier |}>(
({ modifier }) =>
`'${modifier}' modifier cannot appear on a type parameter.`,
),
InvalidModifiersOrder: _<{| orderedModifiers: [TsModifier, TsModifier] |}>(
({ orderedModifiers }) =>
`'${orderedModifiers[0]}' modifier must precede '${orderedModifiers[1]}' modifier.`,
Expand Down Expand Up @@ -286,6 +291,10 @@ function tsIsAccessModifier(modifier: string): boolean %checks {
);
}

function tsIsVarianceAnnotations(modifier: string): boolean %checks {
return modifier === "in" || modifier === "out";
}

export default (superClass: Class<Parser>): Class<Parser> =>
class extends superClass {
getScopeHandler(): Class<TypeScriptScopeHandler> {
Expand Down Expand Up @@ -324,7 +333,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
allowedModifiers: T[],
stopOnStartOfClassStaticBlock?: boolean,
): ?T {
if (!tokenIsIdentifier(this.state.type)) {
if (!tokenIsIdentifier(this.state.type) && this.state.type !== tt._in) {
return undefined;
}

Expand All @@ -350,6 +359,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
allowedModifiers,
disallowedModifiers,
stopOnStartOfClassStaticBlock,
errorTemplate = TSErrors.InvalidModifierOnTypeMember,
}: {
modified: {
[key: TsModifier]: ?true,
Expand All @@ -358,6 +368,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
allowedModifiers: TsModifier[],
disallowedModifiers?: TsModifier[],
stopOnStartOfClassStaticBlock?: boolean,
// FIXME: make sure errorTemplate can receive `modifier`
errorTemplate?: any,
}): void {
const enforceOrder = (loc, modifier, before, after) => {
if (modifier === before && modified[after]) {
Expand Down Expand Up @@ -401,6 +413,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>

modified.accessibility = modifier;
}
} else if (tsIsVarianceAnnotations(modifier)) {
if (modified[modifier]) {
this.raise(TSErrors.DuplicateModifier, { at: startLoc, modifier });
}
modified[modifier] = true;

enforceOrder(startLoc, modifier, "in", "out");
} else {
if (Object.hasOwnProperty.call(modified, modifier)) {
this.raise(TSErrors.DuplicateModifier, { at: startLoc, modifier });
Expand All @@ -417,7 +436,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

if (disallowedModifiers?.includes(modifier)) {
this.raise(TSErrors.InvalidModifierOnTypeMember, {
this.raise(errorTemplate, {
at: startLoc,
modifier,
});
Expand Down Expand Up @@ -635,6 +654,22 @@ export default (superClass: Class<Parser>): Class<Parser> =>

tsParseTypeParameter(): N.TsTypeParameter {
const node: N.TsTypeParameter = this.startNode();

this.tsParseModifiers({
modified: node,
allowedModifiers: ["in", "out"],
disallowedModifiers: [
"public",
"private",
"protected",
"readonly",
"declare",
"abstract",
"override",
],
errorTemplate: TSErrors.InvalidModifierOnTypeParameter,
});

node.name = this.tsParseTypeParameterName();
node.constraint = this.tsEatThenParseType(tt._extends);
node.default = this.tsEatThenParseType(tt.eq);
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-parser/src/types.js
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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,
};
Expand Down
@@ -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;
}
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -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": []
}
}
@@ -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
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}