Skip to content

Commit

Permalink
fix(scope-manager): handle typeParameters of TSInstantiationExpression (
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzzen committed Jul 22, 2022
1 parent a0d5a70 commit 2595ccf
Show file tree
Hide file tree
Showing 7 changed files with 418 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Expand Up @@ -17,6 +17,8 @@ packages/ast-spec/src/*/*/fixtures/_error_/*/fixture.ts
# Syntax not yet supported
packages/scope-manager/tests/fixtures/type-declaration/type-query-with-parameters.ts
packages/scope-manager/tests/fixtures/type-declaration/infer-type-constraint.ts
packages/scope-manager/tests/fixtures/instantiation-expressions/type-arguments1.ts
packages/scope-manager/tests/fixtures/instantiation-expressions/type-arguments2.ts

# Ignore CHANGELOG.md files to avoid issues with automated release job
CHANGELOG.md
Expand Down
Expand Up @@ -921,6 +921,15 @@ export declare namespace Foo {
}
}
}
`,
noFormat`
class Foo<T> {
value: T;
}
class Bar<T> {
foo = Foo<T>;
}
new Bar();
`,
{
code: `
Expand Down
7 changes: 7 additions & 0 deletions packages/scope-manager/src/referencer/Referencer.ts
Expand Up @@ -692,6 +692,13 @@ class Referencer extends Visitor {
this.close(node);
}

protected TSInstantiationExpression(
node: TSESTree.TSInstantiationExpression,
): void {
this.visitChildren(node, ['typeParameters']);
this.visitType(node.typeParameters);
}

protected TSInterfaceDeclaration(
node: TSESTree.TSInterfaceDeclaration,
): void {
Expand Down
@@ -0,0 +1,9 @@
class Foo<T> {
value: T
}

class Bar<T> {
foo = Foo<T>
}

new Bar()
@@ -0,0 +1,182 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`instantiation-expressions type-arguments1 1`] = `
ScopeManager {
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2 {
defs: Array [
ClassNameDefinition$1 {
name: Identifier<"Foo">,
node: ClassDeclaration$1,
},
],
name: "Foo",
references: Array [
Reference$2 {
identifier: Identifier<"Foo">,
isRead: true,
isTypeReference: false,
isValueReference: true,
isWrite: false,
resolved: Variable$2,
},
],
isValueVariable: true,
isTypeVariable: true,
},
Variable$3 {
defs: Array [
ClassNameDefinition$2 {
name: Identifier<"Foo">,
node: ClassDeclaration$1,
},
],
name: "Foo",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
Variable$4 {
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$4,
},
],
isValueVariable: false,
isTypeVariable: true,
},
Variable$5 {
defs: Array [
ClassNameDefinition$4 {
name: Identifier<"Bar">,
node: ClassDeclaration$3,
},
],
name: "Bar",
references: Array [
Reference$4 {
identifier: Identifier<"Bar">,
isRead: true,
isTypeReference: false,
isValueReference: true,
isWrite: false,
resolved: Variable$5,
},
],
isValueVariable: true,
isTypeVariable: true,
},
Variable$6 {
defs: Array [
ClassNameDefinition$5 {
name: Identifier<"Bar">,
node: ClassDeclaration$3,
},
],
name: "Bar",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
Variable$7 {
defs: Array [
TypeDefinition$6 {
name: Identifier<"T">,
node: TSTypeParameter$4,
},
],
name: "T",
references: Array [
Reference$3 {
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 [
Reference$4,
],
set: Map {
"const" => ImplicitGlobalConstTypeVariable,
"Foo" => Variable$2,
"Bar" => Variable$5,
},
type: "global",
upper: null,
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2,
Variable$5,
],
},
ClassScope$2 {
block: ClassDeclaration$1,
isStrict: true,
references: Array [
Reference$1,
],
set: Map {
"Foo" => Variable$3,
"T" => Variable$4,
},
type: "class",
upper: GlobalScope$1,
variables: Array [
Variable$3,
Variable$4,
],
},
ClassScope$3 {
block: ClassDeclaration$3,
isStrict: true,
references: Array [],
set: Map {
"Bar" => Variable$6,
"T" => Variable$7,
},
type: "class",
upper: GlobalScope$1,
variables: Array [
Variable$6,
Variable$7,
],
},
ClassFieldInitializerScope$4 {
block: TSInstantiationExpression$6,
isStrict: true,
references: Array [
Reference$2,
Reference$3,
],
set: Map {},
type: "class-field-initializer",
upper: ClassScope$3,
variables: Array [],
},
],
}
`;
@@ -0,0 +1,6 @@
function makeBox<T>(value: T) {
return { value };
}

type BoxFunc<T> = typeof makeBox<T>;
const makeStringBox = makeBox<string>;

0 comments on commit 2595ccf

Please sign in to comment.