Repository navigation
Expand file tree
/
Copy pathlayering-ast.ts
More file actions
283 lines (263 loc) · 10.1 KB
/
Copy pathlayering-ast.ts
File metadata and controls
283 lines (263 loc) · 10.1 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
/** Shared AST helpers for permanent layering policies. */
export type ProductionSource = Readonly<{ path: string; source: string }>;
export type AstNode = Record<string, unknown>;
export function asNode(value: unknown): AstNode | undefined {
return value !== null && typeof value === 'object' && !Array.isArray(value)
? (value as AstNode)
: undefined;
}
export function nodeList(value: unknown): unknown[] {
return Array.isArray(value) ? value : [];
}
export function memberName(node: Record<string, unknown>): string | undefined {
const property = node.property as Record<string, unknown> | undefined;
if (!property) return undefined;
return node.computed === true
? propertyName(property)
: property.type === 'Identifier'
? String(property.name)
: undefined;
}
export function propertyName(node: unknown): string | undefined {
if (node === null || typeof node !== 'object') return undefined;
const value = node as Record<string, unknown>;
return value.type === 'Identifier' || value.type === 'Literal'
? ((value.name as string | undefined) ?? (value.value as string | undefined))
: undefined;
}
export function memberPath(node: unknown): string[] | undefined {
if (node === null || typeof node !== 'object') return undefined;
const record = node as Record<string, unknown>;
if (record.type === 'Identifier') {
return typeof record.name === 'string' ? [record.name] : undefined;
}
if (record.type === 'ChainExpression') return memberPath(record.expression);
if (record.type !== 'MemberExpression' || record.computed === true) return undefined;
const object = memberPath(record.object);
const name = propertyName(record.property);
return object && name ? [...object, name] : undefined;
}
export function visitAst(node: unknown, visitor: (node: Record<string, unknown>) => void): void {
if (node === null || typeof node !== 'object') return;
if (Array.isArray(node)) {
for (const child of node) visitAst(child, visitor);
return;
}
const record = node as Record<string, unknown>;
visitor(record);
for (const child of Object.values(record)) visitAst(child, visitor);
}
const IMPORT_WRAPPER_TYPES = new Set([
'AwaitExpression',
'ParenthesizedExpression',
'TSAsExpression',
'TSSatisfiesExpression',
'TSNonNullExpression',
]);
function unwrapImportInit(node: unknown): Record<string, unknown> | null {
let current = node;
while (current !== null && typeof current === 'object') {
const record = current as Record<string, unknown>;
if (typeof record.type !== 'string' || !IMPORT_WRAPPER_TYPES.has(record.type)) {
return record.type === 'ImportExpression' ? record : null;
}
current =
record.type === 'AwaitExpression' || record.type === 'ParenthesizedExpression'
? record.argument
: record.expression;
}
return null;
}
export type DestructuredDynamicImport = Readonly<{
/** Export names captured from static (non-computed) property keys. */
symbols: readonly string[];
}>;
/**
* Bindings captured by destructuring a dynamic import
* (`const { a, b: local } = await import('...')` captures `a` and `b`), keyed by the import
* expression's source offset. A rest element or a computed key names no export and is skipped.
* Namespace-form and bare dynamic imports capture nothing.
*/
export function destructuredDynamicImportBindings(
program: unknown,
): ReadonlyMap<number, DestructuredDynamicImport> {
const captures = new Map<number, DestructuredDynamicImport>();
const visit = (node: unknown): void => {
if (node === null || typeof node !== 'object') return;
if (Array.isArray(node)) {
for (const child of node) visit(child);
return;
}
const record = node as Record<string, unknown>;
if (record.type === 'VariableDeclarator') {
const init = unwrapImportInit(record.init);
const id = record.id;
if (
init !== null &&
typeof init.start === 'number' &&
id !== null &&
typeof id === 'object' &&
(id as Record<string, unknown>).type === 'ObjectPattern'
) {
const symbols: string[] = [];
const properties = (id as Record<string, unknown>).properties;
if (Array.isArray(properties)) {
for (const property of properties) {
if (
property === null ||
typeof property !== 'object' ||
(property as Record<string, unknown>).type !== 'Property' ||
(property as Record<string, unknown>).computed === true
) {
continue;
}
const name = propertyName((property as Record<string, unknown>).key);
if (name) symbols.push(name);
}
}
captures.set(init.start, { symbols });
}
}
for (const child of Object.values(record)) visit(child);
};
visit(program);
return captures;
}
function patternNames(pattern: unknown, into: string[]): void {
const node = asNode(pattern);
if (node === undefined) return;
switch (node.type) {
case 'Identifier':
into.push(node.name as string);
break;
case 'ObjectPattern':
for (const property of nodeList(node.properties)) {
const entry = asNode(property);
patternNames(entry?.type === 'Property' ? entry.value : entry, into);
}
break;
case 'ArrayPattern':
for (const element of nodeList(node.elements)) patternNames(element, into);
break;
case 'AssignmentPattern':
patternNames(node.left, into);
break;
case 'RestElement':
patternNames(node.argument, into);
break;
case 'TSParameterProperty':
patternNames(node.parameter, into);
break;
}
}
function lexicalNames(statements: unknown[], into: string[]): void {
for (const statement of statements) {
const node = asNode(statement);
if (node?.type === 'VariableDeclaration' && node.kind !== 'var') {
for (const declarator of nodeList(node.declarations)) {
patternNames(asNode(declarator)?.id, into);
}
} else if (node?.type === 'FunctionDeclaration' || node?.type === 'ClassDeclaration') {
patternNames(node.id, into);
}
}
}
function isFunctionNode(node: AstNode): boolean {
return (
node.type === 'FunctionDeclaration' ||
node.type === 'FunctionExpression' ||
node.type === 'ArrowFunctionExpression'
);
}
/** Bodies that open their own `var` scope: a `var` inside one never reaches the enclosing function. */
function isVarScopeBoundary(node: AstNode): boolean {
return isFunctionNode(node) || node.type === 'StaticBlock' || node.type === 'TSModuleBlock';
}
function hoistedVarNames(node: unknown, into: string[]): void {
for (const child of Array.isArray(node) ? node : [node]) {
const record = asNode(child);
if (record === undefined || isVarScopeBoundary(record)) continue;
if (record.type === 'VariableDeclaration' && record.kind === 'var') {
for (const declarator of nodeList(record.declarations)) {
patternNames(asNode(declarator)?.id, into);
}
}
for (const value of Object.values(record)) hoistedVarNames(value, into);
}
}
/**
* Names `node` declares for its own subtree: parameters, block-level lexical declarations, and the
* `var`s of a static block or namespace body. A function's own `var`s join its body only
* (`childScope`), since a parameter initializer runs before the body declarations exist.
*/
function scopeBindings(node: AstNode): string[] {
const names: string[] = [];
if (isFunctionNode(node)) {
for (const param of nodeList(node.params)) patternNames(param, names);
if (node.type === 'FunctionExpression') patternNames(node.id, names);
} else if (node.type === 'StaticBlock' || node.type === 'TSModuleBlock') {
lexicalNames(nodeList(node.body), names);
hoistedVarNames(node.body, names);
} else if (node.type === 'BlockStatement') {
lexicalNames(nodeList(node.body), names);
} else if (node.type === 'ClassExpression') {
patternNames(node.id, names);
} else if (node.type === 'SwitchStatement') {
for (const switchCase of nodeList(node.cases)) {
lexicalNames(nodeList(asNode(switchCase)?.consequent), names);
}
} else if (node.type === 'ForStatement') {
lexicalNames([node.init], names);
} else if (node.type === 'ForInStatement' || node.type === 'ForOfStatement') {
lexicalNames([node.left], names);
} else if (node.type === 'CatchClause') {
patternNames(node.param, names);
}
return names;
}
function withNames(scope: ReadonlySet<string>, names: readonly string[]): ReadonlySet<string> {
return names.length > 0 ? new Set([...scope, ...names]) : scope;
}
/**
* The scope a child property sees. Most children see everything the node declares; a switch
* discriminant runs before the case declarations exist, and only a function body sees the
* function's `var`s.
*/
function childScope(
node: AstNode,
key: string,
outer: ReadonlySet<string>,
inner: ReadonlySet<string>,
): ReadonlySet<string> {
if (node.type === 'SwitchStatement' && key === 'discriminant') return outer;
if (isFunctionNode(node) && key === 'body') {
const vars: string[] = [];
hoistedVarNames(node.body, vars);
return withNames(inner, vars);
}
return inner;
}
/**
* `visitAst` that also hands the visitor the names declared by the enclosing scopes: function
* parameters and `var`s, block-level `let`/`const`/`function`/`class`, loop heads and `catch`
* parameters. A parameter initializer, a switch discriminant, a static block and a namespace
* body keep their own boundaries. A `with` statement or `eval` can rebind names invisibly and is
* not modelled.
*/
export function visitAstScoped(
node: unknown,
shadowed: ReadonlySet<string>,
visitor: (node: AstNode, shadowed: ReadonlySet<string>) => void,
): void {
if (Array.isArray(node)) {
for (const child of node) visitAstScoped(child, shadowed, visitor);
return;
}
const record = asNode(node);
if (record === undefined) return;
const scope = withNames(shadowed, scopeBindings(record));
visitor(record, scope);
for (const [key, child] of Object.entries(record)) {
visitAstScoped(child, childScope(record, key, shadowed, scope), visitor);
}
}