Skip to content

Commit

Permalink
feat: [4.7] support optional variance annotation (#4831)
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed May 19, 2022
1 parent 79fbc77 commit 7e7b24c
Show file tree
Hide file tree
Showing 68 changed files with 2,309 additions and 0 deletions.
Expand Up @@ -71,6 +71,7 @@ Program {
params: Array [
TSTypeParameter {
type: "TSTypeParameter",
in: false,
name: Identifier {
type: "Identifier",
name: "T",
Expand All @@ -81,6 +82,7 @@ Program {
end: { column: 11, line: 1 },
},
},
out: false,
range: [10, 11],
loc: {
Expand Down
Expand Up @@ -75,6 +75,7 @@ exports[`AST Fixtures declaration ClassDeclaration type-parameters-extends-type-
params: Array [
TSTypeParameter {
type: 'TSTypeParameter',
- in: false,
- name: Identifier {
- type: 'Identifier',
- name: 'T',
Expand All @@ -85,6 +86,7 @@ exports[`AST Fixtures declaration ClassDeclaration type-parameters-extends-type-
- end: { column: 11, line: 1 },
- },
- },
- out: false,
+ name: 'T',
range: [10, 11],
Expand Down
Expand Up @@ -32,6 +32,7 @@ Program {
params: Array [
TSTypeParameter {
type: "TSTypeParameter",
in: false,
name: Identifier {
type: "Identifier",
name: "T",
Expand All @@ -42,6 +43,7 @@ Program {
end: { column: 11, line: 1 },
},
},
out: false,
range: [10, 11],
loc: {
Expand Down
Expand Up @@ -36,6 +36,7 @@ exports[`AST Fixtures declaration ClassDeclaration type-parameters AST Alignment
params: Array [
TSTypeParameter {
type: 'TSTypeParameter',
- in: false,
- name: Identifier {
- type: 'Identifier',
- name: 'T',
Expand All @@ -46,6 +47,7 @@ exports[`AST Fixtures declaration ClassDeclaration type-parameters AST Alignment
- end: { column: 11, line: 1 },
- },
- },
- out: false,
+ name: 'T',
range: [10, 11],
Expand Down
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 @@ -496,6 +496,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 variance yet.
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 @@ -2734,6 +2734,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

0 comments on commit 7e7b24c

Please sign in to comment.