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;
}
@@ -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;
2 changes: 2 additions & 0 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -2342,6 +2342,8 @@ export class Converter {
? this.convertType(node.constraint)
: undefined,
default: node.default ? this.convertType(node.default) : undefined,
in: hasModifier(SyntaxKind.InKeyword, node),
out: hasModifier(SyntaxKind.OutKeyword, node),
});
}

Expand Down
Expand Up @@ -489,6 +489,14 @@ tester.addFixturePatternConfig('typescript/types', {
* Babel doesn't support TS 4.7 new feature yet.
*/
'conditional-infer-with-constraint',
/**
* [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
7 changes: 7 additions & 0 deletions packages/typescript-estree/tests/ast-alignment/utils.ts
Expand Up @@ -150,6 +150,13 @@ export function preprocessBabylonAST(ast: File): any {
type: AST_NODE_TYPES.Identifier,
};
}
// Babel does not support TS 4.7 optional vaiance yet.
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
if (!node.in) {
node.in = false;
}
if (!node.out) {
node.out = false;
}
},
MethodDefinition(node) {
/**
Expand Down
Expand Up @@ -475,6 +475,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 11,
Expand Down Expand Up @@ -503,6 +504,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
10,
11,
Expand Down
Expand Up @@ -2732,6 +2732,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": false,
"loc": Object {
"end": Object {
"column": 14,
Expand Down Expand Up @@ -102,6 +103,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
13,
14,
Expand Down
Expand Up @@ -106,6 +106,7 @@ Object {
],
"type": "TSObjectKeyword",
},
"in": false,
"loc": Object {
"end": Object {
"column": 36,
Expand Down Expand Up @@ -134,6 +135,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
11,
36,
Expand Down
Expand Up @@ -219,6 +219,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 2,
Expand Down Expand Up @@ -247,6 +248,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
1,
2,
Expand Down
Expand Up @@ -168,6 +168,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 4,
Expand Down Expand Up @@ -196,6 +197,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
16,
17,
Expand Down Expand Up @@ -329,6 +331,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 7,
Expand Down Expand Up @@ -357,6 +360,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
44,
45,
Expand Down
Expand Up @@ -98,6 +98,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 15,
Expand Down Expand Up @@ -126,6 +127,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
24,
25,
Expand Down Expand Up @@ -234,6 +236,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 19,
Expand Down Expand Up @@ -262,6 +265,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
52,
53,
Expand Down
Expand Up @@ -214,6 +214,7 @@ Object {
"typeParameters": undefined,
},
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 21,
Expand Down Expand Up @@ -242,6 +243,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
10,
21,
Expand Down
Expand Up @@ -143,6 +143,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 11,
Expand Down Expand Up @@ -171,6 +172,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
10,
11,
Expand Down
Expand Up @@ -133,6 +133,7 @@ Object {
},
"typeParameters": undefined,
},
"in": false,
"loc": Object {
"end": Object {
"column": 16,
Expand Down Expand Up @@ -161,6 +162,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
21,
28,
Expand Down
Expand Up @@ -98,6 +98,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 10,
Expand Down Expand Up @@ -126,6 +127,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
21,
22,
Expand Down
Expand Up @@ -211,6 +211,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 7,
Expand Down Expand Up @@ -239,6 +240,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
36,
37,
Expand Down
Expand Up @@ -235,6 +235,7 @@ Object {
},
},
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 35,
Expand Down Expand Up @@ -263,6 +264,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
11,
35,
Expand Down
Expand Up @@ -289,6 +289,7 @@ Object {
},
},
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 36,
Expand Down Expand Up @@ -317,6 +318,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
11,
36,
Expand Down Expand Up @@ -828,6 +830,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 18,
Expand Down Expand Up @@ -856,6 +859,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
175,
176,
Expand Down
Expand Up @@ -133,6 +133,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 17,
Expand Down Expand Up @@ -161,6 +162,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
26,
27,
Expand Down Expand Up @@ -303,6 +305,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 19,
Expand Down Expand Up @@ -331,6 +334,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
64,
65,
Expand Down
Expand Up @@ -106,6 +106,7 @@ Object {
},
"typeParameters": undefined,
},
"in": false,
"loc": Object {
"end": Object {
"column": 17,
Expand Down Expand Up @@ -134,6 +135,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
10,
17,
Expand Down
Expand Up @@ -71,6 +71,7 @@ Object {
Object {
"constraint": undefined,
"default": undefined,
"in": false,
"loc": Object {
"end": Object {
"column": 11,
Expand Down Expand Up @@ -99,6 +100,7 @@ Object {
],
"type": "Identifier",
},
"out": false,
"range": Array [
8,
11,
Expand Down