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