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

feat: [4.7] support optional variance annotation #4831

Merged
merged 12 commits into from May 19, 2022
2 changes: 2 additions & 0 deletions packages/ast-spec/src/special/TSTypeParameter/spec.ts
Expand Up @@ -8,4 +8,6 @@ export interface TSTypeParameter extends BaseNode {
name: Identifier;
constraint?: TypeNode;
default?: TypeNode;
in?: boolean;
out?: boolean;
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
}
@@ -0,0 +1 @@
type Mapper<in T, out U> = (x: T) => U;
@@ -0,0 +1 @@
type Processor<in out T> = (x: T) => T;
@@ -0,0 +1 @@
type Consumer<in T> = (x: T) => void;
@@ -0,0 +1 @@
type Provider<out T> = () => T;
21 changes: 21 additions & 0 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -671,6 +671,26 @@ export class Converter {
: node.elements.map(element => this.convertChild(element));
}

private convertTypeParamVariances(
modifiers?: ts.ModifiersArray,
): Pick<TSESTree.TSTypeParameter, 'in' | 'out'> {
const result: Pick<TSESTree.TSTypeParameter, 'in' | 'out'> = {
in: undefined,
out: undefined,
};
if (!modifiers) {
return result;
}
for (const modifier of modifiers) {
if (modifier.kind === SyntaxKind.InKeyword) {
result.in = true;
} else if (modifier.kind === SyntaxKind.OutKeyword) {
result.out = true;
}
}
return result;
}

/**
* Applies the given TS modifiers to the given result object.
* @param result
Expand Down Expand Up @@ -2342,6 +2362,7 @@ export class Converter {
? this.convertType(node.constraint)
: undefined,
default: node.default ? this.convertType(node.default) : undefined,
...this.convertTypeParamVariances(node.modifiers),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about changing it to hasModifier to be consistent with other use cases?

Suggested change
...this.convertTypeParamVariances(node.modifiers),
in: hasModifier(SyntaxKind.InKeyword, node) || undefined,
out: hasModifier(SyntaxKind.OutKeyword, node) || undefined,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of undefined - could we make it false?
I'd love for us to standardise on not emitting undefined for properties.
I personally don't like having 3 states on a flag - the keyword is either there, or it's not - there's no 3rd state!

});
}

Expand Down
Expand Up @@ -484,6 +484,14 @@ tester.addFixturePatternConfig('typescript/types', {
'template-literal-type-2',
'template-literal-type-3',
'template-literal-type-4',
/**
* [BABEL ERRORED, BUT TS-ESTREE DID NOT]
* Babel doesn't support TS 4.7 new features yet.
*/
'optional-variance-in',
'optional-variance-out',
'optional-variance-in-out',
'optional-variance-in-and-out',
],
});

Expand Down
Expand Up @@ -475,6 +475,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 11,
Expand Down Expand Up @@ -503,6 +504,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
10,
11,
Expand Down
Expand Up @@ -2730,6 +2730,14 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/object-literal-type-with-accessors.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/optional-variance-in.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/optional-variance-in-and-out.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/optional-variance-in-out.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/optional-variance-out.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/parenthesized-type.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/reference.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down
Expand Up @@ -74,6 +74,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 14,
Expand Down Expand Up @@ -102,6 +103,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
13,
14,
Expand Down
Expand Up @@ -106,6 +106,7 @@ Object {
],
"type": "TSObjectKeyword",
},
"in": undefined,
"loc": Object {
"end": Object {
"column": 36,
Expand Down Expand Up @@ -134,6 +135,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
11,
36,
Expand Down
Expand Up @@ -219,6 +219,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 2,
Expand Down Expand Up @@ -247,6 +248,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
1,
2,
Expand Down
Expand Up @@ -168,6 +168,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 4,
Expand Down Expand Up @@ -196,6 +197,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
16,
17,
Expand Down Expand Up @@ -329,6 +331,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 7,
Expand Down Expand Up @@ -357,6 +360,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
44,
45,
Expand Down
Expand Up @@ -98,6 +98,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 15,
Expand Down Expand Up @@ -126,6 +127,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
24,
25,
Expand Down Expand Up @@ -234,6 +236,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 19,
Expand Down Expand Up @@ -262,6 +265,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
52,
53,
Expand Down
Expand Up @@ -214,6 +214,7 @@ Object {
"typeParameters": undefined,
},
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 21,
Expand Down Expand Up @@ -242,6 +243,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
10,
21,
Expand Down
Expand Up @@ -143,6 +143,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 11,
Expand Down Expand Up @@ -171,6 +172,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
10,
11,
Expand Down
Expand Up @@ -133,6 +133,7 @@ Object {
},
"typeParameters": undefined,
},
"in": undefined,
"loc": Object {
"end": Object {
"column": 16,
Expand Down Expand Up @@ -161,6 +162,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
21,
28,
Expand Down
Expand Up @@ -98,6 +98,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 10,
Expand Down Expand Up @@ -126,6 +127,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
21,
22,
Expand Down
Expand Up @@ -211,6 +211,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 7,
Expand Down Expand Up @@ -239,6 +240,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
36,
37,
Expand Down
Expand Up @@ -235,6 +235,7 @@ Object {
},
},
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 35,
Expand Down Expand Up @@ -263,6 +264,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
11,
35,
Expand Down
Expand Up @@ -289,6 +289,7 @@ Object {
},
},
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 36,
Expand Down Expand Up @@ -317,6 +318,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
11,
36,
Expand Down Expand Up @@ -828,6 +830,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 18,
Expand Down Expand Up @@ -856,6 +859,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
175,
176,
Expand Down
Expand Up @@ -133,6 +133,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 17,
Expand Down Expand Up @@ -161,6 +162,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
26,
27,
Expand Down Expand Up @@ -303,6 +305,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 19,
Expand Down Expand Up @@ -331,6 +334,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
64,
65,
Expand Down
Expand Up @@ -106,6 +106,7 @@ Object {
},
"typeParameters": undefined,
},
"in": undefined,
"loc": Object {
"end": Object {
"column": 17,
Expand Down Expand Up @@ -134,6 +135,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
10,
17,
Expand Down
Expand Up @@ -71,6 +71,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": undefined,
"loc": Object {
"end": Object {
"column": 11,
Expand Down Expand Up @@ -99,6 +100,7 @@ Object {
],
"type": "Identifier",
},
"out": undefined,
"range": Array [
8,
11,
Expand Down