ScopeManager
This document was written based on the implementation of eslint-scope, a fork of escope, and deprecates some members ESLint is not using.
ScopeManager interface
ScopeManager object has all variable scopes.
Fields
scopes
- Type:
Scope[] - Description: All scopes.
globalScope
- Type:
Scope - Description: The root scope.
Methods
acquire(node, inner = false)
- Parameters:
node(ASTNode) … An AST node to get their scope.inner(boolean) … If the node has multiple scope, this returns the outermost scope normally. Ifinneristruethen this returns the innermost scope. Default isfalse.
- Return type:
Scope | null - Description: Get the scope of a given AST node. The gotten scope’s
blockproperty is the node. This method never returnsfunction-expression-namescope. If the node does not have their scope, this returnsnull.
getDeclaredVariables(node)
- Parameters:
node(ASTNode) … An AST node to get their variables.
- Return type:
Variable[] - Description: Get the variables that a given AST node defines. The gotten variables’
def[].node/def[].parentproperty is the node. If the node does not define any variable, this returns an empty array.
Deprecated members
Those members are defined but not used in ESLint.
isModule()
- Parameters:
- Return type:
boolean - Description:
trueif this program is module.
isImpliedStrict()
- Parameters:
- Return type:
boolean - Description:
trueif this program is strict mode implicitly. I.e.,options.impliedStrict === true.
isStrictModeSupported()
- Parameters:
- Return type:
boolean - Description:
trueif this program supports strict mode. I.e.,options.ecmaVersion >= 5.
acquireAll(node)
- Parameters:
node(ASTNode) … An AST node to get their scope.
- Return type:
Scope[] | null - Description: Get the scopes of a given AST node. The gotten scopes’
blockproperty is the node. If the node does not have their scope, this returnsnull.
Scope interface
Scope object has all variables and references in the scope.
Fields
type
- Type:
string - Description: The type of this scope. This is one of
"block","catch","class","class-field-initializer","class-static-block","for","function","function-expression-name","global","module","switch","with".
isStrict
- Type:
boolean - Description:
trueif this scope is strict mode.
upper
- Type:
Scope | null - Description: The parent scope. If this is the global scope then this property is
null.
childScopes
- Type:
Scope[] - Description: The array of child scopes. This does not include grandchild scopes.
variableScope
- Type:
Scope - Description: The nearest ancestor whose
typeis one of"class-field-initializer","class-static-block","function","global", or"module". For the aforementioned scopes this is a self-reference.
This represents the lowest enclosing function or top-level scope. Class field initializers and class static blocks are implicit functions. Historically, this was the scope which hosts variables that are defined by
vardeclarations, and thus the namevariableScope.
block
- Type:
ASTNode - Description: The AST node which created this scope.
variables
- Type:
Variable[] - Description: The array of all variables which are defined on this scope. This does not include variables which are defined in child scopes.
set
- Type:
Map<string, Variable> - Description: The map from variable names to variable objects.
references
- Type:
Reference[] - Description: The array of all references on this scope. This does not include references in child scopes.
through
- Type:
Reference[] - Description: The array of references which could not be resolved in this scope.
functionExpressionScope
- Type:
boolean - Description:
trueif this scope is"function-expression-name"scope.
implicit
This field exists only in the root Scope object (the global scope). It provides information about implicit global variables. Implicit global variables are variables that are neither built-in nor explicitly declared, but created implicitly by assigning values to undeclared variables in non-strict code. Variable objects for these variables are not present in the root Scope object’s fields variables and set.
The value of the implicit field is an object with two properties.
variables
- Type:
Variable[] - Description: The array of all implicit global variables.
set
- Type:
Map<string, Variable> - Description: The map from variable names to variable objects for implicit global variables.
Deprecated members
Those members are defined but not used in ESLint.
taints
- Type:
Map<string, boolean> - Description: The map from variable names to
taintedflag.
dynamic
- Type:
boolean - Description:
trueif this scope is dynamic. I.e., the type of this scope is"global"or"with".
directCallToEvalScope
- Type:
boolean - Description:
trueif this scope containseval()invocations.
thisFound
- Type:
boolean - Description:
trueif this scope containsthis.
resolve(node)
- Parameters:
node(ASTNode) … An AST node to get their reference object. The type of the node must be"Identifier".
- Return type:
Reference | null - Description: Returns
this.references.find(r => r.identifier === node).
isStatic()
- Parameters:
- Return type:
boolean - Description: Returns
!this.dynamic.
isArgumentsMaterialized()
- Parameters:
- Return type:
boolean - Description:
trueif this is a"function"scope which has usedargumentsvariable.
isThisMaterialized()
- Parameters:
- Return type:
boolean - Description: Returns
this.thisFound.
isUsedName(name)
- Parameters:
name(string) … The name to check.
- Return type:
boolean - Description:
trueif a given name is used in variable names or reference names.
Variable interface
Variable object is variable’s information.
Fields
name
- Type:
string - Description: The name of this variable.
scope
- Type:
Scope - Description: The scope in which this variable is defined.
identifiers
- Type:
ASTNode[] - Description: The array of
Identifiernodes which define this variable. If this variable is redeclared, this array includes two or more nodes.
references
- Type:
Reference[] - Description: The array of the references of this variable.
defs
- Type:
Definition[] - Description: The array of the definitions of this variable.
Deprecated members
Those members are defined but not used in ESLint.
tainted
- Type:
boolean - Description: The
taintedflag. (alwaysfalse)
stack
- Type:
boolean - Description: The
stackflag. (I’m not sure what this means.)
Reference interface
Reference object is reference’s information.
Fields
identifier
- Type:
ASTNode - Description: The
Identifiernode of this reference.
from
- Type:
Scope - Description: The
Scopeobject that this reference is on.
resolved
- Type:
Variable | null - Description: The
Variableobject that this reference refers. If such variable was not defined, this isnull.
writeExpr
- Type:
ASTNode | null - Description: The ASTNode object which is right-hand side.
init
- Type:
boolean - Description:
trueif this writing reference is a variable initializer or a default value.
Methods
isWrite()
- Parameters:
- Return type:
boolean - Description:
trueif this reference is writing.
isRead()
- Parameters:
- Return type:
boolean - Description:
trueif this reference is reading.
isWriteOnly()
- Parameters:
- Return type:
boolean - Description:
trueif this reference is writing but not reading.
isReadOnly()
- Parameters:
- Return type:
boolean - Description:
trueif this reference is reading but not writing.
isReadWrite()
- Parameters:
- Return type:
boolean - Description:
trueif this reference is reading and writing.
Deprecated members
Those members are defined but not used in ESLint.
tainted
- Type:
boolean - Description: The
taintedflag. (alwaysfalse)
flag
- Type:
number - Description:
1is reading,2is writing,3is reading/writing.
partial
- Type:
boolean - Description: The
partialflag.
isStatic()
- Parameters:
- Return type:
boolean - Description:
trueif this reference is resolved statically.
Definition interface
Definition object is variable definition’s information.
Fields
type
- Type:
string - Description: The type of this definition. One of
"CatchClause","ClassName","FunctionName","ImplicitGlobalVariable","ImportBinding","Parameter", and"Variable".
name
- Type:
ASTNode - Description: The
Identifiernode of this definition.
node
- Type:
ASTNode - Description: The enclosing node of the name.
| type | node |
|---|---|
"CatchClause" |
CatchClause |
"ClassName" |
ClassDeclaration or ClassExpression |
"FunctionName" |
FunctionDeclaration or FunctionExpression |
"ImplicitGlobalVariable" |
AssignmentExpression or ForInStatement or ForOfStatement |
"ImportBinding" |
ImportSpecifier, ImportDefaultSpecifier, or ImportNamespaceSpecifier |
"Parameter" |
FunctionDeclaration, FunctionExpression, or ArrowFunctionExpression |
"Variable" |
VariableDeclarator |
parent
- Type:
ASTNode | undefined | null - Description: The enclosing statement node of the name.
| type | parent |
|---|---|
"CatchClause" |
null |
"ClassName" |
null |
"FunctionName" |
null |
"ImplicitGlobalVariable" |
null |
"ImportBinding" |
ImportDeclaration |
"Parameter" |
null |
"Variable" |
VariableDeclaration |
Deprecated members
Those members are defined but not used in ESLint.
index
- Type:
number | undefined | null - Description: The index in the declaration statement.
kind
- Type:
string | undefined | null - Description: The kind of the declaration statement.