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

Update: support class fields (refs eslint/eslint#14343) #69

Merged
merged 12 commits into from
Jul 2, 2021
19 changes: 19 additions & 0 deletions lib/referencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,12 @@ class Referencer extends esrecurse.Visitor {
this.currentScope().__referencing(node);
}

// eslint-disable-next-line class-methods-use-this
PrivateIdentifier() {

// Do nothing.
}

UpdateExpression(node) {
if (PatternVisitor.isPattern(node.argument)) {
this.currentScope().__referencing(node.argument, Reference.RW, null);
Expand All @@ -453,6 +459,19 @@ class Referencer extends esrecurse.Visitor {
this.visitProperty(node);
}

PropertyDefinition(node) {
const { computed, key, value } = node;

if (computed) {
this.visit(key);
}
if (value) {
this.scopeManager.__nestClassFieldInitializerScope(value);
this.visit(value);
this.close(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a non-blocking observation: the class field initializer scope might have been already closed at this point, triggered by closing a child function/class scope on the same node, but then this will simply do nothing as the current scope is already set to the upper class scope.

}
}

MethodDefinition(node) {
this.visitProperty(node);
}
Expand Down
29 changes: 17 additions & 12 deletions lib/scope-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,21 @@

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

const Scope = require("./scope");
const {
BlockScope,
CatchScope,
ClassFieldInitializerScope,
ClassScope,
ForScope,
FunctionExpressionNameScope,
FunctionScope,
GlobalScope,
ModuleScope,
SwitchScope,
WithScope
} = require("./scope");
const assert = require("assert");

const GlobalScope = Scope.GlobalScope;
const CatchScope = Scope.CatchScope;
const WithScope = Scope.WithScope;
const ModuleScope = Scope.ModuleScope;
const ClassScope = Scope.ClassScope;
const SwitchScope = Scope.SwitchScope;
const FunctionScope = Scope.FunctionScope;
const ForScope = Scope.ForScope;
const FunctionExpressionNameScope = Scope.FunctionExpressionNameScope;
const BlockScope = Scope.BlockScope;

/**
* @class ScopeManager
*/
Expand Down Expand Up @@ -225,6 +226,10 @@ class ScopeManager {
return this.__nestScope(new ClassScope(this, this.__currentScope, node));
}

__nestClassFieldInitializerScope(node) {
return this.__nestScope(new ClassFieldInitializerScope(this, this.__currentScope, node));
}

__nestSwitchScope(node) {
return this.__nestScope(new SwitchScope(this, this.__currentScope, node));
}
Expand Down
11 changes: 9 additions & 2 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class Scope {
* @member {Scope} Scope#variableScope
*/
this.variableScope =
(this.type === "global" || this.type === "function" || this.type === "module") ? this : upperScope.variableScope;
(this.type === "global" || this.type === "function" || this.type === "module" || this.type === "class-field-initializer") ? this : upperScope.variableScope;

/**
* Whether this scope is created by a FunctionExpression.
Expand Down Expand Up @@ -731,6 +731,12 @@ class ClassScope extends Scope {
}
}

class ClassFieldInitializerScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "class-field-initializer", upperScope, block, true);
}
}

module.exports = {
Scope,
GlobalScope,
Expand All @@ -742,7 +748,8 @@ module.exports = {
SwitchScope,
FunctionScope,
ForScope,
ClassScope
ClassScope,
ClassFieldInitializerScope
};

/* vim: set sw=4 ts=4 et tw=80 : */
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"eslint-config-eslint": "^5.0.1",
"eslint-plugin-node": "^9.1.0",
"eslint-release": "^1.0.0",
"eslint-visitor-keys": "^1.2.0",
"espree": "^7.1.0",
"eslint-visitor-keys": "^3.0.0",
"espree": "^8.0.0",
"istanbul": "^0.4.5",
"mocha": "^6.1.4",
"npm-license": "^0.3.3",
Expand Down