/
ScopeManager.ts
270 lines (233 loc) · 7.62 KB
/
ScopeManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import type { SourceType, TSESTree } from '@typescript-eslint/types';
import { assert } from './assert';
import type { Scope } from './scope';
import {
BlockScope,
CatchScope,
ClassScope,
ConditionalTypeScope,
ForScope,
FunctionExpressionNameScope,
FunctionScope,
FunctionTypeScope,
GlobalScope,
MappedTypeScope,
ModuleScope,
ScopeType,
SwitchScope,
TSEnumScope,
TSModuleScope,
TypeScope,
WithScope,
} from './scope';
import { ClassFieldInitializerScope } from './scope/ClassFieldInitializerScope';
import { ClassStaticBlockScope } from './scope/ClassStaticBlockScope';
import type { Variable } from './variable';
interface ScopeManagerOptions {
globalReturn?: boolean;
sourceType?: SourceType;
impliedStrict?: boolean;
}
/**
* @see https://eslint.org/docs/latest/developer-guide/scope-manager-interface#scopemanager-interface
*/
class ScopeManager {
public currentScope: Scope | null;
public readonly declaredVariables: WeakMap<TSESTree.Node, Variable[]>;
/**
* The root scope
* @public
*/
public globalScope: GlobalScope | null;
public readonly nodeToScope: WeakMap<TSESTree.Node, Scope[]>;
readonly #options: ScopeManagerOptions;
/**
* All scopes
* @public
*/
public readonly scopes: Scope[];
public get variables(): Variable[] {
const variables = new Set<Variable>();
function recurse(scope: Scope): void {
scope.variables.forEach(v => variables.add(v));
scope.childScopes.forEach(recurse);
}
this.scopes.forEach(recurse);
return Array.from(variables).sort((a, b) => a.$id - b.$id);
}
constructor(options: ScopeManagerOptions) {
this.scopes = [];
this.globalScope = null;
this.nodeToScope = new WeakMap();
this.currentScope = null;
this.#options = options;
this.declaredVariables = new WeakMap();
}
public isGlobalReturn(): boolean {
return this.#options.globalReturn === true;
}
public isModule(): boolean {
return this.#options.sourceType === 'module';
}
public isImpliedStrict(): boolean {
return this.#options.impliedStrict === true;
}
public isStrictModeSupported(): boolean {
return true;
}
public isES6(): boolean {
return true;
}
/**
* 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.
* @param node An AST node to get their variables.
* @public
*/
public getDeclaredVariables(node: TSESTree.Node): Variable[] {
return this.declaredVariables.get(node) ?? [];
}
/**
* 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`.
*
* @param node An AST node to get their scope.
* @param inner If the node has multiple scopes, this returns the outermost scope normally.
* If `inner` is `true` then this returns the innermost scope.
* @public
*/
public acquire(node: TSESTree.Node, inner = false): Scope | null {
function predicate(testScope: Scope): boolean {
if (
testScope.type === ScopeType.function &&
testScope.functionExpressionScope
) {
return false;
}
return true;
}
const scopes = this.nodeToScope.get(node);
if (!scopes || scopes.length === 0) {
return null;
}
// Heuristic selection from all scopes.
// If you would like to get all scopes, please use ScopeManager#acquireAll.
if (scopes.length === 1) {
return scopes[0];
}
if (inner) {
for (let i = scopes.length - 1; i >= 0; --i) {
const scope = scopes[i];
if (predicate(scope)) {
return scope;
}
}
return null;
}
return scopes.find(predicate) ?? null;
}
protected nestScope<T extends Scope>(scope: T): T;
protected nestScope(scope: Scope): Scope {
if (scope instanceof GlobalScope) {
assert(this.currentScope == null);
this.globalScope = scope;
}
this.currentScope = scope;
return scope;
}
public nestBlockScope(node: BlockScope['block']): BlockScope {
assert(this.currentScope);
return this.nestScope(new BlockScope(this, this.currentScope, node));
}
public nestCatchScope(node: CatchScope['block']): CatchScope {
assert(this.currentScope);
return this.nestScope(new CatchScope(this, this.currentScope, node));
}
public nestClassScope(node: ClassScope['block']): ClassScope {
assert(this.currentScope);
return this.nestScope(new ClassScope(this, this.currentScope, node));
}
public nestClassFieldInitializerScope(
node: ClassFieldInitializerScope['block'],
): ClassFieldInitializerScope {
assert(this.currentScope);
return this.nestScope(
new ClassFieldInitializerScope(this, this.currentScope, node),
);
}
public nestClassStaticBlockScope(
node: ClassStaticBlockScope['block'],
): ClassStaticBlockScope {
assert(this.currentScope);
return this.nestScope(
new ClassStaticBlockScope(this, this.currentScope, node),
);
}
public nestConditionalTypeScope(
node: ConditionalTypeScope['block'],
): ConditionalTypeScope {
assert(this.currentScope);
return this.nestScope(
new ConditionalTypeScope(this, this.currentScope, node),
);
}
public nestForScope(node: ForScope['block']): ForScope {
assert(this.currentScope);
return this.nestScope(new ForScope(this, this.currentScope, node));
}
public nestFunctionExpressionNameScope(
node: FunctionExpressionNameScope['block'],
): FunctionExpressionNameScope {
assert(this.currentScope);
return this.nestScope(
new FunctionExpressionNameScope(this, this.currentScope, node),
);
}
public nestFunctionScope(
node: FunctionScope['block'],
isMethodDefinition: boolean,
): FunctionScope {
assert(this.currentScope);
return this.nestScope(
new FunctionScope(this, this.currentScope, node, isMethodDefinition),
);
}
public nestFunctionTypeScope(
node: FunctionTypeScope['block'],
): FunctionTypeScope {
assert(this.currentScope);
return this.nestScope(new FunctionTypeScope(this, this.currentScope, node));
}
public nestGlobalScope(node: GlobalScope['block']): GlobalScope {
return this.nestScope(new GlobalScope(this, node));
}
public nestMappedTypeScope(node: MappedTypeScope['block']): MappedTypeScope {
assert(this.currentScope);
return this.nestScope(new MappedTypeScope(this, this.currentScope, node));
}
public nestModuleScope(node: ModuleScope['block']): ModuleScope {
assert(this.currentScope);
return this.nestScope(new ModuleScope(this, this.currentScope, node));
}
public nestSwitchScope(node: SwitchScope['block']): SwitchScope {
assert(this.currentScope);
return this.nestScope(new SwitchScope(this, this.currentScope, node));
}
public nestTSEnumScope(node: TSEnumScope['block']): TSEnumScope {
assert(this.currentScope);
return this.nestScope(new TSEnumScope(this, this.currentScope, node));
}
public nestTSModuleScope(node: TSModuleScope['block']): TSModuleScope {
assert(this.currentScope);
return this.nestScope(new TSModuleScope(this, this.currentScope, node));
}
public nestTypeScope(node: TypeScope['block']): TypeScope {
assert(this.currentScope);
return this.nestScope(new TypeScope(this, this.currentScope, node));
}
public nestWithScope(node: WithScope['block']): WithScope {
assert(this.currentScope);
return this.nestScope(new WithScope(this, this.currentScope, node));
}
}
export { ScopeManager };