Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: [TS4.7] allow visiting of typeParameters in TSTypeQuery (#5166)
* fix: allow visiting of typeParameters in TSTypeQuery

* fix: update eslint disable to noFormat
  • Loading branch information
armano2 committed Jun 10, 2022
1 parent 331ff3b commit dc1f930
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Expand Up @@ -14,6 +14,9 @@ packages/eslint-plugin/src/configs/*.json
CONTRIBUTORS.md
packages/ast-spec/src/*/*/fixtures/_error_/*/fixture.ts

# Syntax not yet supported
packages/scope-manager/tests/fixtures/type-declaration/type-query-with-parameters.ts

# Ignore CHANGELOG.md files to avoid issues with automated release job
CHANGELOG.md

Expand Down
Expand Up @@ -739,6 +739,15 @@ export function foo() {
return new Promise<Foo>();
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/5152
{
code: noFormat`
function foo<T>(value: T): T {
return { value };
}
export type Foo<T> = typeof foo<T>;
`,
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2331
{
code: `
Expand Down
1 change: 1 addition & 0 deletions packages/scope-manager/src/referencer/TypeVisitor.ts
Expand Up @@ -265,6 +265,7 @@ class TypeVisitor extends Visitor {
}
this.#referencer.currentScope().referenceValue(expr);
}
this.visit(node.typeParameters);
}

protected TSTypeAnnotation(node: TSESTree.TSTypeAnnotation): void {
Expand Down
4 changes: 3 additions & 1 deletion packages/scope-manager/tests/fixtures.test.ts
Expand Up @@ -13,7 +13,9 @@ const ONLY = [].join(path.sep);
const FIXTURES_DIR = path.resolve(__dirname, 'fixtures');

const fixtures = glob
.sync(`${FIXTURES_DIR}/**/*.{js,ts,jsx,tsx}`, {
.sync('**/*.{js,ts,jsx,tsx}', {
cwd: FIXTURES_DIR,
absolute: true,
ignore: ['fixtures.test.ts'],
})
.map(absolute => {
Expand Down
@@ -0,0 +1,5 @@
function foo<T>(y: T) {
return { y };
}

export type Foo<T> = typeof foo<T>;
@@ -0,0 +1,167 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`type-declaration type-query-with-parameters 1`] = `
ScopeManager {
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2 {
defs: Array [
FunctionNameDefinition$1 {
name: Identifier<"foo">,
node: FunctionDeclaration$1,
},
],
name: "foo",
references: Array [
Reference$3 {
identifier: Identifier<"foo">,
isRead: true,
isTypeReference: false,
isValueReference: true,
isWrite: false,
resolved: Variable$2,
},
],
isValueVariable: true,
isTypeVariable: false,
},
Variable$3 {
defs: Array [],
name: "arguments",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
Variable$4 {
defs: Array [
ParameterDefinition$2 {
name: Identifier<"y">,
node: FunctionDeclaration$1,
},
],
name: "y",
references: Array [
Reference$2 {
identifier: Identifier<"y">,
isRead: true,
isTypeReference: false,
isValueReference: true,
isWrite: false,
resolved: Variable$4,
},
],
isValueVariable: true,
isTypeVariable: false,
},
Variable$5 {
defs: Array [
TypeDefinition$3 {
name: Identifier<"T">,
node: TSTypeParameter$2,
},
],
name: "T",
references: Array [
Reference$1 {
identifier: Identifier<"T">,
isRead: true,
isTypeReference: true,
isValueReference: false,
isWrite: false,
resolved: Variable$5,
},
],
isValueVariable: false,
isTypeVariable: true,
},
Variable$6 {
defs: Array [
TypeDefinition$4 {
name: Identifier<"Foo">,
node: TSTypeAliasDeclaration$3,
},
],
name: "Foo",
references: Array [],
isValueVariable: false,
isTypeVariable: true,
},
Variable$7 {
defs: Array [
TypeDefinition$5 {
name: Identifier<"T">,
node: TSTypeParameter$4,
},
],
name: "T",
references: Array [
Reference$4 {
identifier: Identifier<"T">,
isRead: true,
isTypeReference: true,
isValueReference: false,
isWrite: false,
resolved: Variable$7,
},
],
isValueVariable: false,
isTypeVariable: true,
},
],
scopes: Array [
GlobalScope$1 {
block: Program$5,
isStrict: false,
references: Array [],
set: Map {
"const" => ImplicitGlobalConstTypeVariable,
"foo" => Variable$2,
"Foo" => Variable$6,
},
type: "global",
upper: null,
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2,
Variable$6,
],
},
FunctionScope$2 {
block: FunctionDeclaration$1,
isStrict: false,
references: Array [
Reference$1,
Reference$2,
],
set: Map {
"arguments" => Variable$3,
"y" => Variable$4,
"T" => Variable$5,
},
type: "function",
upper: GlobalScope$1,
variables: Array [
Variable$3,
Variable$4,
Variable$5,
],
},
TypeScope$3 {
block: TSTypeAliasDeclaration$3,
isStrict: true,
references: Array [
Reference$3,
Reference$4,
],
set: Map {
"T" => Variable$7,
},
type: "type",
upper: GlobalScope$1,
variables: Array [
Variable$7,
],
},
],
}
`;
2 changes: 1 addition & 1 deletion packages/visitor-keys/src/visitor-keys.ts
Expand Up @@ -142,7 +142,7 @@ const additionalKeys: AdditionalKeys = {
TSTypeParameterDeclaration: ['params'],
TSTypeParameterInstantiation: ['params'],
TSTypePredicate: ['typeAnnotation', 'parameterName'],
TSTypeQuery: ['exprName'],
TSTypeQuery: ['exprName', 'typeParameters'],
TSTypeReference: ['typeName', 'typeParameters'],
TSUndefinedKeyword: [],
TSUnionType: ['types'],
Expand Down

0 comments on commit dc1f930

Please sign in to comment.