solid-optimizer 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +269 -14
- package/dist/compile-BroGlagZ.d.mts +172 -0
- package/dist/compile-BroGlagZ.d.mts.map +1 -0
- package/dist/compile-ocDYvJUW.mjs +3910 -0
- package/dist/compile-ocDYvJUW.mjs.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/vite.d.mts +35 -0
- package/dist/vite.d.mts.map +1 -0
- package/dist/vite.mjs +1465 -0
- package/dist/vite.mjs.map +1 -0
- package/package.json +60 -58
- package/src/ast.ts +263 -0
- package/src/compile.ts +304 -0
- package/src/constants.ts +249 -0
- package/src/context.ts +87 -0
- package/src/fold.ts +850 -0
- package/src/index.ts +4 -107
- package/src/inline.ts +2028 -0
- package/src/jsx.ts +170 -0
- package/src/memo.ts +383 -0
- package/src/provider.ts +842 -0
- package/src/scope.ts +588 -0
- package/src/server.ts +275 -0
- package/src/size.ts +307 -0
- package/src/value.ts +386 -0
- package/src/vite/index.ts +1135 -0
- package/src/vite/inject.ts +311 -0
- package/src/vite/namespace.ts +66 -0
- package/src/vite/runtime.ts +390 -0
- package/src/vite/usage.ts +405 -0
- package/dist/cjs/development/index.cjs +0 -228
- package/dist/cjs/development/index.cjs.map +0 -7
- package/dist/cjs/production/index.cjs +0 -1
- package/dist/esm/development/index.mjs +0 -197
- package/dist/esm/development/index.mjs.map +0 -7
- package/dist/esm/production/index.mjs +0 -1
- package/dist/types/checks.d.ts +0 -9
- package/dist/types/checks.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/path-references-import.d.ts +0 -3
- package/dist/types/path-references-import.d.ts.map +0 -1
- package/dist/types/unwrap-node.d.ts +0 -7
- package/dist/types/unwrap-node.d.ts.map +0 -1
- package/dist/types/unwrap-path.d.ts +0 -6
- package/dist/types/unwrap-path.d.ts.map +0 -1
- package/src/checks.ts +0 -42
- package/src/path-references-import.ts +0 -82
- package/src/unwrap-node.ts +0 -27
- package/src/unwrap-path.ts +0 -18
package/src/scope.ts
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Binding resolution for the passes.
|
|
3
|
+
*
|
|
4
|
+
* oxc-parser returns an ESTree AST without scope information, so this module
|
|
5
|
+
* resolves every identifier reference to the declaration it binds to. The
|
|
6
|
+
* passes use it to fold a constant only where it is the same binding, to tell
|
|
7
|
+
* Solid's own components from local ones, and to rename the bindings of an
|
|
8
|
+
* inlined component.
|
|
9
|
+
*
|
|
10
|
+
* Code is treated as an ES module: it is strict, and a function declared in a
|
|
11
|
+
* block is scoped to that block.
|
|
12
|
+
*/
|
|
13
|
+
import type {
|
|
14
|
+
BindingPattern,
|
|
15
|
+
Class,
|
|
16
|
+
Directive,
|
|
17
|
+
Function as FunctionNode,
|
|
18
|
+
JSXElementName,
|
|
19
|
+
ModuleExportName,
|
|
20
|
+
Node,
|
|
21
|
+
ParamPattern,
|
|
22
|
+
Program,
|
|
23
|
+
Statement,
|
|
24
|
+
} from 'oxc-parser';
|
|
25
|
+
import type { Parents } from './ast';
|
|
26
|
+
import { forEachChild, isIntrinsicTag } from './ast';
|
|
27
|
+
|
|
28
|
+
export type BindingKind =
|
|
29
|
+
| 'var'
|
|
30
|
+
| 'let'
|
|
31
|
+
| 'const'
|
|
32
|
+
| 'function'
|
|
33
|
+
| 'class'
|
|
34
|
+
| 'param'
|
|
35
|
+
| 'import'
|
|
36
|
+
| 'catch'
|
|
37
|
+
| 'other';
|
|
38
|
+
|
|
39
|
+
export interface Binding {
|
|
40
|
+
readonly name: string;
|
|
41
|
+
readonly kind: BindingKind;
|
|
42
|
+
readonly scope: Scope;
|
|
43
|
+
/** The identifier that declares the binding. */
|
|
44
|
+
readonly identifier: Node;
|
|
45
|
+
/**
|
|
46
|
+
* The node that declares the binding: a `VariableDeclarator`, a function, a
|
|
47
|
+
* class, an import specifier, or the function or clause that owns a parameter.
|
|
48
|
+
*/
|
|
49
|
+
readonly declaration: Node;
|
|
50
|
+
/** The module an import binding comes from. */
|
|
51
|
+
readonly source: string | undefined;
|
|
52
|
+
/** The name an import binding has in its module: an export name, `default`, or `*`. */
|
|
53
|
+
readonly imported: string | undefined;
|
|
54
|
+
/** Every reference, including JSX tag names in opening and closing tags. */
|
|
55
|
+
readonly references: Node[];
|
|
56
|
+
/** Whether anything writes to the binding after its declaration. */
|
|
57
|
+
mutated: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface Scope {
|
|
61
|
+
readonly node: Node;
|
|
62
|
+
readonly parent: Scope | undefined;
|
|
63
|
+
/** Whether `var` declarations stop here. */
|
|
64
|
+
readonly isFunction: boolean;
|
|
65
|
+
readonly bindings: Map<string, Binding>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ScopeAnalysis {
|
|
69
|
+
readonly root: Scope;
|
|
70
|
+
/** What each identifier reference resolves to. `undefined` means a global. */
|
|
71
|
+
readonly references: Map<Node, Binding | undefined>;
|
|
72
|
+
/** The scope each scope-creating node opens. A function body shares its function's scope. */
|
|
73
|
+
readonly scopes: Map<Node, Scope>;
|
|
74
|
+
/** Every identifier name in the program, for generating names that collide with none. */
|
|
75
|
+
readonly names: Set<string>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface PendingReference {
|
|
79
|
+
readonly node: Node;
|
|
80
|
+
readonly name: string;
|
|
81
|
+
readonly scope: Scope;
|
|
82
|
+
readonly write: boolean;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function lookup(scope: Scope, name: string): Binding | undefined {
|
|
86
|
+
let current: Scope | undefined = scope;
|
|
87
|
+
while (current) {
|
|
88
|
+
const binding = current.bindings.get(name);
|
|
89
|
+
if (binding) {
|
|
90
|
+
return binding;
|
|
91
|
+
}
|
|
92
|
+
current = current.parent;
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The innermost scope that contains `node`.
|
|
99
|
+
*/
|
|
100
|
+
export function scopeAt(analysis: ScopeAnalysis, parents: Parents, node: Node): Scope {
|
|
101
|
+
let current: Node | undefined = parents.get(node);
|
|
102
|
+
while (current) {
|
|
103
|
+
const scope = analysis.scopes.get(current);
|
|
104
|
+
if (scope) {
|
|
105
|
+
return scope;
|
|
106
|
+
}
|
|
107
|
+
current = parents.get(current);
|
|
108
|
+
}
|
|
109
|
+
return analysis.root;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Whether `scope` is `ancestor` or nested inside it.
|
|
114
|
+
*/
|
|
115
|
+
export function isWithinScope(scope: Scope, ancestor: Scope): boolean {
|
|
116
|
+
let current: Scope | undefined = scope;
|
|
117
|
+
while (current) {
|
|
118
|
+
if (current === ancestor) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
current = current.parent;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function exportName(name: ModuleExportName): string {
|
|
127
|
+
return name.type === 'Literal' ? name.value : name.name;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
class Analyzer {
|
|
131
|
+
private scope: Scope;
|
|
132
|
+
|
|
133
|
+
readonly root: Scope;
|
|
134
|
+
|
|
135
|
+
readonly scopes = new Map<Node, Scope>();
|
|
136
|
+
|
|
137
|
+
readonly names = new Set<string>();
|
|
138
|
+
|
|
139
|
+
private readonly pending: PendingReference[] = [];
|
|
140
|
+
|
|
141
|
+
constructor(program: Program) {
|
|
142
|
+
this.root = {
|
|
143
|
+
node: program,
|
|
144
|
+
parent: undefined,
|
|
145
|
+
isFunction: true,
|
|
146
|
+
bindings: new Map(),
|
|
147
|
+
};
|
|
148
|
+
this.scopes.set(program, this.root);
|
|
149
|
+
this.scope = this.root;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
finish(): Map<Node, Binding | undefined> {
|
|
153
|
+
const references = new Map<Node, Binding | undefined>();
|
|
154
|
+
for (const item of this.pending) {
|
|
155
|
+
const binding = lookup(item.scope, item.name);
|
|
156
|
+
if (binding) {
|
|
157
|
+
binding.references.push(item.node);
|
|
158
|
+
if (item.write) {
|
|
159
|
+
binding.mutated = true;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
references.set(item.node, binding);
|
|
163
|
+
}
|
|
164
|
+
return references;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private withScope(node: Node, isFunction: boolean, run: () => void): Scope {
|
|
168
|
+
const scope: Scope = {
|
|
169
|
+
node,
|
|
170
|
+
parent: this.scope,
|
|
171
|
+
isFunction,
|
|
172
|
+
bindings: new Map(),
|
|
173
|
+
};
|
|
174
|
+
this.scopes.set(node, scope);
|
|
175
|
+
const previous = this.scope;
|
|
176
|
+
this.scope = scope;
|
|
177
|
+
run();
|
|
178
|
+
this.scope = previous;
|
|
179
|
+
return scope;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private declare(
|
|
183
|
+
identifier: Node & { name: string },
|
|
184
|
+
kind: BindingKind,
|
|
185
|
+
declaration: Node,
|
|
186
|
+
source?: string,
|
|
187
|
+
imported?: string,
|
|
188
|
+
): void {
|
|
189
|
+
let target = this.scope;
|
|
190
|
+
if (kind === 'var') {
|
|
191
|
+
while (!target.isFunction && target.parent) {
|
|
192
|
+
target = target.parent;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
this.names.add(identifier.name);
|
|
196
|
+
const existing = target.bindings.get(identifier.name);
|
|
197
|
+
if (existing) {
|
|
198
|
+
// A redeclaration, like `var a; var a;`. The binding is no longer a
|
|
199
|
+
// single declaration, so no pass treats it as a constant.
|
|
200
|
+
existing.mutated = true;
|
|
201
|
+
existing.references.push(identifier);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
target.bindings.set(identifier.name, {
|
|
205
|
+
name: identifier.name,
|
|
206
|
+
kind,
|
|
207
|
+
scope: target,
|
|
208
|
+
identifier,
|
|
209
|
+
declaration,
|
|
210
|
+
source,
|
|
211
|
+
imported,
|
|
212
|
+
references: [],
|
|
213
|
+
mutated: false,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private reference(node: Node & { name: string }, write: boolean): void {
|
|
218
|
+
this.names.add(node.name);
|
|
219
|
+
this.pending.push({ node, name: node.name, scope: this.scope, write });
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private declarePattern(
|
|
223
|
+
pattern: BindingPattern | ParamPattern,
|
|
224
|
+
kind: BindingKind,
|
|
225
|
+
declaration: Node,
|
|
226
|
+
): void {
|
|
227
|
+
switch (pattern.type) {
|
|
228
|
+
case 'Identifier':
|
|
229
|
+
this.declare(pattern, kind, declaration);
|
|
230
|
+
break;
|
|
231
|
+
case 'ObjectPattern':
|
|
232
|
+
for (const property of pattern.properties) {
|
|
233
|
+
if (property.type === 'RestElement') {
|
|
234
|
+
this.declarePattern(property.argument, kind, declaration);
|
|
235
|
+
} else {
|
|
236
|
+
if (property.computed) {
|
|
237
|
+
this.visit(property.key);
|
|
238
|
+
}
|
|
239
|
+
this.declarePattern(property.value, kind, declaration);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
break;
|
|
243
|
+
case 'ArrayPattern':
|
|
244
|
+
for (const element of pattern.elements) {
|
|
245
|
+
if (element) {
|
|
246
|
+
this.declarePattern(element, kind, declaration);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
break;
|
|
250
|
+
case 'AssignmentPattern':
|
|
251
|
+
this.declarePattern(pattern.left, kind, declaration);
|
|
252
|
+
this.visit(pattern.right);
|
|
253
|
+
break;
|
|
254
|
+
case 'RestElement':
|
|
255
|
+
this.declarePattern(pattern.argument, kind, declaration);
|
|
256
|
+
break;
|
|
257
|
+
case 'TSParameterProperty':
|
|
258
|
+
this.declarePattern(pattern.parameter, kind, declaration);
|
|
259
|
+
break;
|
|
260
|
+
default:
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Visits the target of an assignment, marking every identifier it writes.
|
|
267
|
+
*/
|
|
268
|
+
private visitTarget(target: Node): void {
|
|
269
|
+
switch (target.type) {
|
|
270
|
+
case 'Identifier':
|
|
271
|
+
this.reference(target, true);
|
|
272
|
+
break;
|
|
273
|
+
case 'ObjectPattern':
|
|
274
|
+
for (const property of target.properties) {
|
|
275
|
+
if (property.type === 'RestElement') {
|
|
276
|
+
this.visitTarget(property.argument);
|
|
277
|
+
} else {
|
|
278
|
+
if (property.computed) {
|
|
279
|
+
this.visit(property.key);
|
|
280
|
+
}
|
|
281
|
+
this.visitTarget(property.value);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
break;
|
|
285
|
+
case 'ArrayPattern':
|
|
286
|
+
for (const element of target.elements) {
|
|
287
|
+
if (element) {
|
|
288
|
+
this.visitTarget(element);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
case 'AssignmentPattern':
|
|
293
|
+
this.visitTarget(target.left);
|
|
294
|
+
this.visit(target.right);
|
|
295
|
+
break;
|
|
296
|
+
case 'RestElement':
|
|
297
|
+
this.visitTarget(target.argument);
|
|
298
|
+
break;
|
|
299
|
+
case 'ParenthesizedExpression':
|
|
300
|
+
case 'TSAsExpression':
|
|
301
|
+
case 'TSSatisfiesExpression':
|
|
302
|
+
case 'TSNonNullExpression':
|
|
303
|
+
case 'TSTypeAssertion':
|
|
304
|
+
this.visitTarget(target.expression);
|
|
305
|
+
break;
|
|
306
|
+
default:
|
|
307
|
+
this.visit(target);
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private visitStatements(statements: (Statement | Directive)[]): void {
|
|
313
|
+
for (const statement of statements) {
|
|
314
|
+
this.visit(statement);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private visitFunction(node: FunctionNode): void {
|
|
319
|
+
this.withScope(node, true, () => {
|
|
320
|
+
if (node.type === 'FunctionExpression' && node.id) {
|
|
321
|
+
this.declare(node.id, 'other', node);
|
|
322
|
+
}
|
|
323
|
+
for (const param of node.params) {
|
|
324
|
+
this.declarePattern(param, 'param', node);
|
|
325
|
+
}
|
|
326
|
+
if (node.body) {
|
|
327
|
+
this.scopes.set(node.body, this.scope);
|
|
328
|
+
this.visitStatements(node.body.body);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private visitClass(node: Class): void {
|
|
334
|
+
for (const decorator of node.decorators) {
|
|
335
|
+
this.visit(decorator);
|
|
336
|
+
}
|
|
337
|
+
if (node.superClass) {
|
|
338
|
+
this.visit(node.superClass);
|
|
339
|
+
}
|
|
340
|
+
for (const element of node.body.body) {
|
|
341
|
+
switch (element.type) {
|
|
342
|
+
case 'MethodDefinition':
|
|
343
|
+
case 'PropertyDefinition':
|
|
344
|
+
case 'AccessorProperty':
|
|
345
|
+
for (const decorator of element.decorators) {
|
|
346
|
+
this.visit(decorator);
|
|
347
|
+
}
|
|
348
|
+
if (element.computed) {
|
|
349
|
+
this.visit(element.key);
|
|
350
|
+
}
|
|
351
|
+
if (element.value) {
|
|
352
|
+
this.visit(element.value);
|
|
353
|
+
}
|
|
354
|
+
break;
|
|
355
|
+
case 'StaticBlock':
|
|
356
|
+
this.withScope(element, true, () => {
|
|
357
|
+
this.visitStatements(element.body);
|
|
358
|
+
});
|
|
359
|
+
break;
|
|
360
|
+
default:
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private visitJSXName(name: JSXElementName): void {
|
|
367
|
+
if (name.type === 'JSXIdentifier') {
|
|
368
|
+
if (!isIntrinsicTag(name.name) && name.name !== 'this') {
|
|
369
|
+
this.reference(name, false);
|
|
370
|
+
}
|
|
371
|
+
} else if (name.type === 'JSXMemberExpression') {
|
|
372
|
+
let object = name.object;
|
|
373
|
+
while (object.type === 'JSXMemberExpression') {
|
|
374
|
+
object = object.object;
|
|
375
|
+
}
|
|
376
|
+
if (object.name !== 'this') {
|
|
377
|
+
this.reference(object, false);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
visit(node: Node): void {
|
|
383
|
+
switch (node.type) {
|
|
384
|
+
case 'Identifier':
|
|
385
|
+
this.reference(node, false);
|
|
386
|
+
break;
|
|
387
|
+
case 'VariableDeclaration': {
|
|
388
|
+
// `using` binds a disposable resource, so it is never a folding candidate.
|
|
389
|
+
let kind: BindingKind = 'other';
|
|
390
|
+
if (node.kind === 'var' || node.kind === 'let' || node.kind === 'const') {
|
|
391
|
+
kind = node.kind;
|
|
392
|
+
}
|
|
393
|
+
for (const declarator of node.declarations) {
|
|
394
|
+
this.declarePattern(declarator.id, kind, declarator);
|
|
395
|
+
if (declarator.init) {
|
|
396
|
+
this.visit(declarator.init);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
case 'FunctionDeclaration':
|
|
402
|
+
case 'TSDeclareFunction':
|
|
403
|
+
if (node.id) {
|
|
404
|
+
this.declare(node.id, 'function', node);
|
|
405
|
+
}
|
|
406
|
+
this.visitFunction(node);
|
|
407
|
+
break;
|
|
408
|
+
case 'FunctionExpression':
|
|
409
|
+
this.visitFunction(node);
|
|
410
|
+
break;
|
|
411
|
+
case 'ArrowFunctionExpression':
|
|
412
|
+
this.withScope(node, true, () => {
|
|
413
|
+
for (const param of node.params) {
|
|
414
|
+
this.declarePattern(param, 'param', node);
|
|
415
|
+
}
|
|
416
|
+
if (node.body.type === 'BlockStatement') {
|
|
417
|
+
this.scopes.set(node.body, this.scope);
|
|
418
|
+
this.visitStatements(node.body.body);
|
|
419
|
+
} else {
|
|
420
|
+
this.visit(node.body);
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
break;
|
|
424
|
+
case 'ClassDeclaration':
|
|
425
|
+
if (node.id) {
|
|
426
|
+
this.declare(node.id, 'class', node);
|
|
427
|
+
}
|
|
428
|
+
this.visitClass(node);
|
|
429
|
+
break;
|
|
430
|
+
case 'ClassExpression':
|
|
431
|
+
this.withScope(node, false, () => {
|
|
432
|
+
if (node.id) {
|
|
433
|
+
this.declare(node.id, 'other', node);
|
|
434
|
+
}
|
|
435
|
+
this.visitClass(node);
|
|
436
|
+
});
|
|
437
|
+
break;
|
|
438
|
+
case 'BlockStatement':
|
|
439
|
+
this.withScope(node, false, () => {
|
|
440
|
+
this.visitStatements(node.body);
|
|
441
|
+
});
|
|
442
|
+
break;
|
|
443
|
+
case 'ForStatement':
|
|
444
|
+
this.withScope(node, false, () => {
|
|
445
|
+
if (node.init) {
|
|
446
|
+
this.visit(node.init);
|
|
447
|
+
}
|
|
448
|
+
if (node.test) {
|
|
449
|
+
this.visit(node.test);
|
|
450
|
+
}
|
|
451
|
+
if (node.update) {
|
|
452
|
+
this.visit(node.update);
|
|
453
|
+
}
|
|
454
|
+
this.visit(node.body);
|
|
455
|
+
});
|
|
456
|
+
break;
|
|
457
|
+
case 'ForInStatement':
|
|
458
|
+
case 'ForOfStatement':
|
|
459
|
+
this.withScope(node, false, () => {
|
|
460
|
+
if (node.left.type === 'VariableDeclaration') {
|
|
461
|
+
this.visit(node.left);
|
|
462
|
+
} else {
|
|
463
|
+
this.visitTarget(node.left);
|
|
464
|
+
}
|
|
465
|
+
this.visit(node.right);
|
|
466
|
+
this.visit(node.body);
|
|
467
|
+
});
|
|
468
|
+
break;
|
|
469
|
+
case 'CatchClause':
|
|
470
|
+
this.withScope(node, false, () => {
|
|
471
|
+
if (node.param) {
|
|
472
|
+
this.declarePattern(node.param, 'catch', node);
|
|
473
|
+
}
|
|
474
|
+
this.visit(node.body);
|
|
475
|
+
});
|
|
476
|
+
break;
|
|
477
|
+
case 'SwitchStatement':
|
|
478
|
+
this.visit(node.discriminant);
|
|
479
|
+
this.withScope(node, false, () => {
|
|
480
|
+
for (const item of node.cases) {
|
|
481
|
+
if (item.test) {
|
|
482
|
+
this.visit(item.test);
|
|
483
|
+
}
|
|
484
|
+
this.visitStatements(item.consequent);
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
break;
|
|
488
|
+
case 'ImportDeclaration': {
|
|
489
|
+
const source = node.source.value;
|
|
490
|
+
for (const specifier of node.specifiers) {
|
|
491
|
+
let imported = '*';
|
|
492
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
493
|
+
imported = exportName(specifier.imported);
|
|
494
|
+
} else if (specifier.type === 'ImportDefaultSpecifier') {
|
|
495
|
+
imported = 'default';
|
|
496
|
+
}
|
|
497
|
+
this.declare(specifier.local, 'import', specifier, source, imported);
|
|
498
|
+
}
|
|
499
|
+
break;
|
|
500
|
+
}
|
|
501
|
+
case 'ExportNamedDeclaration':
|
|
502
|
+
if (node.declaration) {
|
|
503
|
+
this.visit(node.declaration);
|
|
504
|
+
} else if (!node.source) {
|
|
505
|
+
for (const specifier of node.specifiers) {
|
|
506
|
+
if (specifier.local.type === 'Identifier') {
|
|
507
|
+
this.reference(specifier.local, false);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
break;
|
|
512
|
+
case 'ExportAllDeclaration':
|
|
513
|
+
case 'BreakStatement':
|
|
514
|
+
case 'ContinueStatement':
|
|
515
|
+
case 'MetaProperty':
|
|
516
|
+
case 'PrivateIdentifier':
|
|
517
|
+
case 'Super':
|
|
518
|
+
case 'ThisExpression':
|
|
519
|
+
case 'JSXText':
|
|
520
|
+
case 'JSXEmptyExpression':
|
|
521
|
+
break;
|
|
522
|
+
case 'LabeledStatement':
|
|
523
|
+
this.visit(node.body);
|
|
524
|
+
break;
|
|
525
|
+
case 'MemberExpression':
|
|
526
|
+
this.visit(node.object);
|
|
527
|
+
if (node.computed) {
|
|
528
|
+
this.visit(node.property);
|
|
529
|
+
}
|
|
530
|
+
break;
|
|
531
|
+
case 'Property':
|
|
532
|
+
if (node.computed) {
|
|
533
|
+
this.visit(node.key);
|
|
534
|
+
}
|
|
535
|
+
this.visit(node.value);
|
|
536
|
+
break;
|
|
537
|
+
case 'AssignmentExpression':
|
|
538
|
+
this.visitTarget(node.left);
|
|
539
|
+
this.visit(node.right);
|
|
540
|
+
break;
|
|
541
|
+
case 'UpdateExpression':
|
|
542
|
+
this.visitTarget(node.argument);
|
|
543
|
+
break;
|
|
544
|
+
case 'JSXElement':
|
|
545
|
+
this.visitJSXName(node.openingElement.name);
|
|
546
|
+
for (const attribute of node.openingElement.attributes) {
|
|
547
|
+
if (attribute.type === 'JSXSpreadAttribute') {
|
|
548
|
+
this.visit(attribute.argument);
|
|
549
|
+
} else if (attribute.value) {
|
|
550
|
+
this.visit(attribute.value);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
for (const child of node.children) {
|
|
554
|
+
this.visit(child);
|
|
555
|
+
}
|
|
556
|
+
if (node.closingElement) {
|
|
557
|
+
this.visitJSXName(node.closingElement.name);
|
|
558
|
+
}
|
|
559
|
+
break;
|
|
560
|
+
case 'TSEnumDeclaration':
|
|
561
|
+
case 'TSImportEqualsDeclaration':
|
|
562
|
+
this.declare(node.id, 'other', node);
|
|
563
|
+
break;
|
|
564
|
+
case 'TSModuleDeclaration':
|
|
565
|
+
if (node.id.type === 'Identifier') {
|
|
566
|
+
this.declare(node.id, 'other', node);
|
|
567
|
+
}
|
|
568
|
+
break;
|
|
569
|
+
default:
|
|
570
|
+
forEachChild(node, (child) => {
|
|
571
|
+
this.visit(child);
|
|
572
|
+
});
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
export function analyzeScopes(program: Program): ScopeAnalysis {
|
|
579
|
+
const analyzer = new Analyzer(program);
|
|
580
|
+
analyzer.visit(program);
|
|
581
|
+
const references = analyzer.finish();
|
|
582
|
+
return {
|
|
583
|
+
root: analyzer.root,
|
|
584
|
+
references,
|
|
585
|
+
scopes: analyzer.scopes,
|
|
586
|
+
names: analyzer.names,
|
|
587
|
+
};
|
|
588
|
+
}
|