feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -21,18 +21,17 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
const Variable = require("./variable");
import Variable from "./variable.js";
/**
* @class Definition
* @constructor Definition
*/
class Definition {
constructor(type, name, node, parent, index, kind) {
/**
* @member {String} Definition#type - type of the occurrence (e.g. "Parameter", "Variable", ...).
* @member {string} Definition#type - type of the occurrence (e.g. "Parameter", "Variable", ...).
*/
this.type = type;
@@ -52,19 +51,19 @@ class Definition {
this.parent = parent;
/**
* @member {Number?} Definition#index - the index in the declaration statement.
* @member {number?} Definition#index - the index in the declaration statement.
*/
this.index = index;
/**
* @member {String?} Definition#kind - the kind of the declaration statement.
* @member {string?} Definition#kind - the kind of the declaration statement.
*/
this.kind = kind;
}
}
/**
* @class ParameterDefinition
* @constructor ParameterDefinition
*/
class ParameterDefinition extends Definition {
constructor(name, node, index, rest) {
@@ -78,7 +77,7 @@ class ParameterDefinition extends Definition {
}
}
module.exports = {
export {
ParameterDefinition,
Definition
};

View File

@@ -45,18 +45,15 @@
* The main interface is the {@link analyze} function.
* @module escope
*/
"use strict";
/* eslint no-underscore-dangle: ["error", { "allow": ["__currentScope"] }] */
import { assert } from "./assert.js";
const assert = require("assert");
import ScopeManager from "./scope-manager.js";
import Referencer from "./referencer.js";
import Reference from "./reference.js";
import Variable from "./variable.js";
const ScopeManager = require("./scope-manager");
const Referencer = require("./referencer");
const Reference = require("./reference");
const Variable = require("./variable");
const Scope = require("./scope").Scope;
const version = require("../package.json").version;
import eslintScopeVersion from "./version.js";
/**
* Set the default options
@@ -65,10 +62,9 @@ const version = require("../package.json").version;
function defaultOptions() {
return {
optimistic: false,
directive: false,
nodejsScope: false,
impliedStrict: false,
sourceType: "script", // one of ['script', 'module']
sourceType: "script", // one of ['script', 'module', 'commonjs']
ecmaVersion: 5,
childVisitorKeys: null,
fallback: "iteration"
@@ -77,15 +73,15 @@ function defaultOptions() {
/**
* Preform deep update on option object
* @param {Object} target - Options
* @param {Object} override - Updates
* @param {Object} target Options
* @param {Object} override Updates
* @returns {Object} Updated options
*/
function updateDeeply(target, override) {
/**
* Is hash object
* @param {Object} value - Test value
* @param {Object} value Test value
* @returns {boolean} Result
*/
function isHashObject(value) {
@@ -93,7 +89,7 @@ function updateDeeply(target, override) {
}
for (const key in override) {
if (Object.prototype.hasOwnProperty.call(override, key)) {
if (Object.hasOwn(override, key)) {
const val = override[key];
if (isHashObject(val)) {
@@ -114,20 +110,19 @@ function updateDeeply(target, override) {
* Main interface function. Takes an Espree syntax tree and returns the
* analyzed scopes.
* @function analyze
* @param {espree.Tree} tree - Abstract Syntax Tree
* @param {Object} providedOptions - Options that tailor the scope analysis
* @param {boolean} [providedOptions.optimistic=false] - the optimistic flag
* @param {boolean} [providedOptions.directive=false]- the directive flag
* @param {boolean} [providedOptions.ignoreEval=false]- whether to check 'eval()' calls
* @param {boolean} [providedOptions.nodejsScope=false]- whether the whole
* @param {espree.Tree} tree Abstract Syntax Tree
* @param {Object} providedOptions Options that tailor the scope analysis
* @param {boolean} [providedOptions.optimistic=false] the optimistic flag
* @param {boolean} [providedOptions.ignoreEval=false] whether to check 'eval()' calls
* @param {boolean} [providedOptions.nodejsScope=false] whether the whole
* script is executed under node.js environment. When enabled, escope adds
* a function scope immediately following the global scope.
* @param {boolean} [providedOptions.impliedStrict=false]- implied strict mode
* @param {boolean} [providedOptions.impliedStrict=false] implied strict mode
* (if ecmaVersion >= 5).
* @param {string} [providedOptions.sourceType='script']- the source type of the script. one of 'script' and 'module'
* @param {number} [providedOptions.ecmaVersion=5]- which ECMAScript version is considered
* @param {Object} [providedOptions.childVisitorKeys=null] - Additional known visitor keys. See [esrecurse](https://github.com/estools/esrecurse)'s the `childVisitorKeys` option.
* @param {string} [providedOptions.fallback='iteration'] - A kind of the fallback in order to encounter with unknown node. See [esrecurse](https://github.com/estools/esrecurse)'s the `fallback` option.
* @param {string} [providedOptions.sourceType='script'] the source type of the script. one of 'script', 'module', and 'commonjs'
* @param {number} [providedOptions.ecmaVersion=5] which ECMAScript version is considered
* @param {Object} [providedOptions.childVisitorKeys=null] Additional known visitor keys. See [esrecurse](https://github.com/estools/esrecurse)'s the `childVisitorKeys` option.
* @param {string} [providedOptions.fallback='iteration'] A kind of the fallback in order to encounter with unknown node. See [esrecurse](https://github.com/estools/esrecurse)'s the `fallback` option.
* @returns {ScopeManager} ScopeManager
*/
function analyze(tree, providedOptions) {
@@ -142,10 +137,10 @@ function analyze(tree, providedOptions) {
return scopeManager;
}
module.exports = {
export {
/** @name module:escope.version */
version,
eslintScopeVersion as version,
/** @name module:escope.Reference */
Reference,
@@ -153,13 +148,22 @@ module.exports = {
/** @name module:escope.Variable */
Variable,
/** @name module:escope.Scope */
Scope,
/** @name module:escope.ScopeManager */
ScopeManager,
/** @name module:escope.Referencer */
Referencer,
analyze
};
/** @name module:escope.Definition */
export { Definition } from "./definition.js";
/** @name module:escope.PatternVisitor */
export { default as PatternVisitor } from "./pattern-visitor.js";
/** @name module:escope.Scope */
export { Scope } from "./scope.js";
/* vim: set sw=4 ts=4 et tw=80 : */

View File

@@ -21,22 +21,24 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
/* eslint-disable no-undefined */
import estraverse from "estraverse";
import esrecurse from "esrecurse";
const Syntax = require("estraverse").Syntax;
const esrecurse = require("esrecurse");
const { Syntax } = estraverse;
/**
* Get last array element
* @param {array} xs - array
* @param {Array} xs array
* @returns {any} Last elment
*/
function getLast(xs) {
return xs[xs.length - 1] || null;
return xs.at(-1) || null;
}
/**
* Visitor for destructuring patterns.
*/
class PatternVisitor extends esrecurse.Visitor {
static isPattern(node) {
const nodeType = node.type;
@@ -65,7 +67,7 @@ class PatternVisitor extends esrecurse.Visitor {
this.callback(pattern, {
topLevel: pattern === this.rootPattern,
rest: lastRestElement !== null && lastRestElement !== undefined && lastRestElement.argument === pattern,
rest: lastRestElement !== null && lastRestElement !== void 0 && lastRestElement.argument === pattern,
assignments: this.assignments
});
}
@@ -147,6 +149,6 @@ class PatternVisitor extends esrecurse.Visitor {
}
}
module.exports = PatternVisitor;
export default PatternVisitor;
/* vim: set sw=4 ts=4 et tw=80 : */

View File

@@ -21,7 +21,6 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
const READ = 0x1;
const WRITE = 0x2;
@@ -29,7 +28,7 @@ const RW = READ | WRITE;
/**
* A Reference represents a single occurrence of an identifier in code.
* @class Reference
* @constructor Reference
*/
class Reference {
constructor(ident, scope, flag, writeExpr, maybeImplicitGlobal, partial, init) {
@@ -91,7 +90,7 @@ class Reference {
/**
* Whether the reference is static.
* @method Reference#isStatic
* @function Reference#isStatic
* @returns {boolean} static
*/
isStatic() {
@@ -100,7 +99,7 @@ class Reference {
/**
* Whether the reference is writeable.
* @method Reference#isWrite
* @function Reference#isWrite
* @returns {boolean} write
*/
isWrite() {
@@ -109,7 +108,7 @@ class Reference {
/**
* Whether the reference is readable.
* @method Reference#isRead
* @function Reference#isRead
* @returns {boolean} read
*/
isRead() {
@@ -118,7 +117,7 @@ class Reference {
/**
* Whether the reference is read-only.
* @method Reference#isReadOnly
* @function Reference#isReadOnly
* @returns {boolean} read only
*/
isReadOnly() {
@@ -127,7 +126,7 @@ class Reference {
/**
* Whether the reference is write-only.
* @method Reference#isWriteOnly
* @function Reference#isWriteOnly
* @returns {boolean} write only
*/
isWriteOnly() {
@@ -136,7 +135,7 @@ class Reference {
/**
* Whether the reference is read-write.
* @method Reference#isReadWrite
* @function Reference#isReadWrite
* @returns {boolean} read write
*/
isReadWrite() {
@@ -162,6 +161,6 @@ Reference.WRITE = WRITE;
*/
Reference.RW = RW;
module.exports = Reference;
export default Reference;
/* vim: set sw=4 ts=4 et tw=80 : */

View File

@@ -21,28 +21,23 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-undefined */
import estraverse from "estraverse";
import esrecurse from "esrecurse";
import Reference from "./reference.js";
import Variable from "./variable.js";
import PatternVisitor from "./pattern-visitor.js";
import { Definition, ParameterDefinition } from "./definition.js";
import { assert } from "./assert.js";
const Syntax = require("estraverse").Syntax;
const esrecurse = require("esrecurse");
const Reference = require("./reference");
const Variable = require("./variable");
const PatternVisitor = require("./pattern-visitor");
const definition = require("./definition");
const assert = require("assert");
const ParameterDefinition = definition.ParameterDefinition;
const Definition = definition.Definition;
const { Syntax } = estraverse;
/**
* Traverse identifier in pattern
* @param {Object} options - options
* @param {pattern} rootPattern - root pattern
* @param {Refencer} referencer - referencer
* @param {callback} callback - callback
* @param {Object} options options
* @param {pattern} rootPattern root pattern
* @param {Refencer} referencer referencer
* @param {callback} callback callback
* @returns {void}
*/
function traverseIdentifierInPattern(options, rootPattern, referencer, callback) {
@@ -53,7 +48,7 @@ function traverseIdentifierInPattern(options, rootPattern, referencer, callback)
visitor.visit(rootPattern);
// Process the right hand nodes recursively.
if (referencer !== null && referencer !== undefined) {
if (referencer !== null && referencer !== void 0) {
visitor.rightHandNodes.forEach(referencer.visit, referencer);
}
}
@@ -64,6 +59,9 @@ function traverseIdentifierInPattern(options, rootPattern, referencer, callback)
// FIXME: Now, we don't create module environment, because the context is
// implementation dependent.
/**
* Visitor for import specifiers.
*/
class Importer extends esrecurse.Visitor {
constructor(declaration, referencer) {
super(null, referencer.options);
@@ -110,7 +108,9 @@ class Importer extends esrecurse.Visitor {
}
}
// Referencing variables and creating bindings.
/**
* Referencing variables and creating bindings.
*/
class Referencer extends esrecurse.Visitor {
constructor(options, scopeManager) {
super(null, options);
@@ -209,8 +209,8 @@ class Referencer extends esrecurse.Visitor {
/**
* Visit pattern callback
* @param {pattern} pattern - pattern
* @param {Object} info - info
* @param {pattern} pattern pattern
* @param {Object} info info
* @returns {void}
*/
function visitPatternCallback(pattern, info) {
@@ -274,8 +274,6 @@ class Referencer extends esrecurse.Visitor {
));
}
this.visit(node.superClass);
this.scopeManager.__nestClassScope(node);
if (node.id) {
@@ -286,6 +284,8 @@ class Referencer extends esrecurse.Visitor {
node
));
}
this.visit(node.superClass);
this.visit(node.body);
this.close(node);
@@ -395,7 +395,7 @@ class Referencer extends esrecurse.Visitor {
this.currentScope().__define(pattern,
new Definition(
Variable.CatchClause,
node.param,
pattern,
node,
null,
null,
@@ -411,7 +411,7 @@ class Referencer extends esrecurse.Visitor {
Program(node) {
this.scopeManager.__nestGlobalScope(node);
if (this.scopeManager.__isNodejsScope()) {
if (this.scopeManager.isGlobalReturn()) {
// Force strictness of GlobalScope to false when using node.js scope.
this.currentScope().isStrict = false;
@@ -434,6 +434,12 @@ class Referencer extends esrecurse.Visitor {
this.currentScope().__referencing(node);
}
// eslint-disable-next-line class-methods-use-this -- Desired as instance method
PrivateIdentifier() {
// Do nothing.
}
UpdateExpression(node) {
if (PatternVisitor.isPattern(node.argument)) {
this.currentScope().__referencing(node.argument, Reference.RW, null);
@@ -453,13 +459,34 @@ 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);
}
}
StaticBlock(node) {
this.scopeManager.__nestClassStaticBlockScope(node);
this.visitChildren(node);
this.close(node);
}
MethodDefinition(node) {
this.visitProperty(node);
}
BreakStatement() {} // eslint-disable-line class-methods-use-this
BreakStatement() {} // eslint-disable-line class-methods-use-this -- Desired as instance method
ContinueStatement() {} // eslint-disable-line class-methods-use-this
ContinueStatement() {} // eslint-disable-line class-methods-use-this -- Desired as instance method
LabeledStatement(node) {
this.visit(node.body);
@@ -618,12 +645,12 @@ class Referencer extends esrecurse.Visitor {
this.visit(local);
}
MetaProperty() { // eslint-disable-line class-methods-use-this
MetaProperty() { // eslint-disable-line class-methods-use-this -- Desired as instance method
// do nothing.
}
}
module.exports = Referencer;
export default Referencer;
/* vim: set sw=4 ts=4 et tw=80 : */

View File

@@ -21,26 +21,25 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
/* eslint-disable no-underscore-dangle */
const Scope = 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;
import {
BlockScope,
CatchScope,
ClassFieldInitializerScope,
ClassStaticBlockScope,
ClassScope,
ForScope,
FunctionExpressionNameScope,
FunctionScope,
GlobalScope,
ModuleScope,
SwitchScope,
WithScope
} from "./scope.js";
import { assert } from "./assert.js";
/**
* @class ScopeManager
* @constructor ScopeManager
*/
class ScopeManager {
constructor(options) {
@@ -52,10 +51,6 @@ class ScopeManager {
this.__declaredVariables = new WeakMap();
}
__useDirective() {
return this.__options.directive;
}
__isOptimistic() {
return this.__options.optimistic;
}
@@ -64,8 +59,8 @@ class ScopeManager {
return this.__options.ignoreEval;
}
__isNodejsScope() {
return this.__options.nodejsScope;
isGlobalReturn() {
return this.__options.nodejsScope || this.__options.sourceType === "commonjs";
}
isModule() {
@@ -91,8 +86,7 @@ class ScopeManager {
* "are declared by the node" means the node is same as `Variable.defs[].node` or `Variable.defs[].parent`.
* If the node declares nothing, this method returns an empty array.
* CAUTION: This API is experimental. See https://github.com/estools/escope/pull/69 for more details.
*
* @param {Espree.Node} node - a node to get.
* @param {Espree.Node} node a node to get.
* @returns {Variable[]} variables that declared by the node.
*/
getDeclaredVariables(node) {
@@ -101,16 +95,16 @@ class ScopeManager {
/**
* acquire scope from node.
* @method ScopeManager#acquire
* @param {Espree.Node} node - node for the acquired scope.
* @param {boolean=} inner - look up the most inner scope, default value is false.
* @function ScopeManager#acquire
* @param {Espree.Node} node node for the acquired scope.
* @param {?boolean} [inner=false] look up the most inner scope, default value is false.
* @returns {Scope?} Scope from node
*/
acquire(node, inner) {
/**
* predicate
* @param {Scope} testScope - scope to test
* @param {Scope} testScope scope to test
* @returns {boolean} predicate
*/
function predicate(testScope) {
@@ -155,8 +149,8 @@ class ScopeManager {
/**
* acquire all scopes from node.
* @method ScopeManager#acquireAll
* @param {Espree.Node} node - node for the acquired scope.
* @function ScopeManager#acquireAll
* @param {Espree.Node} node node for the acquired scope.
* @returns {Scopes?} Scope array
*/
acquireAll(node) {
@@ -165,9 +159,9 @@ class ScopeManager {
/**
* release the node.
* @method ScopeManager#release
* @param {Espree.Node} node - releasing node.
* @param {boolean=} inner - look up the most inner scope, default value is false.
* @function ScopeManager#release
* @param {Espree.Node} node releasing node.
* @param {?boolean} [inner=false] look up the most inner scope, default value is false.
* @returns {Scope?} upper scope for the node.
*/
release(node, inner) {
@@ -184,9 +178,9 @@ class ScopeManager {
return null;
}
attach() { } // eslint-disable-line class-methods-use-this
attach() { } // eslint-disable-line class-methods-use-this -- Desired as instance method
detach() { } // eslint-disable-line class-methods-use-this
detach() { } // eslint-disable-line class-methods-use-this -- Desired as instance method
__nestScope(scope) {
if (scope instanceof GlobalScope) {
@@ -225,6 +219,14 @@ class ScopeManager {
return this.__nestScope(new ClassScope(this, this.__currentScope, node));
}
__nestClassFieldInitializerScope(node) {
return this.__nestScope(new ClassFieldInitializerScope(this, this.__currentScope, node));
}
__nestClassStaticBlockScope(node) {
return this.__nestScope(new ClassStaticBlockScope(this, this.__currentScope, node));
}
__nestSwitchScope(node) {
return this.__nestScope(new SwitchScope(this, this.__currentScope, node));
}
@@ -242,6 +244,6 @@ class ScopeManager {
}
}
module.exports = ScopeManager;
export default ScopeManager;
/* vim: set sw=4 ts=4 et tw=80 : */

View File

@@ -21,27 +21,24 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-undefined */
import estraverse from "estraverse";
const Syntax = require("estraverse").Syntax;
import Reference from "./reference.js";
import Variable from "./variable.js";
import { Definition } from "./definition.js";
import { assert } from "./assert.js";
const Reference = require("./reference");
const Variable = require("./variable");
const Definition = require("./definition").Definition;
const assert = require("assert");
const { Syntax } = estraverse;
/**
* Test if scope is struct
* @param {Scope} scope - scope
* @param {Block} block - block
* @param {boolean} isMethodDefinition - is method definition
* @param {boolean} useDirective - use directive
* @param {Scope} scope scope
* @param {Block} block block
* @param {boolean} isMethodDefinition is method definition
* @returns {boolean} is strict scope
*/
function isStrictScope(scope, block, isMethodDefinition, useDirective) {
function isStrictScope(scope, block, isMethodDefinition) {
let body;
// When upper scope is exists and strict, inner scope is also strict.
@@ -81,48 +78,36 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) {
return false;
}
// Search 'use strict' directive.
if (useDirective) {
for (let i = 0, iz = body.body.length; i < iz; ++i) {
const stmt = body.body[i];
// Search for a 'use strict' directive.
for (let i = 0, iz = body.body.length; i < iz; ++i) {
const stmt = body.body[i];
if (stmt.type !== Syntax.DirectiveStatement) {
break;
}
if (stmt.raw === "\"use strict\"" || stmt.raw === "'use strict'") {
return true;
}
/*
* Check if the current statement is a directive.
* If it isn't, then we're past the directive prologue
* so stop the search because directives cannot
* appear after this point.
*
* Some parsers set `directive:null` on non-directive
* statements, so the `typeof` check is safer than
* checking for property existence.
*/
if (typeof stmt.directive !== "string") {
break;
}
} else {
for (let i = 0, iz = body.body.length; i < iz; ++i) {
const stmt = body.body[i];
if (stmt.type !== Syntax.ExpressionStatement) {
break;
}
const expr = stmt.expression;
if (expr.type !== Syntax.Literal || typeof expr.value !== "string") {
break;
}
if (expr.raw !== null && expr.raw !== undefined) {
if (expr.raw === "\"use strict\"" || expr.raw === "'use strict'") {
return true;
}
} else {
if (expr.value === "use strict") {
return true;
}
}
if (stmt.directive === "use strict") {
return true;
}
}
return false;
}
/**
* Register scope
* @param {ScopeManager} scopeManager - scope manager
* @param {Scope} scope - scope
* @param {ScopeManager} scopeManager scope manager
* @param {Scope} scope scope
* @returns {void}
*/
function registerScope(scopeManager, scope) {
@@ -139,7 +124,7 @@ function registerScope(scopeManager, scope) {
/**
* Should be statically
* @param {Object} def - def
* @param {Object} def def
* @returns {boolean} should be statically
*/
function shouldBeStatically(def) {
@@ -150,14 +135,15 @@ function shouldBeStatically(def) {
}
/**
* @class Scope
* @constructor Scope
*/
class Scope {
constructor(scopeManager, type, upperScope, block, isMethodDefinition) {
/**
* One of 'module', 'block', 'switch', 'function', 'catch', 'with', 'function', 'class', 'global'.
* @member {String} Scope#type
* One of "global", "module", "function", "function-expression-name", "block", "switch", "catch", "with", "for",
* "class", "class-field-initializer", "class-static-block".
* @member {string} Scope#type
*/
this.type = type;
@@ -171,7 +157,8 @@ class Scope {
/**
* The tainted variables of this scope, as <code>{ Variable.name :
* boolean }</code>.
* @member {Map} Scope#taints */
* @member {Map} Scope#taints
*/
this.taints = new Map();
/**
@@ -224,7 +211,13 @@ 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 === "module" ||
this.type === "function" ||
this.type === "class-field-initializer" ||
this.type === "class-static-block"
? this
: upperScope.variableScope;
/**
* Whether this scope is created by a FunctionExpression.
@@ -255,7 +248,9 @@ class Scope {
* Whether 'use strict' is in effect in this scope.
* @member {boolean} Scope#isStrict
*/
this.isStrict = isStrictScope(this, block, isMethodDefinition, scopeManager.__useDirective());
this.isStrict = scopeManager.isStrictModeSupported()
? isStrictScope(this, block, isMethodDefinition)
: false;
/**
* List of nested {@link Scope}s.
@@ -342,7 +337,7 @@ class Scope {
// To override by function scopes.
// References in default parameters isn't resolved to variables which are in their function body.
__isValidResolution(ref, variable) { // eslint-disable-line class-methods-use-this, no-unused-vars
__isValidResolution(ref, variable) { // eslint-disable-line class-methods-use-this, no-unused-vars -- Desired as instance method with signature
return true;
}
@@ -376,17 +371,17 @@ class Scope {
}
__addDeclaredVariablesOfNode(variable, node) {
if (node === null || node === undefined) {
if (node === null || node === void 0) {
return;
}
let variables = this.__declaredVariables.get(node);
if (variables === null || variables === undefined) {
if (variables === null || variables === void 0) {
variables = [];
this.__declaredVariables.set(node, variables);
}
if (variables.indexOf(variable) === -1) {
if (!variables.includes(variable)) {
variables.push(variable);
}
}
@@ -461,8 +456,8 @@ class Scope {
/**
* returns resolved {Reference}
* @method Scope#resolve
* @param {Espree.Identifier} ident - identifier to be resolved.
* @function Scope#resolve
* @param {Espree.Identifier} ident identifier to be resolved.
* @returns {Reference} reference
*/
resolve(ident) {
@@ -481,7 +476,7 @@ class Scope {
/**
* returns this scope is static
* @method Scope#isStatic
* @function Scope#isStatic
* @returns {boolean} static
*/
isStatic() {
@@ -490,19 +485,19 @@ class Scope {
/**
* returns this scope has materialized arguments
* @method Scope#isArgumentsMaterialized
* @function Scope#isArgumentsMaterialized
* @returns {boolean} arguemnts materialized
*/
isArgumentsMaterialized() { // eslint-disable-line class-methods-use-this
isArgumentsMaterialized() { // eslint-disable-line class-methods-use-this -- Desired as instance method
return true;
}
/**
* returns this scope has materialized `this` reference
* @method Scope#isThisMaterialized
* @function Scope#isThisMaterialized
* @returns {boolean} this materialized
*/
isThisMaterialized() { // eslint-disable-line class-methods-use-this
isThisMaterialized() { // eslint-disable-line class-methods-use-this -- Desired as instance method
return true;
}
@@ -519,6 +514,9 @@ class Scope {
}
}
/**
* Global scope.
*/
class GlobalScope extends Scope {
constructor(scopeManager, block) {
super(scopeManager, "global", null, block, false);
@@ -527,10 +525,10 @@ class GlobalScope extends Scope {
variables: [],
/**
* List of {@link Reference}s that are left to be resolved (i.e. which
* need to be linked to the variable they refer to).
* @member {Reference[]} Scope#implicit#left
*/
* List of {@link Reference}s that are left to be resolved (i.e. which
* need to be linked to the variable they refer to).
* @member {Reference[]} Scope#implicit#left
*/
left: []
};
}
@@ -580,12 +578,18 @@ class GlobalScope extends Scope {
}
}
/**
* Module scope.
*/
class ModuleScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "module", upperScope, block, false);
}
}
/**
* Function expression name scope.
*/
class FunctionExpressionNameScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "function-expression-name", upperScope, block, false);
@@ -602,12 +606,18 @@ class FunctionExpressionNameScope extends Scope {
}
}
/**
* Catch scope.
*/
class CatchScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "catch", upperScope, block, false);
}
}
/**
* With statement scope.
*/
class WithScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "with", upperScope, block, false);
@@ -630,18 +640,27 @@ class WithScope extends Scope {
}
}
/**
* Block scope.
*/
class BlockScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "block", upperScope, block, false);
}
}
/**
* Switch scope.
*/
class SwitchScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "switch", upperScope, block, false);
}
}
/**
* Function scope.
*/
class FunctionScope extends Scope {
constructor(scopeManager, upperScope, block, isMethodDefinition) {
super(scopeManager, "function", upperScope, block, isMethodDefinition);
@@ -719,19 +738,43 @@ class FunctionScope extends Scope {
}
}
/**
* Scope of for, for-in, and for-of statements.
*/
class ForScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "for", upperScope, block, false);
}
}
/**
* Class scope.
*/
class ClassScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "class", upperScope, block, false);
}
}
module.exports = {
/**
* Class field initializer scope.
*/
class ClassFieldInitializerScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "class-field-initializer", upperScope, block, true);
}
}
/**
* Class static block scope.
*/
class ClassStaticBlockScope extends Scope {
constructor(scopeManager, upperScope, block) {
super(scopeManager, "class-static-block", upperScope, block, true);
}
}
export {
Scope,
GlobalScope,
ModuleScope,
@@ -742,7 +785,9 @@ module.exports = {
SwitchScope,
FunctionScope,
ForScope,
ClassScope
ClassScope,
ClassFieldInitializerScope,
ClassStaticBlockScope
};
/* vim: set sw=4 ts=4 et tw=80 : */

View File

@@ -21,19 +21,18 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
"use strict";
/**
* A Variable represents a locally scoped identifier. These include arguments to
* functions.
* @class Variable
* @constructor Variable
*/
class Variable {
constructor(name, scope) {
/**
* The variable name, as given in the source code.
* @member {String} Variable#name
* @member {string} Variable#name
*/
this.name = name;
@@ -83,6 +82,6 @@ Variable.Variable = "Variable";
Variable.ImportBinding = "ImportBinding";
Variable.ImplicitGlobalVariable = "ImplicitGlobalVariable";
module.exports = Variable;
export default Variable;
/* vim: set sw=4 ts=4 et tw=80 : */