Skip to content

Commit

Permalink
Update: support class fields (refs eslint/eslint#14343) (#69)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Jul 2, 2021
1 parent 39f8cfc commit 0b4a5f1
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 16 deletions.
19 changes: 19 additions & 0 deletions lib/referencer.js
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);
}
}

MethodDefinition(node) {
this.visitProperty(node);
}
Expand Down
29 changes: 17 additions & 12 deletions lib/scope-manager.js
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
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
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

0 comments on commit 0b4a5f1

Please sign in to comment.