Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scope-manager): correct handling for class static blocks #5580

Merged
merged 1 commit into from Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1018,6 +1018,27 @@ export class TestClass {
`,
parserOptions: withMetaParserOptions,
},
// https://github.com/typescript-eslint/typescript-eslint/issues/5577
`
function foo() {}

export class Foo {
constructor() {
foo();
}
}
`,
`
function foo() {}

export class Foo {
static {}

constructor() {
foo();
}
}
`,
],

invalid: [
Expand Down
10 changes: 5 additions & 5 deletions packages/scope-manager/src/referencer/ClassVisitor.ts
Expand Up @@ -322,10 +322,6 @@ class ClassVisitor extends Visitor {
this.visitType(node);
}

protected visitStaticBlock(node: TSESTree.StaticBlock): void {
this.#referencer.scopeManager.nestClassStaticBlockScope(node);
}

/////////////////////
// Visit selectors //
/////////////////////
Expand Down Expand Up @@ -365,7 +361,11 @@ class ClassVisitor extends Visitor {
}

protected StaticBlock(node: TSESTree.StaticBlock): void {
this.visitStaticBlock(node);
this.#referencer.scopeManager.nestClassStaticBlockScope(node);

node.body.forEach(b => this.visit(b));

this.#referencer.close(node);
}
}

Expand Down
@@ -0,0 +1,7 @@
function f() {}

class A {
static {
f();
}
}
@@ -0,0 +1,117 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`class declaration static-external-ref 1`] = `
ScopeManager {
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2 {
defs: Array [
FunctionNameDefinition$1 {
name: Identifier<"f">,
node: FunctionDeclaration$1,
},
],
name: "f",
references: Array [
Reference$1 {
identifier: Identifier<"f">,
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 [
ClassNameDefinition$2 {
name: Identifier<"A">,
node: ClassDeclaration$2,
},
],
name: "A",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
Variable$5 {
defs: Array [
ClassNameDefinition$3 {
name: Identifier<"A">,
node: ClassDeclaration$2,
},
],
name: "A",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
],
scopes: Array [
GlobalScope$1 {
block: Program$3,
isStrict: false,
references: Array [],
set: Map {
"const" => ImplicitGlobalConstTypeVariable,
"f" => Variable$2,
"A" => Variable$4,
},
type: "global",
upper: null,
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2,
Variable$4,
],
},
FunctionScope$2 {
block: FunctionDeclaration$1,
isStrict: false,
references: Array [],
set: Map {
"arguments" => Variable$3,
},
type: "function",
upper: GlobalScope$1,
variables: Array [
Variable$3,
],
},
ClassScope$3 {
block: ClassDeclaration$2,
isStrict: true,
references: Array [],
set: Map {
"A" => Variable$5,
},
type: "class",
upper: GlobalScope$1,
variables: Array [
Variable$5,
],
},
ClassStaticBlockScope$4 {
block: StaticBlock$4,
isStrict: true,
references: Array [
Reference$1,
],
set: Map {},
type: "class-static-block",
upper: ClassScope$3,
variables: Array [],
},
],
}
`;
@@ -0,0 +1,10 @@
// https://github.com/typescript-eslint/typescript-eslint/issues/5577
function f() {}

class A {
static {}

constructor() {
f();
}
}
@@ -0,0 +1,137 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`class declaration static-with-constructor 1`] = `
ScopeManager {
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2 {
defs: Array [
FunctionNameDefinition$1 {
name: Identifier<"f">,
node: FunctionDeclaration$1,
},
],
name: "f",
references: Array [
Reference$1 {
identifier: Identifier<"f">,
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 [
ClassNameDefinition$2 {
name: Identifier<"A">,
node: ClassDeclaration$2,
},
],
name: "A",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
Variable$5 {
defs: Array [
ClassNameDefinition$3 {
name: Identifier<"A">,
node: ClassDeclaration$2,
},
],
name: "A",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
Variable$6 {
defs: Array [],
name: "arguments",
references: Array [],
isValueVariable: true,
isTypeVariable: true,
},
],
scopes: Array [
GlobalScope$1 {
block: Program$3,
isStrict: false,
references: Array [],
set: Map {
"const" => ImplicitGlobalConstTypeVariable,
"f" => Variable$2,
"A" => Variable$4,
},
type: "global",
upper: null,
variables: Array [
ImplicitGlobalConstTypeVariable,
Variable$2,
Variable$4,
],
},
FunctionScope$2 {
block: FunctionDeclaration$1,
isStrict: false,
references: Array [],
set: Map {
"arguments" => Variable$3,
},
type: "function",
upper: GlobalScope$1,
variables: Array [
Variable$3,
],
},
ClassScope$3 {
block: ClassDeclaration$2,
isStrict: true,
references: Array [],
set: Map {
"A" => Variable$5,
},
type: "class",
upper: GlobalScope$1,
variables: Array [
Variable$5,
],
},
ClassStaticBlockScope$4 {
block: StaticBlock$4,
isStrict: true,
references: Array [],
set: Map {},
type: "class-static-block",
upper: ClassScope$3,
variables: Array [],
},
FunctionScope$5 {
block: FunctionExpression$5,
isStrict: true,
references: Array [
Reference$1,
],
set: Map {
"arguments" => Variable$6,
},
type: "function",
upper: ClassScope$3,
variables: Array [
Variable$6,
],
},
],
}
`;