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

Commit 0b4a5f1

Browse files
mysticateanzakasbtmills
authored
Update: support class fields (refs eslint/eslint#14343) (#69)
* Update: support class fields (refs eslint/eslint#14343) * upgrade eslint-visitor-keys * update espree * make function-scope for class field initializers * Update package.json * Update package.json * Update tests/class-fields.js * Update tests/class-fields.js Co-authored-by: Brandon Mills <btmills@users.noreply.github.com> * Remove trailing whitespace to fix lint failure * introduce class-field-initializer scope * fix typo * fix variableScope Co-authored-by: Nicholas C. Zakas <nicholas@nczconsulting.com> Co-authored-by: Brandon Mills <btmills@users.noreply.github.com>
1 parent 39f8cfc commit 0b4a5f1

File tree

5 files changed

+352
-16
lines changed

5 files changed

+352
-16
lines changed

lib/referencer.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ class Referencer extends esrecurse.Visitor {
434434
this.currentScope().__referencing(node);
435435
}
436436

437+
// eslint-disable-next-line class-methods-use-this
438+
PrivateIdentifier() {
439+
440+
// Do nothing.
441+
}
442+
437443
UpdateExpression(node) {
438444
if (PatternVisitor.isPattern(node.argument)) {
439445
this.currentScope().__referencing(node.argument, Reference.RW, null);
@@ -453,6 +459,19 @@ class Referencer extends esrecurse.Visitor {
453459
this.visitProperty(node);
454460
}
455461

462+
PropertyDefinition(node) {
463+
const { computed, key, value } = node;
464+
465+
if (computed) {
466+
this.visit(key);
467+
}
468+
if (value) {
469+
this.scopeManager.__nestClassFieldInitializerScope(value);
470+
this.visit(value);
471+
this.close(value);
472+
}
473+
}
474+
456475
MethodDefinition(node) {
457476
this.visitProperty(node);
458477
}

lib/scope-manager.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@
2525

2626
/* eslint-disable no-underscore-dangle */
2727

28-
const Scope = require("./scope");
28+
const {
29+
BlockScope,
30+
CatchScope,
31+
ClassFieldInitializerScope,
32+
ClassScope,
33+
ForScope,
34+
FunctionExpressionNameScope,
35+
FunctionScope,
36+
GlobalScope,
37+
ModuleScope,
38+
SwitchScope,
39+
WithScope
40+
} = require("./scope");
2941
const assert = require("assert");
3042

31-
const GlobalScope = Scope.GlobalScope;
32-
const CatchScope = Scope.CatchScope;
33-
const WithScope = Scope.WithScope;
34-
const ModuleScope = Scope.ModuleScope;
35-
const ClassScope = Scope.ClassScope;
36-
const SwitchScope = Scope.SwitchScope;
37-
const FunctionScope = Scope.FunctionScope;
38-
const ForScope = Scope.ForScope;
39-
const FunctionExpressionNameScope = Scope.FunctionExpressionNameScope;
40-
const BlockScope = Scope.BlockScope;
41-
4243
/**
4344
* @class ScopeManager
4445
*/
@@ -225,6 +226,10 @@ class ScopeManager {
225226
return this.__nestScope(new ClassScope(this, this.__currentScope, node));
226227
}
227228

229+
__nestClassFieldInitializerScope(node) {
230+
return this.__nestScope(new ClassFieldInitializerScope(this, this.__currentScope, node));
231+
}
232+
228233
__nestSwitchScope(node) {
229234
return this.__nestScope(new SwitchScope(this, this.__currentScope, node));
230235
}

lib/scope.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class Scope {
224224
* @member {Scope} Scope#variableScope
225225
*/
226226
this.variableScope =
227-
(this.type === "global" || this.type === "function" || this.type === "module") ? this : upperScope.variableScope;
227+
(this.type === "global" || this.type === "function" || this.type === "module" || this.type === "class-field-initializer") ? this : upperScope.variableScope;
228228

229229
/**
230230
* Whether this scope is created by a FunctionExpression.
@@ -731,6 +731,12 @@ class ClassScope extends Scope {
731731
}
732732
}
733733

734+
class ClassFieldInitializerScope extends Scope {
735+
constructor(scopeManager, upperScope, block) {
736+
super(scopeManager, "class-field-initializer", upperScope, block, true);
737+
}
738+
}
739+
734740
module.exports = {
735741
Scope,
736742
GlobalScope,
@@ -742,7 +748,8 @@ module.exports = {
742748
SwitchScope,
743749
FunctionScope,
744750
ForScope,
745-
ClassScope
751+
ClassScope,
752+
ClassFieldInitializerScope
746753
};
747754

748755
/* 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
@@ -37,8 +37,8 @@
3737
"eslint-config-eslint": "^5.0.1",
3838
"eslint-plugin-node": "^9.1.0",
3939
"eslint-release": "^1.0.0",
40-
"eslint-visitor-keys": "^1.2.0",
41-
"espree": "^7.1.0",
40+
"eslint-visitor-keys": "^3.0.0",
41+
"espree": "^8.0.0",
4242
"istanbul": "^0.4.5",
4343
"mocha": "^6.1.4",
4444
"npm-license": "^0.3.3",

0 commit comments

Comments
 (0)