Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 22a55c0

Browse files
authored
feat!: support class static blocks (#80)
1 parent 4aafb61 commit 22a55c0

File tree

5 files changed

+907
-5
lines changed

5 files changed

+907
-5
lines changed

lib/referencer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ class Referencer extends esrecurse.Visitor {
470470
}
471471
}
472472

473+
StaticBlock(node) {
474+
this.scopeManager.__nestClassStaticBlockScope(node);
475+
476+
this.visitChildren(node);
477+
478+
this.close(node);
479+
}
480+
473481
MethodDefinition(node) {
474482
this.visitProperty(node);
475483
}

lib/scope-manager.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
BlockScope,
2929
CatchScope,
3030
ClassFieldInitializerScope,
31+
ClassStaticBlockScope,
3132
ClassScope,
3233
ForScope,
3334
FunctionExpressionNameScope,
@@ -228,6 +229,10 @@ class ScopeManager {
228229
return this.__nestScope(new ClassFieldInitializerScope(this, this.__currentScope, node));
229230
}
230231

232+
__nestClassStaticBlockScope(node) {
233+
return this.__nestScope(new ClassStaticBlockScope(this, this.__currentScope, node));
234+
}
235+
231236
__nestSwitchScope(node) {
232237
return this.__nestScope(new SwitchScope(this, this.__currentScope, node));
233238
}

lib/scope.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ class Scope {
157157
constructor(scopeManager, type, upperScope, block, isMethodDefinition) {
158158

159159
/**
160-
* One of 'module', 'block', 'switch', 'function', 'catch', 'with', 'function', 'class', 'global'.
160+
* One of "global", "module", "function", "function-expression-name", "block", "switch", "catch", "with", "for",
161+
* "class", "class-field-initializer", "class-static-block".
161162
* @member {string} Scope#type
162163
*/
163164
this.type = type;
@@ -225,7 +226,13 @@ class Scope {
225226
* @member {Scope} Scope#variableScope
226227
*/
227228
this.variableScope =
228-
(this.type === "global" || this.type === "function" || this.type === "module" || this.type === "class-field-initializer") ? this : upperScope.variableScope;
229+
this.type === "global" ||
230+
this.type === "module" ||
231+
this.type === "function" ||
232+
this.type === "class-field-initializer" ||
233+
this.type === "class-static-block"
234+
? this
235+
: upperScope.variableScope;
229236

230237
/**
231238
* Whether this scope is created by a FunctionExpression.
@@ -738,6 +745,12 @@ class ClassFieldInitializerScope extends Scope {
738745
}
739746
}
740747

748+
class ClassStaticBlockScope extends Scope {
749+
constructor(scopeManager, upperScope, block) {
750+
super(scopeManager, "class-static-block", upperScope, block, true);
751+
}
752+
}
753+
741754
export {
742755
Scope,
743756
GlobalScope,
@@ -750,7 +763,8 @@ export {
750763
FunctionScope,
751764
ForScope,
752765
ClassScope,
753-
ClassFieldInitializerScope
766+
ClassFieldInitializerScope,
767+
ClassStaticBlockScope
754768
};
755769

756770
/* vim: set sw=4 ts=4 et tw=80 : */

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
"eslint-plugin-jsdoc": "^35.4.1",
5252
"eslint-plugin-node": "^11.1.0",
5353
"eslint-release": "^3.2.0",
54-
"eslint-visitor-keys": "^3.0.0",
55-
"espree": "^8.0.0",
54+
"eslint-visitor-keys": "^3.1.0",
55+
"espree": "^9.0.0",
5656
"mocha": "^9.0.1",
5757
"npm-license": "^0.3.3",
5858
"rollup": "^2.52.7",

0 commit comments

Comments
 (0)