]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/developer-guide/scope-manager-interface.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / developer-guide / scope-manager-interface.md
1 ---
2 title: ScopeManager
3 layout: doc
4
5 ---
6
7 This document was written based on the implementation of [eslint-scope](https://github.com/eslint/eslint-scope), a fork of [escope](https://github.com/estools/escope), and deprecates some members ESLint is not using.
8
9 ----
10
11 ## ScopeManager interface
12
13 `ScopeManager` object has all variable scopes.
14
15 ### Fields
16
17 #### scopes
18
19 * **Type:** `Scope[]`
20 * **Description:** All scopes.
21
22 #### globalScope
23
24 * **Type:** `Scope`
25 * **Description:** The root scope.
26
27 ### Methods
28
29 #### acquire(node, inner = false)
30
31 * **Parameters:**
32 * `node` (`ASTNode`) ... An AST node to get their scope.
33 * `inner` (`boolean`) ... If the node has multiple scope, this returns the outermost scope normally. If `inner` is `true` then this returns the innermost scope. Default is `false`.
34 * **Return type:** `Scope | null`
35 * **Description:** Get the scope of a given AST node. The gotten scope's `block` property is the node. This method never returns `function-expression-name` scope. If the node does not have their scope, this returns `null`.
36
37 #### getDeclaredVariables(node)
38
39 * **Parameters:**
40 * `node` (`ASTNode`) ... An AST node to get their variables.
41 * **Return type:** `Variable[]`
42 * **Description:** Get the variables that a given AST node defines. The gotten variables' `def[].node`/`def[].parent` property is the node. If the node does not define any variable, this returns an empty array.
43
44 ### Deprecated members
45
46 Those members are defined but not used in ESLint.
47
48 #### isModule()
49
50 * **Parameters:**
51 * **Return type:** `boolean`
52 * **Description:** `true` if this program is module.
53
54 #### isImpliedStrict()
55
56 * **Parameters:**
57 * **Return type:** `boolean`
58 * **Description:** `true` if this program is strict mode implicitly. I.e., `options.impliedStrict === true`.
59
60 #### isStrictModeSupported()
61
62 * **Parameters:**
63 * **Return type:** `boolean`
64 * **Description:** `true` if this program supports strict mode. I.e., `options.ecmaVersion >= 5`.
65
66 #### acquireAll(node)
67
68 * **Parameters:**
69 * `node` (`ASTNode`) ... An AST node to get their scope.
70 * **Return type:** `Scope[] | null`
71 * **Description:** Get the scopes of a given AST node. The gotten scopes' `block` property is the node. If the node does not have their scope, this returns `null`.
72
73 ----
74
75 ## Scope interface
76
77 `Scope` object has all variables and references in the scope.
78
79 ### Fields
80
81 #### type
82
83 * **Type:** `string`
84 * **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"`.
85
86 #### isStrict
87
88 * **Type:** `boolean`
89 * **Description:** `true` if this scope is strict mode.
90
91 #### upper
92
93 * **Type:** `Scope | null`
94 * **Description:** The parent scope. If this is the global scope then this property is `null`.
95
96 #### childScopes
97
98 * **Type:** `Scope[]`
99 * **Description:** The array of child scopes. This does not include grandchild scopes.
100
101 #### variableScope
102
103 * **Type:** `Scope`
104 * **Description:** The nearest ancestor whose `type` is one of `"class-field-initializer"`, `"class-static-block"`, `"function"`, `"global"`, or `"module"`. For the aforementioned scopes this is a self-reference.
105
106 > 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 `var` declarations, and thus the name `variableScope`.
107
108 #### block
109
110 * **Type:** `ASTNode`
111 * **Description:** The AST node which created this scope.
112
113 #### variables
114
115 * **Type:** `Variable[]`
116 * **Description:** The array of all variables which are defined on this scope. This does not include variables which are defined in child scopes.
117
118 #### set
119
120 * **Type:** `Map<string, Variable>`
121 * **Description:** The map from variable names to variable objects.
122
123 > I hope to rename `set` field or replace by a method.
124
125 #### references
126
127 * **Type:** `Reference[]`
128 * **Description:** The array of all references on this scope. This does not include references in child scopes.
129
130 #### through
131
132 * **Type:** `Reference[]`
133 * **Description:** The array of references which could not be resolved in this scope.
134
135 #### functionExpressionScope
136
137 * **Type:** `boolean`
138 * **Description:** `true` if this scope is `"function-expression-name"` scope.
139
140 > I hope to deprecate `functionExpressionScope` field as replacing by `scope.type === "function-expression-name"`.
141
142 ### Deprecated members
143
144 Those members are defined but not used in ESLint.
145
146 #### taints
147
148 * **Type:** `Map<string, boolean>`
149 * **Description:** The map from variable names to `tainted` flag.
150
151 #### dynamic
152
153 * **Type:** `boolean`
154 * **Description:** `true` if this scope is dynamic. I.e., the type of this scope is `"global"` or `"with"`.
155
156 #### directCallToEvalScope
157
158 * **Type:** `boolean`
159 * **Description:** `true` if this scope contains `eval()` invocations.
160
161 #### thisFound
162
163 * **Type:** `boolean`
164 * **Description:** `true` if this scope contains `this`.
165
166 #### resolve(node)
167
168 * **Parameters:**
169 * `node` (`ASTNode`) ... An AST node to get their reference object. The type of the node must be `"Identifier"`.
170 * **Return type:** `Reference | null`
171 * **Description:** Returns `this.references.find(r => r.identifier === node)`.
172
173 #### isStatic()
174
175 * **Parameters:**
176 * **Return type:** `boolean`
177 * **Description:** Returns `!this.dynamic`.
178
179 #### isArgumentsMaterialized()
180
181 * **Parameters:**
182 * **Return type:** `boolean`
183 * **Description:** `true` if this is a `"function"` scope which has used `arguments` variable.
184
185 #### isThisMaterialized()
186
187 * **Parameters:**
188 * **Return type:** `boolean`
189 * **Description:** Returns `this.thisFound`.
190
191 #### isUsedName(name)
192
193 * **Parameters:**
194 * `name` (`string`) ... The name to check.
195 * **Return type:** `boolean`
196 * **Description:** `true` if a given name is used in variable names or reference names.
197
198 ----
199
200 ## Variable interface
201
202 `Variable` object is variable's information.
203
204 ### Fields
205
206 #### name
207
208 * **Type:** `string`
209 * **Description:** The name of this variable.
210
211 #### scope
212
213 * **Type:** `Scope`
214 * **Description:** The scope in which this variable is defined.
215
216 #### identifiers
217
218 * **Type:** `ASTNode[]`
219 * **Description:** The array of `Identifier` nodes which define this variable. If this variable is redeclared, this array includes two or more nodes.
220
221 > I hope to deprecate `identifiers` field as replacing by `defs[].name` field.
222
223 #### references
224
225 * **Type:** `Reference[]`
226 * **Description:** The array of the references of this variable.
227
228 #### defs
229
230 * **Type:** `Definition[]`
231 * **Description:** The array of the definitions of this variable.
232
233 ### Deprecated members
234
235 Those members are defined but not used in ESLint.
236
237 #### tainted
238
239 * **Type:** `boolean`
240 * **Description:** The `tainted` flag. (always `false`)
241
242 #### stack
243
244 * **Type:** `boolean`
245 * **Description:** The `stack` flag. (I'm not sure what this means.)
246
247 ----
248
249 ## Reference interface
250
251 `Reference` object is reference's information.
252
253 ### Fields
254
255 #### identifier
256
257 * **Type:** `ASTNode`
258 * **Description:** The `Identifier` node of this reference.
259
260 #### from
261
262 * **Type:** `Scope`
263 * **Description:** The `Scope` object that this reference is on.
264
265 #### resolved
266
267 * **Type:** `Variable | null`
268 * **Description:** The `Variable` object that this reference refers. If such variable was not defined, this is `null`.
269
270 #### writeExpr
271
272 * **Type:** `ASTNode | null`
273 * **Description:** The ASTNode object which is right-hand side.
274
275 #### init
276
277 * **Type:** `boolean`
278 * **Description:** `true` if this writing reference is a variable initializer or a default value.
279
280 ### Methods
281
282 #### isWrite()
283
284 * **Parameters:**
285 * **Return type:** `boolean`
286 * **Description:** `true` if this reference is writing.
287
288 #### isRead()
289
290 * **Parameters:**
291 * **Return type:** `boolean`
292 * **Description:** `true` if this reference is reading.
293
294 #### isWriteOnly()
295
296 * **Parameters:**
297 * **Return type:** `boolean`
298 * **Description:** `true` if this reference is writing but not reading.
299
300 #### isReadOnly()
301
302 * **Parameters:**
303 * **Return type:** `boolean`
304 * **Description:** `true` if this reference is reading but not writing.
305
306 #### isReadWrite()
307
308 * **Parameters:**
309 * **Return type:** `boolean`
310 * **Description:** `true` if this reference is reading and writing.
311
312 ### Deprecated members
313
314 Those members are defined but not used in ESLint.
315
316 #### tainted
317
318 * **Type:** `boolean`
319 * **Description:** The `tainted` flag. (always `false`)
320
321 #### flag
322
323 * **Type:** `number`
324 * **Description:** `1` is reading, `2` is writing, `3` is reading/writing.
325
326 #### partial
327
328 * **Type:** `boolean`
329 * **Description:** The `partial` flag.
330
331 #### isStatic()
332
333 * **Parameters:**
334 * **Return type:** `boolean`
335 * **Description:** `true` if this reference is resolved statically.
336
337 ----
338
339 ## Definition interface
340
341 `Definition` object is variable definition's information.
342
343 ### Fields
344
345 #### type
346
347 * **Type:** `string`
348 * **Description:** The type of this definition. One of `"CatchClause"`, `"ClassName"`, `"FunctionName"`, `"ImplicitGlobalVariable"`, `"ImportBinding"`, `"Parameter"`, and `"Variable"`.
349
350 #### name
351
352 * **Type:** `ASTNode`
353 * **Description:** The `Identifier` node of this definition.
354
355 #### node
356
357 * **Type:** `ASTNode`
358 * **Description:** The enclosing node of the name.
359
360 | type | node |
361 |:---------------------------|:-----|
362 | `"CatchClause"` | `CatchClause`
363 | `"ClassName"` | `ClassDeclaration` or `ClassExpression`
364 | `"FunctionName"` | `FunctionDeclaration` or `FunctionExpression`
365 | `"ImplicitGlobalVariable"` | `Program`
366 | `"ImportBinding"` | `ImportSpecifier`, `ImportDefaultSpecifier`, or `ImportNamespaceSpecifier`
367 | `"Parameter"` | `FunctionDeclaration`, `FunctionExpression`, or `ArrowFunctionExpression`
368 | `"Variable"` | `VariableDeclarator`
369
370 #### parent
371
372 * **Type:** `ASTNode | undefined | null`
373 * **Description:** The enclosing statement node of the name.
374
375 | type | parent |
376 |:---------------------------|:-------|
377 | `"CatchClause"` | `null`
378 | `"ClassName"` | `null`
379 | `"FunctionName"` | `null`
380 | `"ImplicitGlobalVariable"` | `null`
381 | `"ImportBinding"` | `ImportDeclaration`
382 | `"Parameter"` | `null`
383 | `"Variable"` | `VariableDeclaration`
384
385 ### Deprecated members
386
387 Those members are defined but not used in ESLint.
388
389 #### index
390
391 * **Type:** `number | undefined | null`
392 * **Description:** The index in the declaration statement.
393
394 #### kind
395
396 * **Type:** `string | undefined | null`
397 * **Description:** The kind of the declaration statement.