From 836de79e8d1bff43149168cc913a4c2b60e79bf6 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Wed, 25 May 2022 06:49:25 +0900 Subject: [PATCH] feat: [TS4.7] support type parameters for `typeof` (#5067) --- .../ast-spec/src/type/TSTypeQuery/spec.ts | 2 + .../types/typeof-with-type-parameters.src.ts | 1 + packages/typescript-estree/src/convert.ts | 3 + .../semantic-diagnostics-enabled.test.ts.snap | 2 + .../typeof-with-type-parameters.src.ts.shot | 423 ++++++++++++++++++ .../typescript/types/typeof.src.ts.shot | 1 + 6 files changed, 432 insertions(+) create mode 100644 packages/shared-fixtures/fixtures/typescript/types/typeof-with-type-parameters.src.ts create mode 100644 packages/typescript-estree/tests/snapshots/typescript/types/typeof-with-type-parameters.src.ts.shot diff --git a/packages/ast-spec/src/type/TSTypeQuery/spec.ts b/packages/ast-spec/src/type/TSTypeQuery/spec.ts index bf1cd3e192d..634c307ad2c 100644 --- a/packages/ast-spec/src/type/TSTypeQuery/spec.ts +++ b/packages/ast-spec/src/type/TSTypeQuery/spec.ts @@ -1,8 +1,10 @@ import type { AST_NODE_TYPES } from '../../ast-node-types'; import type { BaseNode } from '../../base/BaseNode'; +import type { TSTypeParameterInstantiation } from '../../special/spec'; import type { EntityName } from '../../unions/EntityName'; export interface TSTypeQuery extends BaseNode { type: AST_NODE_TYPES.TSTypeQuery; exprName: EntityName; + typeParameters?: TSTypeParameterInstantiation; } diff --git a/packages/shared-fixtures/fixtures/typescript/types/typeof-with-type-parameters.src.ts b/packages/shared-fixtures/fixtures/typescript/types/typeof-with-type-parameters.src.ts new file mode 100644 index 00000000000..7287d407d29 --- /dev/null +++ b/packages/shared-fixtures/fixtures/typescript/types/typeof-with-type-parameters.src.ts @@ -0,0 +1 @@ +let x: typeof y.z; diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 9e576429690..864f85d2cd2 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -2429,6 +2429,9 @@ export class Converter { return this.createNode(node, { type: AST_NODE_TYPES.TSTypeQuery, exprName: this.convertType(node.exprName), + typeParameters: + node.typeArguments && + this.convertTypeArgumentsToTypeParameters(node.typeArguments, node), }); } diff --git a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap index 12e8316d7b6..16e81894b38 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap @@ -2786,6 +2786,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/typeof.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; +exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/typeof-with-type-parameters.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; + exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/union-intersection.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/union-type.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; diff --git a/packages/typescript-estree/tests/snapshots/typescript/types/typeof-with-type-parameters.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/types/typeof-with-type-parameters.src.ts.shot new file mode 100644 index 00000000000..4215d60c393 --- /dev/null +++ b/packages/typescript-estree/tests/snapshots/typescript/types/typeof-with-type-parameters.src.ts.shot @@ -0,0 +1,423 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`typescript types typeof-with-type-parameters.src 1`] = ` +Object { + "body": Array [ + Object { + "declarations": Array [ + Object { + "id": Object { + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 4, + "line": 1, + }, + }, + "name": "x", + "range": Array [ + 4, + 20, + ], + "type": "Identifier", + "typeAnnotation": Object { + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 5, + "line": 1, + }, + }, + "range": Array [ + 5, + 20, + ], + "type": "TSTypeAnnotation", + "typeAnnotation": Object { + "exprName": Object { + "left": Object { + "loc": Object { + "end": Object { + "column": 15, + "line": 1, + }, + "start": Object { + "column": 14, + "line": 1, + }, + }, + "name": "y", + "range": Array [ + 14, + 15, + ], + "type": "Identifier", + }, + "loc": Object { + "end": Object { + "column": 17, + "line": 1, + }, + "start": Object { + "column": 14, + "line": 1, + }, + }, + "range": Array [ + 14, + 17, + ], + "right": Object { + "loc": Object { + "end": Object { + "column": 17, + "line": 1, + }, + "start": Object { + "column": 16, + "line": 1, + }, + }, + "name": "z", + "range": Array [ + 16, + 17, + ], + "type": "Identifier", + }, + "type": "TSQualifiedName", + }, + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 20, + ], + "type": "TSTypeQuery", + "typeParameters": Object { + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 17, + "line": 1, + }, + }, + "params": Array [ + Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 18, + "line": 1, + }, + }, + "range": Array [ + 18, + 19, + ], + "type": "TSTypeReference", + "typeName": Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 18, + "line": 1, + }, + }, + "name": "w", + "range": Array [ + 18, + 19, + ], + "type": "Identifier", + }, + "typeParameters": undefined, + }, + ], + "range": Array [ + 17, + 20, + ], + "type": "TSTypeParameterInstantiation", + }, + }, + }, + }, + "init": null, + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 4, + "line": 1, + }, + }, + "range": Array [ + 4, + 20, + ], + "type": "VariableDeclarator", + }, + ], + "kind": "let", + "loc": Object { + "end": Object { + "column": 21, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 21, + ], + "type": "VariableDeclaration", + }, + ], + "comments": Array [], + "loc": Object { + "end": Object { + "column": 0, + "line": 2, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 22, + ], + "sourceType": "script", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 3, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 3, + ], + "type": "Keyword", + "value": "let", + }, + Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 1, + }, + "start": Object { + "column": 4, + "line": 1, + }, + }, + "range": Array [ + 4, + 5, + ], + "type": "Identifier", + "value": "x", + }, + Object { + "loc": Object { + "end": Object { + "column": 6, + "line": 1, + }, + "start": Object { + "column": 5, + "line": 1, + }, + }, + "range": Array [ + 5, + 6, + ], + "type": "Punctuator", + "value": ":", + }, + Object { + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 13, + ], + "type": "Keyword", + "value": "typeof", + }, + Object { + "loc": Object { + "end": Object { + "column": 15, + "line": 1, + }, + "start": Object { + "column": 14, + "line": 1, + }, + }, + "range": Array [ + 14, + 15, + ], + "type": "Identifier", + "value": "y", + }, + Object { + "loc": Object { + "end": Object { + "column": 16, + "line": 1, + }, + "start": Object { + "column": 15, + "line": 1, + }, + }, + "range": Array [ + 15, + 16, + ], + "type": "Punctuator", + "value": ".", + }, + Object { + "loc": Object { + "end": Object { + "column": 17, + "line": 1, + }, + "start": Object { + "column": 16, + "line": 1, + }, + }, + "range": Array [ + 16, + 17, + ], + "type": "Identifier", + "value": "z", + }, + Object { + "loc": Object { + "end": Object { + "column": 18, + "line": 1, + }, + "start": Object { + "column": 17, + "line": 1, + }, + }, + "range": Array [ + 17, + 18, + ], + "type": "Punctuator", + "value": "<", + }, + Object { + "loc": Object { + "end": Object { + "column": 19, + "line": 1, + }, + "start": Object { + "column": 18, + "line": 1, + }, + }, + "range": Array [ + 18, + 19, + ], + "type": "Identifier", + "value": "w", + }, + Object { + "loc": Object { + "end": Object { + "column": 20, + "line": 1, + }, + "start": Object { + "column": 19, + "line": 1, + }, + }, + "range": Array [ + 19, + 20, + ], + "type": "Punctuator", + "value": ">", + }, + Object { + "loc": Object { + "end": Object { + "column": 21, + "line": 1, + }, + "start": Object { + "column": 20, + "line": 1, + }, + }, + "range": Array [ + 20, + 21, + ], + "type": "Punctuator", + "value": ";", + }, + ], + "type": "Program", +} +`; diff --git a/packages/typescript-estree/tests/snapshots/typescript/types/typeof.src.ts.shot b/packages/typescript-estree/tests/snapshots/typescript/types/typeof.src.ts.shot index 58a01f14e00..43036c5b34a 100644 --- a/packages/typescript-estree/tests/snapshots/typescript/types/typeof.src.ts.shot +++ b/packages/typescript-estree/tests/snapshots/typescript/types/typeof.src.ts.shot @@ -108,6 +108,7 @@ Object { 17, ], "type": "TSTypeQuery", + "typeParameters": undefined, }, }, },