ScopeManager
本文档基于 eslint-scope(escope 的一个分支)的实现编写,并弃用了一些 ESLint 未使用的成员。
ScopeManager 接口
ScopeManager 对象包含所有变量作用域。
字段
scopes
- 类型:
Scope[] - 描述: 所有作用域。
globalScope
- 类型:
Scope - 描述: 根作用域。
方法
acquire(node, inner = false)
- 参数
node(ASTNode) … 获取其作用域的 AST 节点。inner(boolean) … 如果节点有多个作用域,通常返回最外层的作用域。如果inner为true,则返回最内层的作用域。默认为false。
- 返回类型:
Scope | null - 描述: 获取给定 AST 节点的作用域。获取的作用域的
block属性是该节点。此方法永远不会返回function-expression-name作用域。如果节点没有作用域,则返回null。
getDeclaredVariables(node)
- 参数
node(ASTNode) … 获取其变量的 AST 节点。
- 返回类型:
Variable[] - 描述: 获取给定 AST 节点定义的变量。获取的变量的
def[].node/def[].parent属性是该节点。如果节点未定义任何变量,则返回一个空数组。
已弃用的成员
这些成员已定义,但在 ESLint 中未使用。
isModule()
- 参数
- 返回类型:
boolean - 描述: 如果此程序是模块,则为
true。
isImpliedStrict()
- 参数
- 返回类型:
boolean - 描述: 如果此程序隐式地处于严格模式,则为
true。即,options.impliedStrict === true。
isStrictModeSupported()
- 参数
- 返回类型:
boolean - 描述: 如果此程序支持严格模式,则为
true。即,options.ecmaVersion >= 5。
acquireAll(node)
- 参数
node(ASTNode) … 获取其作用域的 AST 节点。
- 返回类型:
Scope[] | null - 描述: 获取给定 AST 节点的作用域。获取的作用域的
block属性是该节点。如果节点没有作用域,则返回null。
Scope 接口
Scope 对象包含作用域中的所有变量和引用。
字段
类型
- 类型:
string - 描述: 此作用域的类型。这是
"block"、"catch"、"class"、"class-field-initializer"、"class-static-block"、"for"、"function"、"function-expression-name"、"global"、"module"、"switch"、"with"之一。
isStrict
- 类型:
boolean - 描述: 如果此作用域处于严格模式,则为
true。
upper
- 类型:
Scope | null - 描述: 父作用域。如果这是全局作用域,则此属性为
null。
childScopes
- 类型:
Scope[] - 描述: 子作用域的数组。这不包括孙子作用域。
variableScope
- 类型:
Scope - 描述: 最近的祖先,其
type是"class-field-initializer"、"class-static-block"、"function"、"global"或"module"之一。对于上述作用域,这是一个自引用。
这表示最内层的封闭函数或顶层作用域。类字段初始化器和类静态块是隐式函数。从历史上看,这是托管由
var声明定义的变量的作用域,因此得名variableScope。
block
- 类型:
ASTNode - 描述: 创建此作用域的 AST 节点。
变量
- 类型:
Variable[] - 描述: 在此作用域上定义的所有变量的数组。这不包括在子作用域中定义的变量。
set
- 类型:
Map<string, Variable> - 描述: 从变量名到变量对象的映射。
references
- 类型:
Reference[] - 描述: 在此作用域上的所有引用的数组。这不包括子作用域中的引用。
through
- 类型:
Reference[] - 描述: 无法在此作用域中解析的引用的数组。
functionExpressionScope
- 类型:
boolean - 描述: 如果此作用域是
"function-expression-name"作用域,则为true。
已弃用的成员
这些成员已定义,但在 ESLint 中未使用。
taints
- 类型:
Map<string, boolean> - 描述: 从变量名到
tainted标志的映射。
dynamic
- 类型:
boolean - 描述: 如果此作用域是动态的,则为
true。即,此作用域的类型是"global"或"with"。
directCallToEvalScope
- 类型:
boolean - 描述: 如果此作用域包含
eval()调用,则为true。
thisFound
- 类型:
boolean - 描述: 如果此作用域包含
this,则为true。
resolve(node)
- 参数
node(ASTNode) … 获取其引用对象的 AST 节点。节点的类型必须是"Identifier"。
- 返回类型:
Reference | null - 描述: 返回
this.references.find(r => r.identifier === node)。
isStatic()
- 参数
- 返回类型:
boolean - 描述: 返回
!this.dynamic。
isArgumentsMaterialized()
- 参数
- 返回类型:
boolean - 描述: 如果这是一个已使用
arguments变量的"function"作用域,则为true。
isThisMaterialized()
- 参数
- 返回类型:
boolean - 描述: 返回
this.thisFound。
isUsedName(name)
- 参数
name(string) … 要检查的名称。
- 返回类型:
boolean - 描述: 如果给定的名称在变量名或引用名中使用,则为
true。
Variable 接口
Variable 对象是变量的信息。
字段
名称
- 类型:
string - 描述: 此变量的名称。
scope
- 类型:
Scope - 描述: 定义此变量的作用域。
identifiers
- 类型:
ASTNode[] - 描述: 定义此变量的
Identifier节点的数组。如果重新声明此变量,则此数组包含两个或多个节点。
references
- 类型:
Reference[] - 描述: 此变量的引用的数组。
defs
- 类型:
Definition[] - 描述: 此变量的定义的数组。
已弃用的成员
这些成员已定义,但在 ESLint 中未使用。
tainted
- 类型:
boolean - 描述:
tainted标志。(始终为false)
stack
- 类型:
boolean - 描述:
stack标志。(我不确定这意味着什么。)
Reference 接口
Reference 对象是引用的信息。
字段
identifier
- 类型:
ASTNode - 描述: 此引用的
Identifier节点。
from
- 类型:
Scope - 描述: 此引用所在的
Scope对象。
resolved
- 类型:
Variable | null - 描述: 此引用指向的
Variable对象。如果未定义此类变量,则为null。
writeExpr
- 类型:
ASTNode | null - 描述: 右侧的 ASTNode 对象。
init
- 类型:
boolean - 描述: 如果此写入引用是变量初始化器或默认值,则为
true。
方法
isWrite()
- 参数
- 返回类型:
boolean - 描述: 如果此引用正在写入,则为
true。
isRead()
- 参数
- 返回类型:
boolean - 描述: 如果此引用正在读取,则为
true。
isWriteOnly()
- 参数
- 返回类型:
boolean - 描述: 如果此引用正在写入但未读取,则为
true。
isReadOnly()
- 参数
- 返回类型:
boolean - 描述: 如果此引用正在读取但未写入,则为
true。
isReadWrite()
- 参数
- 返回类型:
boolean - 描述: 如果此引用正在读取和写入,则为
true。
已弃用的成员
这些成员已定义,但在 ESLint 中未使用。
tainted
- 类型:
boolean - 描述:
tainted标志。(始终为false)
flag
- 类型:
number - 描述:
1是读取,2是写入,3是读取/写入。
partial
- 类型:
boolean - 描述:
partial标志。
isStatic()
- 参数
- 返回类型:
boolean - 描述: 如果此引用是静态解析的,则为
true。
Definition 接口
Definition 对象是变量定义的信息。
字段
类型
- 类型:
string - 描述: 此定义的类型。
"CatchClause"、"ClassName"、"FunctionName"、"ImplicitGlobalVariable"、"ImportBinding"、"Parameter"和"Variable"之一。
名称
- 类型:
ASTNode - 描述: 此定义的
Identifier节点。
node
- 类型:
ASTNode - 描述: 名称的封闭节点。
| 类型 | node |
|---|---|
"CatchClause" |
CatchClause |
"ClassName" |
ClassDeclaration 或 ClassExpression |
"FunctionName" |
FunctionDeclaration 或 FunctionExpression |
"ImplicitGlobalVariable" |
Program |
"ImportBinding" |
ImportSpecifier、ImportDefaultSpecifier 或 ImportNamespaceSpecifier |
"Parameter" |
FunctionDeclaration、FunctionExpression 或 ArrowFunctionExpression |
"Variable" |
VariableDeclarator |
parent
- 类型:
ASTNode | undefined | null - 描述: 名称的封闭语句节点。
| 类型 | parent |
|---|---|
"CatchClause" |
null |
"ClassName" |
null |
"FunctionName" |
null |
"ImplicitGlobalVariable" |
null |
"ImportBinding" |
ImportDeclaration |
"Parameter" |
null |
"Variable" |
VariableDeclaration |
已弃用的成员
这些成员已定义,但在 ESLint 中未使用。
索引
- 类型:
number | undefined | null - 描述: 声明语句中的索引。
类型
- 类型:
string | undefined | null - 描述: 声明语句的类型。