From ce4fcbf4401098387a2cf19ae8457c89c509239a Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sat, 6 Feb 2021 11:50:54 -0800 Subject: [PATCH] fix(scope-manager): fix visiting of TSImportType (#3008) Fixes #3006 --- .../tests/eslint-rules/no-undef.test.ts | 4 ++ .../src/referencer/TypeVisitor.ts | 6 ++ .../import-type-with-qualifier.ts | 1 + .../import-type-with-qualifier.ts.shot | 38 +++++++++++ .../import-type-with-type-params.ts | 2 + .../import-type-with-type-params.ts.shot | 63 +++++++++++++++++++ .../fixtures/type-declaration/import-type.ts | 1 + .../type-declaration/import-type.ts.shot | 38 +++++++++++ 8 files changed, 153 insertions(+) create mode 100644 packages/scope-manager/tests/fixtures/type-declaration/import-type-with-qualifier.ts create mode 100644 packages/scope-manager/tests/fixtures/type-declaration/import-type-with-qualifier.ts.shot create mode 100644 packages/scope-manager/tests/fixtures/type-declaration/import-type-with-type-params.ts create mode 100644 packages/scope-manager/tests/fixtures/type-declaration/import-type-with-type-params.ts.shot create mode 100644 packages/scope-manager/tests/fixtures/type-declaration/import-type.ts create mode 100644 packages/scope-manager/tests/fixtures/type-declaration/import-type.ts.shot diff --git a/packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts b/packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts index c7cd5aeb399..020a4e715d6 100644 --- a/packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts +++ b/packages/eslint-plugin/tests/eslint-rules/no-undef.test.ts @@ -235,6 +235,10 @@ declare function deco(...param: any): (...param: any) => any; }) class Foo {} `, + // https://github.com/typescript-eslint/typescript-eslint/issues/3006 + ` +export type AppState = typeof import('./src/store/reducers').default; + `, ], invalid: [ { diff --git a/packages/scope-manager/src/referencer/TypeVisitor.ts b/packages/scope-manager/src/referencer/TypeVisitor.ts index 155aea5c5d9..e9abb40f4f4 100644 --- a/packages/scope-manager/src/referencer/TypeVisitor.ts +++ b/packages/scope-manager/src/referencer/TypeVisitor.ts @@ -116,6 +116,12 @@ class TypeVisitor extends Visitor { this.visitFunctionType(node); } + protected TSImportType(node: TSESTree.TSImportType): void { + // the TS parser allows any type to be the parameter, but it's a syntax error - so we can ignore it + this.visit(node.typeParameters); + // the qualifier is just part of a standard EntityName, so it should not be visited + } + protected TSIndexSignature(node: TSESTree.TSIndexSignature): void { for (const param of node.parameters) { if (param.type === AST_NODE_TYPES.Identifier) { diff --git a/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-qualifier.ts b/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-qualifier.ts new file mode 100644 index 00000000000..b67f291d115 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-qualifier.ts @@ -0,0 +1 @@ +type T = import('').default; diff --git a/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-qualifier.ts.shot b/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-qualifier.ts.shot new file mode 100644 index 00000000000..25e42ebd08b --- /dev/null +++ b/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-qualifier.ts.shot @@ -0,0 +1,38 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`type-declaration import-type-with-qualifier 1`] = ` +ScopeManager { + variables: Array [ + ImplicitGlobalConstTypeVariable, + Variable$2 { + defs: Array [ + TypeDefinition$1 { + name: Identifier<"T">, + node: TSTypeAliasDeclaration$1, + }, + ], + name: "T", + references: Array [], + isValueVariable: false, + isTypeVariable: true, + }, + ], + scopes: Array [ + GlobalScope$1 { + block: Program$2, + isStrict: false, + references: Array [], + set: Map { + "const" => ImplicitGlobalConstTypeVariable, + "T" => Variable$2, + }, + type: "global", + upper: null, + variables: Array [ + ImplicitGlobalConstTypeVariable, + Variable$2, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-type-params.ts b/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-type-params.ts new file mode 100644 index 00000000000..ce2c80bdced --- /dev/null +++ b/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-type-params.ts @@ -0,0 +1,2 @@ +type Param = string; +type T = import('').foo; diff --git a/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-type-params.ts.shot b/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-type-params.ts.shot new file mode 100644 index 00000000000..4a48b256ff9 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/type-declaration/import-type-with-type-params.ts.shot @@ -0,0 +1,63 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`type-declaration import-type-with-type-params 1`] = ` +ScopeManager { + variables: Array [ + ImplicitGlobalConstTypeVariable, + Variable$2 { + defs: Array [ + TypeDefinition$1 { + name: Identifier<"Param">, + node: TSTypeAliasDeclaration$1, + }, + ], + name: "Param", + references: Array [ + Reference$1 { + identifier: Identifier<"Param">, + isRead: true, + isTypeReference: true, + isValueReference: false, + isWrite: false, + resolved: Variable$2, + }, + ], + isValueVariable: false, + isTypeVariable: true, + }, + Variable$3 { + defs: Array [ + TypeDefinition$2 { + name: Identifier<"T">, + node: TSTypeAliasDeclaration$2, + }, + ], + name: "T", + references: Array [], + isValueVariable: false, + isTypeVariable: true, + }, + ], + scopes: Array [ + GlobalScope$1 { + block: Program$3, + isStrict: false, + references: Array [ + Reference$1, + ], + set: Map { + "const" => ImplicitGlobalConstTypeVariable, + "Param" => Variable$2, + "T" => Variable$3, + }, + type: "global", + upper: null, + variables: Array [ + ImplicitGlobalConstTypeVariable, + Variable$2, + Variable$3, + ], + }, + ], +} +`; diff --git a/packages/scope-manager/tests/fixtures/type-declaration/import-type.ts b/packages/scope-manager/tests/fixtures/type-declaration/import-type.ts new file mode 100644 index 00000000000..888f91b399a --- /dev/null +++ b/packages/scope-manager/tests/fixtures/type-declaration/import-type.ts @@ -0,0 +1 @@ +type T = import(''); diff --git a/packages/scope-manager/tests/fixtures/type-declaration/import-type.ts.shot b/packages/scope-manager/tests/fixtures/type-declaration/import-type.ts.shot new file mode 100644 index 00000000000..af47658faac --- /dev/null +++ b/packages/scope-manager/tests/fixtures/type-declaration/import-type.ts.shot @@ -0,0 +1,38 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`type-declaration import-type 1`] = ` +ScopeManager { + variables: Array [ + ImplicitGlobalConstTypeVariable, + Variable$2 { + defs: Array [ + TypeDefinition$1 { + name: Identifier<"T">, + node: TSTypeAliasDeclaration$1, + }, + ], + name: "T", + references: Array [], + isValueVariable: false, + isTypeVariable: true, + }, + ], + scopes: Array [ + GlobalScope$1 { + block: Program$2, + isStrict: false, + references: Array [], + set: Map { + "const" => ImplicitGlobalConstTypeVariable, + "T" => Variable$2, + }, + type: "global", + upper: null, + variables: Array [ + ImplicitGlobalConstTypeVariable, + Variable$2, + ], + }, + ], +} +`;