carrick 0.3.138 → 0.3.140
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/package.json +7 -6
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/sidecar/dist/src/affected-set.d.ts +172 -0
- package/sidecar/dist/src/affected-set.js +936 -0
- package/sidecar/dist/src/capture/api.d.ts +34 -0
- package/sidecar/dist/src/capture/backfill.d.ts +37 -0
- package/sidecar/dist/src/capture/backfill.js +75 -0
- package/sidecar/dist/src/capture/index.js +419 -133
- package/sidecar/dist/src/capture/installed-package.js +36 -29
- package/sidecar/dist/src/capture/self-check.d.ts +14 -3
- package/sidecar/dist/src/capture/self-check.js +168 -30
- package/sidecar/dist/src/index.js +84 -14
- package/sidecar/dist/src/infer-cache.d.ts +0 -8
- package/sidecar/dist/src/infer-cache.js +0 -14
- package/sidecar/dist/src/library-claims.js +4 -3
- package/sidecar/dist/src/module-format.d.ts +13 -2
- package/sidecar/dist/src/module-format.js +31 -1
- package/sidecar/dist/src/origin.d.ts +10 -7
- package/sidecar/dist/src/origin.js +10 -7
- package/sidecar/dist/src/program-files.d.ts +34 -11
- package/sidecar/dist/src/program-files.js +52 -10
- package/sidecar/dist/src/project-loader.d.ts +7 -0
- package/sidecar/dist/src/project-loader.js +20 -0
- package/sidecar/dist/src/retype-deferred.d.ts +34 -0
- package/sidecar/dist/src/retype-deferred.js +326 -0
- package/sidecar/dist/src/retype-selector.d.ts +43 -0
- package/sidecar/dist/src/retype-selector.js +217 -0
- package/sidecar/dist/src/retype.d.ts +28 -3
- package/sidecar/dist/src/retype.js +106 -41
- package/sidecar/dist/src/type-inferrer.js +3 -2
- package/sidecar/dist/src/types.d.ts +113 -3
- package/sidecar/dist/src/validators.d.ts +413 -4
- package/sidecar/dist/src/validators.js +41 -1
|
@@ -0,0 +1,936 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which files an edit can reach (carrick#2881, carrick#3100).
|
|
3
|
+
*
|
|
4
|
+
* A PR check re-types only the aliases its change can reach. This module says
|
|
5
|
+
* where: the changed files, and every file whose types read a declaration the
|
|
6
|
+
* edit changed, followed name by name rather than file by file, so a barrel
|
|
7
|
+
* (`index.ts` with `export *`) does not turn one changed export into every
|
|
8
|
+
* importer of the package.
|
|
9
|
+
*
|
|
10
|
+
* The walk:
|
|
11
|
+
* - In a changed file, a top-level name is changed when the text of a
|
|
12
|
+
* statement declaring it changed, or when a statement declaring it mentions
|
|
13
|
+
* a changed name of the same file (so `type Dto = z.infer<typeof schema>`
|
|
14
|
+
* changes with `schema`). A statement that declares nothing (an expression
|
|
15
|
+
* statement, which can add an expando member to a function) changes every
|
|
16
|
+
* name it mentions when it changes or mentions a changed name. In a
|
|
17
|
+
* CommonJS file, a changed statement that mentions `module` or `exports`
|
|
18
|
+
* changes the whole module.
|
|
19
|
+
* - An importing file is reached when one of its imports resolves, through
|
|
20
|
+
* every re-export (the compiler's own alias resolution), to a changed
|
|
21
|
+
* declaration. A namespace import reads every export of its module, and so
|
|
22
|
+
* does a dynamic `import()`, `require()` or `import('m').T`, read as a
|
|
23
|
+
* binding of the statement that holds it. Its names that
|
|
24
|
+
* mention the reached imports change in turn, and the walk goes on to their
|
|
25
|
+
* importers. A name whose declaration mentions no changed name stops the walk
|
|
26
|
+
* there, whatever its printed type says.
|
|
27
|
+
* - An added or deleted file is an empty module (`export {};`) on its missing
|
|
28
|
+
* side. It changes every name of the file, and its importers are reached
|
|
29
|
+
* through the graph. Its export names are not matched against importers
|
|
30
|
+
* elsewhere in the repo: nothing imports a name from a file it did not
|
|
31
|
+
* resolve to. What it can change is where an import is looked up, so every
|
|
32
|
+
* import records the stem it was looked up at (a relative specifier's path,
|
|
33
|
+
* and the repo file it resolved to, without extension and trailing
|
|
34
|
+
* `/index`), and an added or deleted file whose own stem matches reaches
|
|
35
|
+
* that import's file.
|
|
36
|
+
* - A global declaration or a module augmentation (`declare global`, `declare
|
|
37
|
+
* module 'x'`) is read by files that never import it (a value returned by
|
|
38
|
+
* the augmented package carries it), so a change to one, or to a name one
|
|
39
|
+
* mentions, is returned in `augmented`: the caller widens it to every file
|
|
40
|
+
* of the programs that hold it, as TypeScript's own scope for an
|
|
41
|
+
* augmentation does. So is any change to a script (a file with no import or
|
|
42
|
+
* export, all of it global). A file that is not TypeScript or JavaScript
|
|
43
|
+
* source, or an installed package's file, reaches every file. An import that
|
|
44
|
+
* resolves to an augmented interface reads the augmentation by its own name
|
|
45
|
+
* (`AUGMENTATION`), so an unchanged augmentation stops nothing.
|
|
46
|
+
* - An export added or removed in a file that exists on both sides (an export
|
|
47
|
+
* list, or a new exported declaration) reaches every file importing that
|
|
48
|
+
* name from anywhere, and the statements of every file holding a namespace
|
|
49
|
+
* or dynamic import that read it.
|
|
50
|
+
*
|
|
51
|
+
* Dependencies run from a file to the declarations it names; the compiler
|
|
52
|
+
* never types a file from the files that import it, so nothing is followed the
|
|
53
|
+
* other way. One assumption: an installed package's declarations do not import
|
|
54
|
+
* the workspace's own sources (only a workspace package could, and its files
|
|
55
|
+
* are read at their real path, outside `node_modules`).
|
|
56
|
+
*
|
|
57
|
+
* The graph is read from a program's own checker, once per program, and keyed
|
|
58
|
+
* by path relative to a root. `serialiseGraph` writes it without statement
|
|
59
|
+
* texts; `reach` walks that form, so a caller needs no program.
|
|
60
|
+
*/
|
|
61
|
+
import * as path from 'node:path';
|
|
62
|
+
import { ts } from 'ts-morph';
|
|
63
|
+
const SOURCE_EXTENSION = /\.(?:[cm]?[jt]sx?)$/i;
|
|
64
|
+
/** The source a missing side of an added or deleted file reads as. */
|
|
65
|
+
const EMPTY_MODULE = 'export {};';
|
|
66
|
+
/** `a/b.d.ts`, `a/b.js` and `a/b/index.ts` all look up as `a/b`. */
|
|
67
|
+
export function stemOf(file) {
|
|
68
|
+
const stem = file
|
|
69
|
+
.replace(/\.d\.[cm]?ts$/i, '')
|
|
70
|
+
.replace(SOURCE_EXTENSION, '')
|
|
71
|
+
.replace(/(?:^|\/)index$/, '');
|
|
72
|
+
return stem;
|
|
73
|
+
}
|
|
74
|
+
function isInstalled(file) {
|
|
75
|
+
// A path relative to the root starts inside `node_modules` without a slash before it.
|
|
76
|
+
return `/${file}`.includes('/node_modules/');
|
|
77
|
+
}
|
|
78
|
+
function hasModifier(node, kind) {
|
|
79
|
+
return (ts.canHaveModifiers(node) ? ts.getModifiers(node) : undefined)?.some((m) => m.kind === kind) ?? false;
|
|
80
|
+
}
|
|
81
|
+
function bindingNames(name, into) {
|
|
82
|
+
if (ts.isIdentifier(name)) {
|
|
83
|
+
into.push(name.text);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
for (const element of name.elements) {
|
|
87
|
+
if (!ts.isOmittedExpression(element))
|
|
88
|
+
bindingNames(element.name, into);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The name a global declaration or module augmentation is known by: no
|
|
93
|
+
* identifier can spell it, so only an import resolving into it names it.
|
|
94
|
+
*/
|
|
95
|
+
const AUGMENTATION = '\u0000augments ';
|
|
96
|
+
function isAugmentation(name) {
|
|
97
|
+
return name.startsWith(AUGMENTATION);
|
|
98
|
+
}
|
|
99
|
+
/** The top-level names one statement declares. */
|
|
100
|
+
export function declaredNames(statement) {
|
|
101
|
+
const names = [];
|
|
102
|
+
if (ts.isVariableStatement(statement)) {
|
|
103
|
+
for (const decl of statement.declarationList.declarations)
|
|
104
|
+
bindingNames(decl.name, names);
|
|
105
|
+
}
|
|
106
|
+
else if (ts.isFunctionDeclaration(statement) ||
|
|
107
|
+
ts.isClassDeclaration(statement) ||
|
|
108
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
109
|
+
ts.isTypeAliasDeclaration(statement) ||
|
|
110
|
+
ts.isEnumDeclaration(statement)) {
|
|
111
|
+
if (statement.name)
|
|
112
|
+
names.push(statement.name.text);
|
|
113
|
+
if (hasModifier(statement, ts.SyntaxKind.DefaultKeyword))
|
|
114
|
+
names.push('default');
|
|
115
|
+
}
|
|
116
|
+
else if (ts.isModuleDeclaration(statement)) {
|
|
117
|
+
const global = ts.isIdentifier(statement.name) && statement.name.text === 'global';
|
|
118
|
+
names.push(ts.isIdentifier(statement.name) && !global ? statement.name.text : `${AUGMENTATION}${statement.name.text}`);
|
|
119
|
+
}
|
|
120
|
+
else if (ts.isImportDeclaration(statement)) {
|
|
121
|
+
const clause = statement.importClause;
|
|
122
|
+
if (clause?.name)
|
|
123
|
+
names.push(clause.name.text);
|
|
124
|
+
const bindings = clause?.namedBindings;
|
|
125
|
+
if (bindings && ts.isNamespaceImport(bindings))
|
|
126
|
+
names.push(bindings.name.text);
|
|
127
|
+
if (bindings && ts.isNamedImports(bindings))
|
|
128
|
+
for (const el of bindings.elements)
|
|
129
|
+
names.push(el.name.text);
|
|
130
|
+
}
|
|
131
|
+
else if (ts.isImportEqualsDeclaration(statement)) {
|
|
132
|
+
names.push(statement.name.text);
|
|
133
|
+
}
|
|
134
|
+
else if (ts.isExportAssignment(statement)) {
|
|
135
|
+
names.push(statement.isExportEquals ? 'export=' : 'default');
|
|
136
|
+
}
|
|
137
|
+
return names;
|
|
138
|
+
}
|
|
139
|
+
const JAVASCRIPT = /\.[cm]?jsx?$/i;
|
|
140
|
+
/**
|
|
141
|
+
* Visit `n`'s children and, in a JavaScript file, the JSDoc comments attached
|
|
142
|
+
* to it first: there `@type`, `@param`, `@returns`, `@typedef` and `@import`
|
|
143
|
+
* type the file, and `forEachChild` never enters them.
|
|
144
|
+
*/
|
|
145
|
+
function forEachChildAndJSDoc(n, js, visit) {
|
|
146
|
+
if (js)
|
|
147
|
+
for (const doc of n.jsDoc ?? [])
|
|
148
|
+
visit(doc);
|
|
149
|
+
n.forEachChild(visit);
|
|
150
|
+
}
|
|
151
|
+
/** The type names a JavaScript statement's JSDoc declares (`@typedef`, `@callback`): top-level names of the file. */
|
|
152
|
+
function jsDocNames(statement, js) {
|
|
153
|
+
if (!js)
|
|
154
|
+
return [];
|
|
155
|
+
const names = [];
|
|
156
|
+
for (const doc of statement.jsDoc ?? []) {
|
|
157
|
+
for (const tag of doc.tags ?? []) {
|
|
158
|
+
const name = typedefName(tag);
|
|
159
|
+
if (name !== undefined)
|
|
160
|
+
names.push(name);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return names;
|
|
164
|
+
}
|
|
165
|
+
function typedefName(node) {
|
|
166
|
+
if (!ts.isJSDocTypedefTag(node) && !ts.isJSDocCallbackTag(node))
|
|
167
|
+
return undefined;
|
|
168
|
+
const name = node.name;
|
|
169
|
+
return name && ts.isIdentifier(name) ? name.text : undefined;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The statements of a file, and in a JavaScript file the end-of-file token
|
|
173
|
+
* when a JSDoc comment (a trailing `@typedef`) is attached to it.
|
|
174
|
+
*/
|
|
175
|
+
function statementsOf(sourceFile, js) {
|
|
176
|
+
const statements = [...sourceFile.statements];
|
|
177
|
+
if (js && (sourceFile.endOfFileToken.jsDoc ?? []).length > 0) {
|
|
178
|
+
statements.push(sourceFile.endOfFileToken);
|
|
179
|
+
}
|
|
180
|
+
return statements;
|
|
181
|
+
}
|
|
182
|
+
function mentioned(node, js) {
|
|
183
|
+
const refs = new Set();
|
|
184
|
+
const visit = (n) => {
|
|
185
|
+
if (ts.isIdentifier(n))
|
|
186
|
+
refs.add(n.text);
|
|
187
|
+
forEachChildAndJSDoc(n, js, visit);
|
|
188
|
+
};
|
|
189
|
+
visit(node);
|
|
190
|
+
return refs;
|
|
191
|
+
}
|
|
192
|
+
function scriptKindOf(file) {
|
|
193
|
+
const ext = file.slice(file.lastIndexOf('.')).toLowerCase();
|
|
194
|
+
if (ext === '.tsx')
|
|
195
|
+
return ts.ScriptKind.TSX;
|
|
196
|
+
if (ext === '.jsx')
|
|
197
|
+
return ts.ScriptKind.JSX;
|
|
198
|
+
if (ext === '.js' || ext === '.mjs' || ext === '.cjs')
|
|
199
|
+
return ts.ScriptKind.JS;
|
|
200
|
+
return ts.ScriptKind.TS;
|
|
201
|
+
}
|
|
202
|
+
/** JavaScript text that reads as a CommonJS module. */
|
|
203
|
+
const COMMONJS_HINT = /\brequire\s*\(|\bmodule\.exports\b|\bexports\.[A-Za-z_$]/;
|
|
204
|
+
/**
|
|
205
|
+
* Whether text alone reads as a module. In TypeScript that is an import or an
|
|
206
|
+
* export. A JavaScript file is also a module when it uses CommonJS, and a
|
|
207
|
+
* `.mjs` file always is. The program reads a file by its own rules; when the
|
|
208
|
+
* two readings disagree the caller reads the file as a script.
|
|
209
|
+
*/
|
|
210
|
+
function textIsModule(sourceFile) {
|
|
211
|
+
if (ts.isExternalModule(sourceFile))
|
|
212
|
+
return true;
|
|
213
|
+
const file = sourceFile.fileName;
|
|
214
|
+
if (!JAVASCRIPT.test(file))
|
|
215
|
+
return false;
|
|
216
|
+
return /\.mjs$/i.test(file) || COMMONJS_HINT.test(sourceFile.text);
|
|
217
|
+
}
|
|
218
|
+
/** A file's shape from its text. */
|
|
219
|
+
export function shapeOf(file, text) {
|
|
220
|
+
const sourceFile = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, scriptKindOf(file));
|
|
221
|
+
return shapeOfSource(sourceFile, !textIsModule(sourceFile));
|
|
222
|
+
}
|
|
223
|
+
function shapeOfSource(sourceFile, global) {
|
|
224
|
+
const shape = {
|
|
225
|
+
declared: new Map(),
|
|
226
|
+
refs: new Map(),
|
|
227
|
+
loose: [],
|
|
228
|
+
exports: new Set(),
|
|
229
|
+
exportTargets: new Map(),
|
|
230
|
+
global,
|
|
231
|
+
};
|
|
232
|
+
const js = JAVASCRIPT.test(sourceFile.fileName);
|
|
233
|
+
const declare = (name, text, refs) => {
|
|
234
|
+
shape.declared.set(name, [...(shape.declared.get(name) ?? []), text]);
|
|
235
|
+
const known = shape.refs.get(name) ?? new Set();
|
|
236
|
+
for (const ref of refs)
|
|
237
|
+
known.add(ref);
|
|
238
|
+
shape.refs.set(name, known);
|
|
239
|
+
};
|
|
240
|
+
for (const node of statementsOf(sourceFile, js)) {
|
|
241
|
+
// With its leading comments: a JSDoc comment types a JavaScript file.
|
|
242
|
+
const text = node.getFullText(sourceFile);
|
|
243
|
+
// A top-level `@typedef` is a name of the file, exported from a module.
|
|
244
|
+
const typedefs = jsDocNames(node, js);
|
|
245
|
+
if (typedefs.length > 0) {
|
|
246
|
+
const refs = mentioned(node, js);
|
|
247
|
+
for (const name of typedefs) {
|
|
248
|
+
declare(name, text, refs);
|
|
249
|
+
shape.exports.add(name);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (node.kind === ts.SyntaxKind.EndOfFileToken) {
|
|
253
|
+
shape.loose.push({ pos: node.pos, text, refs: mentioned(node, js) });
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
const statement = node;
|
|
257
|
+
if (ts.isExportDeclaration(statement)) {
|
|
258
|
+
const clause = statement.exportClause;
|
|
259
|
+
if (!clause) {
|
|
260
|
+
// `export * from`: which names it brings is the target's; adding or
|
|
261
|
+
// removing one is a structure change the freshness sweep rebuilds on.
|
|
262
|
+
shape.exports.add(`*${statement.moduleSpecifier?.getText(sourceFile) ?? ''}`);
|
|
263
|
+
}
|
|
264
|
+
else if (ts.isNamespaceExport(clause)) {
|
|
265
|
+
shape.exports.add(clause.name.text);
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
for (const el of clause.elements) {
|
|
269
|
+
shape.exports.add(el.name.text);
|
|
270
|
+
if (!statement.moduleSpecifier)
|
|
271
|
+
shape.exportTargets.set(el.name.text, (el.propertyName ?? el.name).text);
|
|
272
|
+
// `export { a as b }` of a local: `b` reads `a`.
|
|
273
|
+
if (!statement.moduleSpecifier && el.propertyName)
|
|
274
|
+
declare(el.name.text, text, new Set([el.propertyName.text]));
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (statement.moduleSpecifier)
|
|
278
|
+
shape.loose.push({ pos: statement.pos, text, refs: new Set() });
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
const names = declaredNames(statement);
|
|
282
|
+
if (names.length === 0) {
|
|
283
|
+
shape.loose.push({ pos: statement.pos, text, refs: mentioned(statement, js) });
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const refs = ts.isImportDeclaration(statement) || ts.isImportEqualsDeclaration(statement)
|
|
287
|
+
? new Set()
|
|
288
|
+
: mentioned(statement, js);
|
|
289
|
+
for (const name of names)
|
|
290
|
+
declare(name, text, refs);
|
|
291
|
+
if (hasModifier(statement, ts.SyntaxKind.ExportKeyword) || ts.isExportAssignment(statement)) {
|
|
292
|
+
for (const name of names)
|
|
293
|
+
shape.exports.add(name);
|
|
294
|
+
}
|
|
295
|
+
if (ts.isExportAssignment(statement) && ts.isIdentifier(statement.expression)) {
|
|
296
|
+
shape.exportTargets.set(names[0], statement.expression.text);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return shape;
|
|
300
|
+
}
|
|
301
|
+
/** The names of `shape` that read any of `seeds`, the seeds included. */
|
|
302
|
+
export function closeOver(shape, seeds) {
|
|
303
|
+
const changed = new Set(seeds);
|
|
304
|
+
let grew = true;
|
|
305
|
+
while (grew) {
|
|
306
|
+
grew = false;
|
|
307
|
+
for (const [name, refs] of shape.refs) {
|
|
308
|
+
if (changed.has(name))
|
|
309
|
+
continue;
|
|
310
|
+
for (const ref of refs) {
|
|
311
|
+
if (changed.has(ref)) {
|
|
312
|
+
changed.add(name);
|
|
313
|
+
grew = true;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
for (const loose of shape.loose) {
|
|
319
|
+
if (![...loose.refs].some((ref) => changed.has(ref)))
|
|
320
|
+
continue;
|
|
321
|
+
for (const ref of loose.refs) {
|
|
322
|
+
if (shape.declared.has(ref) && !changed.has(ref)) {
|
|
323
|
+
changed.add(ref);
|
|
324
|
+
grew = true;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return changed;
|
|
330
|
+
}
|
|
331
|
+
/** The top-level names whose statements differ between two shapes of one file. */
|
|
332
|
+
function changedNames(file, before, after) {
|
|
333
|
+
const seeds = new Set();
|
|
334
|
+
for (const name of new Set([...before.declared.keys(), ...after.declared.keys()])) {
|
|
335
|
+
const a = before.declared.get(name) ?? [];
|
|
336
|
+
const b = after.declared.get(name) ?? [];
|
|
337
|
+
if (a.length !== b.length || a.some((text, i) => text !== b[i]))
|
|
338
|
+
seeds.add(name);
|
|
339
|
+
}
|
|
340
|
+
// A statement that declares nothing and is not in both: every name it
|
|
341
|
+
// mentions may read differently.
|
|
342
|
+
const count = (list) => {
|
|
343
|
+
const counts = new Map();
|
|
344
|
+
for (const { text } of list)
|
|
345
|
+
counts.set(text, (counts.get(text) ?? 0) + 1);
|
|
346
|
+
return counts;
|
|
347
|
+
};
|
|
348
|
+
const was = count(before.loose);
|
|
349
|
+
const now = count(after.loose);
|
|
350
|
+
for (const [shape, other] of [
|
|
351
|
+
[before, now],
|
|
352
|
+
[after, was],
|
|
353
|
+
]) {
|
|
354
|
+
for (const loose of shape.loose) {
|
|
355
|
+
if ((other.get(loose.text) ?? 0) > 0)
|
|
356
|
+
continue;
|
|
357
|
+
for (const ref of loose.refs)
|
|
358
|
+
if (before.declared.has(ref) || after.declared.has(ref))
|
|
359
|
+
seeds.add(ref);
|
|
360
|
+
// `module.exports = x` and `exports.a = x` give a CommonJS module its exports.
|
|
361
|
+
if (JAVASCRIPT.test(file) && (loose.refs.has('module') || loose.refs.has('exports')))
|
|
362
|
+
seeds.add('*');
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const merged = {
|
|
366
|
+
declared: new Map([...before.declared, ...after.declared]),
|
|
367
|
+
refs: new Map(),
|
|
368
|
+
loose: [...before.loose, ...after.loose],
|
|
369
|
+
exports: new Set(),
|
|
370
|
+
exportTargets: new Map(),
|
|
371
|
+
global: false,
|
|
372
|
+
};
|
|
373
|
+
for (const source of [before.refs, after.refs]) {
|
|
374
|
+
for (const [name, refs] of source) {
|
|
375
|
+
const known = merged.refs.get(name) ?? new Set();
|
|
376
|
+
for (const ref of refs)
|
|
377
|
+
known.add(ref);
|
|
378
|
+
merged.refs.set(name, known);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return closeOver(merged, seeds);
|
|
382
|
+
}
|
|
383
|
+
function topStatement(node) {
|
|
384
|
+
let at = node;
|
|
385
|
+
while (at.parent && !ts.isSourceFile(at.parent))
|
|
386
|
+
at = at.parent;
|
|
387
|
+
return at.parent && ts.isSourceFile(at.parent) ? at : undefined;
|
|
388
|
+
}
|
|
389
|
+
function originsOfSymbol(checker, symbol, into) {
|
|
390
|
+
if (!symbol)
|
|
391
|
+
return;
|
|
392
|
+
const target = symbol.flags & ts.SymbolFlags.Alias ? safeAliased(checker, symbol) : symbol;
|
|
393
|
+
for (const decl of target?.declarations ?? []) {
|
|
394
|
+
const sourceFile = decl.getSourceFile();
|
|
395
|
+
if (ts.isSourceFile(decl)) {
|
|
396
|
+
into.push({ file: sourceFile.fileName, name: '*' });
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
const typedef = typedefName(decl);
|
|
400
|
+
if (typedef !== undefined) {
|
|
401
|
+
into.push({ file: sourceFile.fileName, name: typedef });
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
const top = topStatement(decl);
|
|
405
|
+
const names = top ? declaredNames(top) : [];
|
|
406
|
+
if (names.length === 0)
|
|
407
|
+
into.push({ file: sourceFile.fileName, name: '*' });
|
|
408
|
+
for (const name of names)
|
|
409
|
+
into.push({ file: sourceFile.fileName, name });
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
function safeAliased(checker, symbol) {
|
|
413
|
+
try {
|
|
414
|
+
return checker.getAliasedSymbol(symbol);
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
return undefined;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
/** Every declaration a module's exports resolve to, and the module file itself when it exports `=`. */
|
|
421
|
+
function originsOfModule(checker, moduleSymbol, into) {
|
|
422
|
+
if (!moduleSymbol)
|
|
423
|
+
return;
|
|
424
|
+
const target = moduleSymbol.flags & ts.SymbolFlags.Alias ? safeAliased(checker, moduleSymbol) : moduleSymbol;
|
|
425
|
+
if (!target)
|
|
426
|
+
return;
|
|
427
|
+
for (const decl of target.declarations ?? [])
|
|
428
|
+
into.push({ file: decl.getSourceFile().fileName, name: '*' });
|
|
429
|
+
let exports = [];
|
|
430
|
+
try {
|
|
431
|
+
exports = checker.getExportsOfModule(target);
|
|
432
|
+
}
|
|
433
|
+
catch {
|
|
434
|
+
exports = [];
|
|
435
|
+
}
|
|
436
|
+
for (const exported of exports)
|
|
437
|
+
originsOfSymbol(checker, exported, into);
|
|
438
|
+
}
|
|
439
|
+
const WHOLE_MODULE_HINT = /\bimport\s*\(|\brequire\s*\(/;
|
|
440
|
+
/** A JavaScript file's `@import` tag. */
|
|
441
|
+
const JSDOC_IMPORT_HINT = /@import\b/;
|
|
442
|
+
/** The name a dynamic import, `require()` or import type is bound under: no identifier can spell it. */
|
|
443
|
+
const WHOLE_MODULE = '\u0000reads module ';
|
|
444
|
+
/**
|
|
445
|
+
* Whether the program reads a file as a module: an ES module, or CommonJS
|
|
446
|
+
* JavaScript (`module.exports`, `exports.a`). The checker's own reading, which
|
|
447
|
+
* the typings do not list; it is set once the program is bound.
|
|
448
|
+
*/
|
|
449
|
+
function isModuleInProgram(sourceFile) {
|
|
450
|
+
return ts.isExternalOrCommonJsModule(sourceFile);
|
|
451
|
+
}
|
|
452
|
+
/** A path relative to `root`, with `/` separators. */
|
|
453
|
+
function relativeTo(root, file) {
|
|
454
|
+
return path.relative(root, file).split(path.sep).join('/');
|
|
455
|
+
}
|
|
456
|
+
/** A repo file's path relative to `root`; `undefined` for an installed file. */
|
|
457
|
+
function repoPath(root, file) {
|
|
458
|
+
return isInstalled(file) ? undefined : relativeTo(root, file);
|
|
459
|
+
}
|
|
460
|
+
function nodeOf(checker, root, sourceFile) {
|
|
461
|
+
const file = relativeTo(root, sourceFile.fileName);
|
|
462
|
+
const node = {
|
|
463
|
+
shape: shapeOfSource(sourceFile, !isModuleInProgram(sourceFile)),
|
|
464
|
+
bindings: new Map(),
|
|
465
|
+
importedNames: new Map(),
|
|
466
|
+
wholeLocals: new Set(),
|
|
467
|
+
stems: [],
|
|
468
|
+
};
|
|
469
|
+
/** The origins a binding resolves to: repo files only, relative to the root. */
|
|
470
|
+
const setBinding = (local, origins) => {
|
|
471
|
+
const seen = new Set();
|
|
472
|
+
const kept = [];
|
|
473
|
+
for (const origin of origins) {
|
|
474
|
+
const at = repoPath(root, origin.file);
|
|
475
|
+
if (at === undefined || seen.has(`${at}\0${origin.name}`))
|
|
476
|
+
continue;
|
|
477
|
+
seen.add(`${at}\0${origin.name}`);
|
|
478
|
+
kept.push({ file: at, name: origin.name });
|
|
479
|
+
}
|
|
480
|
+
node.bindings.set(local, kept);
|
|
481
|
+
};
|
|
482
|
+
/** Records where a specifier was looked up, and the repo file it resolved to. */
|
|
483
|
+
const lookUp = (specifier, locals) => {
|
|
484
|
+
if (!ts.isStringLiteralLike(specifier))
|
|
485
|
+
return;
|
|
486
|
+
const stems = [];
|
|
487
|
+
if (specifier.text.startsWith('.')) {
|
|
488
|
+
stems.push(stemOf(path.posix.normalize(path.posix.join(path.posix.dirname(file), specifier.text))));
|
|
489
|
+
}
|
|
490
|
+
let target;
|
|
491
|
+
const declaration = checker.getSymbolAtLocation(specifier)?.declarations?.[0];
|
|
492
|
+
if (declaration)
|
|
493
|
+
target = repoPath(root, declaration.getSourceFile().fileName);
|
|
494
|
+
if (target !== undefined)
|
|
495
|
+
stems.push(stemOf(target));
|
|
496
|
+
if (stems.length === 0)
|
|
497
|
+
return;
|
|
498
|
+
node.stems.push({ stems: [...new Set(stems)], ...(target === undefined ? {} : { target }), locals });
|
|
499
|
+
};
|
|
500
|
+
const bind = (local, imported) => {
|
|
501
|
+
const origins = [];
|
|
502
|
+
originsOfSymbol(checker, checker.getSymbolAtLocation(local), origins);
|
|
503
|
+
setBinding(local.text, origins);
|
|
504
|
+
if (imported !== undefined)
|
|
505
|
+
node.importedNames.set(local.text, imported);
|
|
506
|
+
};
|
|
507
|
+
for (const statement of sourceFile.statements) {
|
|
508
|
+
if (ts.isImportDeclaration(statement)) {
|
|
509
|
+
const clause = statement.importClause;
|
|
510
|
+
if (!clause) {
|
|
511
|
+
lookUp(statement.moduleSpecifier, []);
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
if (clause.name)
|
|
515
|
+
bind(clause.name, 'default');
|
|
516
|
+
const bindings = clause.namedBindings;
|
|
517
|
+
if (bindings && ts.isNamespaceImport(bindings)) {
|
|
518
|
+
const origins = [];
|
|
519
|
+
originsOfModule(checker, checker.getSymbolAtLocation(statement.moduleSpecifier), origins);
|
|
520
|
+
setBinding(bindings.name.text, origins);
|
|
521
|
+
node.wholeLocals.add(bindings.name.text);
|
|
522
|
+
}
|
|
523
|
+
else if (bindings) {
|
|
524
|
+
for (const el of bindings.elements)
|
|
525
|
+
bind(el.name, (el.propertyName ?? el.name).text);
|
|
526
|
+
}
|
|
527
|
+
lookUp(statement.moduleSpecifier, declaredNames(statement));
|
|
528
|
+
}
|
|
529
|
+
else if (ts.isExportDeclaration(statement)) {
|
|
530
|
+
if (statement.moduleSpecifier)
|
|
531
|
+
lookUp(statement.moduleSpecifier, []);
|
|
532
|
+
}
|
|
533
|
+
else if (ts.isImportEqualsDeclaration(statement)) {
|
|
534
|
+
if (ts.isExternalModuleReference(statement.moduleReference)) {
|
|
535
|
+
const origins = [];
|
|
536
|
+
originsOfModule(checker, checker.getSymbolAtLocation(statement.moduleReference.expression), origins);
|
|
537
|
+
setBinding(statement.name.text, origins);
|
|
538
|
+
node.wholeLocals.add(statement.name.text);
|
|
539
|
+
lookUp(statement.moduleReference.expression, [statement.name.text]);
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
542
|
+
bind(statement.name, undefined);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
const js = JAVASCRIPT.test(sourceFile.fileName);
|
|
547
|
+
if (WHOLE_MODULE_HINT.test(sourceFile.text) || (js && JSDOC_IMPORT_HINT.test(sourceFile.text))) {
|
|
548
|
+
let holder = sourceFile.statements[0] ?? sourceFile.endOfFileToken;
|
|
549
|
+
/** The statement holding a binding reads it, as it would read an import. */
|
|
550
|
+
const holderReads = (local) => {
|
|
551
|
+
const names = holder.kind === ts.SyntaxKind.EndOfFileToken ? [] : declaredNames(holder);
|
|
552
|
+
for (const name of [...names, ...jsDocNames(holder, js)])
|
|
553
|
+
node.shape.refs.get(name)?.add(local);
|
|
554
|
+
if (names.length === 0)
|
|
555
|
+
node.shape.loose.find((loose) => loose.pos === holder.pos)?.refs.add(local);
|
|
556
|
+
};
|
|
557
|
+
const visit = (n) => {
|
|
558
|
+
// `@import { T } from 'm'`: an import declaration written in JSDoc.
|
|
559
|
+
if (ts.isJSDocImportTag(n)) {
|
|
560
|
+
const clause = n.importClause;
|
|
561
|
+
const bindings = clause?.namedBindings;
|
|
562
|
+
if (clause?.name)
|
|
563
|
+
bind(clause.name, 'default');
|
|
564
|
+
if (bindings && ts.isNamespaceImport(bindings)) {
|
|
565
|
+
const origins = [];
|
|
566
|
+
originsOfModule(checker, checker.getSymbolAtLocation(n.moduleSpecifier), origins);
|
|
567
|
+
setBinding(bindings.name.text, origins);
|
|
568
|
+
node.wholeLocals.add(bindings.name.text);
|
|
569
|
+
}
|
|
570
|
+
else if (bindings) {
|
|
571
|
+
for (const el of bindings.elements)
|
|
572
|
+
bind(el.name, (el.propertyName ?? el.name).text);
|
|
573
|
+
}
|
|
574
|
+
lookUp(n.moduleSpecifier, [
|
|
575
|
+
...(clause?.name ? [clause.name.text] : []),
|
|
576
|
+
...(bindings && ts.isNamespaceImport(bindings) ? [bindings.name.text] : []),
|
|
577
|
+
...(bindings && ts.isNamedImports(bindings) ? bindings.elements.map((el) => el.name.text) : []),
|
|
578
|
+
]);
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
let specifier;
|
|
582
|
+
/** `import('m').T` reads `T` only; `typeof import('m')` reads the whole module. */
|
|
583
|
+
let named;
|
|
584
|
+
if (ts.isImportTypeNode(n) && ts.isLiteralTypeNode(n.argument)) {
|
|
585
|
+
specifier = n.argument.literal;
|
|
586
|
+
let qualifier = n.qualifier;
|
|
587
|
+
while (qualifier && ts.isQualifiedName(qualifier))
|
|
588
|
+
qualifier = qualifier.left;
|
|
589
|
+
named = qualifier;
|
|
590
|
+
}
|
|
591
|
+
else if (ts.isCallExpression(n) &&
|
|
592
|
+
(n.expression.kind === ts.SyntaxKind.ImportKeyword ||
|
|
593
|
+
(ts.isIdentifier(n.expression) && n.expression.text === 'require')) &&
|
|
594
|
+
n.arguments[0] &&
|
|
595
|
+
ts.isStringLiteralLike(n.arguments[0])) {
|
|
596
|
+
specifier = n.arguments[0];
|
|
597
|
+
}
|
|
598
|
+
if (specifier) {
|
|
599
|
+
const local = `${WHOLE_MODULE}${node.bindings.size}`;
|
|
600
|
+
const origins = [];
|
|
601
|
+
const symbol = named && checker.getSymbolAtLocation(named);
|
|
602
|
+
if (symbol)
|
|
603
|
+
originsOfSymbol(checker, symbol, origins);
|
|
604
|
+
else
|
|
605
|
+
originsOfModule(checker, checker.getSymbolAtLocation(specifier), origins);
|
|
606
|
+
setBinding(local, origins);
|
|
607
|
+
if (!symbol)
|
|
608
|
+
node.wholeLocals.add(local);
|
|
609
|
+
lookUp(specifier, [local]);
|
|
610
|
+
holderReads(local);
|
|
611
|
+
}
|
|
612
|
+
forEachChildAndJSDoc(n, js, visit);
|
|
613
|
+
};
|
|
614
|
+
for (const statement of statementsOf(sourceFile, js)) {
|
|
615
|
+
holder = statement;
|
|
616
|
+
visit(statement);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return node;
|
|
620
|
+
}
|
|
621
|
+
function targetsOf(node) {
|
|
622
|
+
const targets = new Set();
|
|
623
|
+
for (const origins of node.bindings.values())
|
|
624
|
+
for (const origin of origins)
|
|
625
|
+
targets.add(origin.file);
|
|
626
|
+
return targets;
|
|
627
|
+
}
|
|
628
|
+
function addNode(graph, file, node) {
|
|
629
|
+
graph.nodes.set(file, node);
|
|
630
|
+
for (const target of targetsOf(node)) {
|
|
631
|
+
const set = graph.importers.get(target) ?? new Set();
|
|
632
|
+
set.add(file);
|
|
633
|
+
graph.importers.set(target, set);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* The import graph of `program`'s own (not installed) files, keyed by path
|
|
638
|
+
* relative to `root`.
|
|
639
|
+
*/
|
|
640
|
+
export function importGraphOf(program, root) {
|
|
641
|
+
const checker = program.getTypeChecker();
|
|
642
|
+
const graph = { nodes: new Map(), importers: new Map() };
|
|
643
|
+
for (const sourceFile of program.getSourceFiles()) {
|
|
644
|
+
if (program.isSourceFileDefaultLibrary(sourceFile) || isInstalled(sourceFile.fileName))
|
|
645
|
+
continue;
|
|
646
|
+
addNode(graph, relativeTo(root, sourceFile.fileName), nodeOf(checker, root, sourceFile));
|
|
647
|
+
}
|
|
648
|
+
return graph;
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* One graph from the graphs of several programs. A file two programs hold
|
|
652
|
+
* keeps every binding either read, and is a script when either reads it as
|
|
653
|
+
* one: reaching more is the safe side.
|
|
654
|
+
*/
|
|
655
|
+
export function mergeGraphs(graphs) {
|
|
656
|
+
const merged = { nodes: new Map(), importers: new Map() };
|
|
657
|
+
for (const graph of graphs) {
|
|
658
|
+
for (const [file, node] of graph.nodes) {
|
|
659
|
+
const known = merged.nodes.get(file);
|
|
660
|
+
if (!known) {
|
|
661
|
+
addNode(merged, file, node);
|
|
662
|
+
continue;
|
|
663
|
+
}
|
|
664
|
+
known.shape.global ||= node.shape.global;
|
|
665
|
+
for (const [name, refs] of node.shape.refs) {
|
|
666
|
+
const into = known.shape.refs.get(name) ?? new Set();
|
|
667
|
+
for (const ref of refs)
|
|
668
|
+
into.add(ref);
|
|
669
|
+
known.shape.refs.set(name, into);
|
|
670
|
+
if (!known.shape.declared.has(name))
|
|
671
|
+
known.shape.declared.set(name, node.shape.declared.get(name) ?? []);
|
|
672
|
+
}
|
|
673
|
+
for (const [local, origins] of node.bindings) {
|
|
674
|
+
const into = known.bindings.get(local) ?? [];
|
|
675
|
+
for (const origin of origins) {
|
|
676
|
+
if (!into.some((o) => o.file === origin.file && o.name === origin.name))
|
|
677
|
+
into.push(origin);
|
|
678
|
+
}
|
|
679
|
+
known.bindings.set(local, into);
|
|
680
|
+
}
|
|
681
|
+
for (const [local, imported] of node.importedNames)
|
|
682
|
+
known.importedNames.set(local, imported);
|
|
683
|
+
for (const local of node.wholeLocals)
|
|
684
|
+
known.wholeLocals.add(local);
|
|
685
|
+
for (const entry of node.stems) {
|
|
686
|
+
if (!known.stems.some((e) => JSON.stringify(e) === JSON.stringify(entry)))
|
|
687
|
+
known.stems.push(entry);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
// Importers follow the merged bindings.
|
|
692
|
+
merged.importers.clear();
|
|
693
|
+
for (const [file, node] of merged.nodes) {
|
|
694
|
+
for (const target of targetsOf(node)) {
|
|
695
|
+
const set = merged.importers.get(target) ?? new Set();
|
|
696
|
+
set.add(file);
|
|
697
|
+
merged.importers.set(target, set);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
return merged;
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* The files `changes` can reach in the program `graph` was read from, or
|
|
704
|
+
* `'all'` when a change reaches every file. Read the graph from the program
|
|
705
|
+
* as it was before the change. A file that is added or deleted has `''` on its
|
|
706
|
+
* missing side.
|
|
707
|
+
*/
|
|
708
|
+
export function affectedFiles(graph, changes) {
|
|
709
|
+
/** Changed top-level names, by file; `*` is every name of the file. */
|
|
710
|
+
const changed = new Map();
|
|
711
|
+
const affected = new Set();
|
|
712
|
+
const addedOrRemoved = new Set();
|
|
713
|
+
/** The changed files whose exports were added or removed, and whether one had none before. */
|
|
714
|
+
const exportsChanged = new Set();
|
|
715
|
+
let exportedNothing = false;
|
|
716
|
+
const queue = [];
|
|
717
|
+
/** The files with a changed script, global declaration or module augmentation, or one that reads a changed name. */
|
|
718
|
+
const augmented = new Set();
|
|
719
|
+
/** The stems of the added and deleted files. */
|
|
720
|
+
const moved = new Set();
|
|
721
|
+
const grow = (file, names) => {
|
|
722
|
+
const known = changed.get(file) ?? new Set();
|
|
723
|
+
const size = known.size;
|
|
724
|
+
for (const name of names) {
|
|
725
|
+
known.add(name);
|
|
726
|
+
if (isAugmentation(name))
|
|
727
|
+
augmented.add(file);
|
|
728
|
+
}
|
|
729
|
+
changed.set(file, known);
|
|
730
|
+
if (known.size !== size)
|
|
731
|
+
queue.push(file);
|
|
732
|
+
};
|
|
733
|
+
for (const change of changes) {
|
|
734
|
+
if (change.before === change.after)
|
|
735
|
+
continue;
|
|
736
|
+
if (isInstalled(change.file) || !SOURCE_EXTENSION.test(change.file))
|
|
737
|
+
return 'all';
|
|
738
|
+
const added = change.before === '';
|
|
739
|
+
const deleted = change.after === '';
|
|
740
|
+
// A missing side is an empty module, never a script.
|
|
741
|
+
const before = shapeOf(change.file, added ? EMPTY_MODULE : change.before);
|
|
742
|
+
const after = shapeOf(change.file, deleted ? EMPTY_MODULE : change.after);
|
|
743
|
+
// The program reads the file by its own rules; when it and the text
|
|
744
|
+
// disagree, the file is a script.
|
|
745
|
+
const script = before.global || after.global || (graph.nodes.get(change.file)?.shape.global ?? false);
|
|
746
|
+
if (!deleted)
|
|
747
|
+
affected.add(change.file);
|
|
748
|
+
if (script) {
|
|
749
|
+
augmented.add(change.file);
|
|
750
|
+
continue;
|
|
751
|
+
}
|
|
752
|
+
if (added || deleted) {
|
|
753
|
+
// Nothing imported a name from a file that did not exist, or no longer
|
|
754
|
+
// does: what the change reaches is the file's importers, and imports
|
|
755
|
+
// that looked up its stem.
|
|
756
|
+
moved.add(stemOf(change.file));
|
|
757
|
+
grow(change.file, [...changedNames(change.file, before, after), '*']);
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
const exported = [
|
|
761
|
+
...[...before.exports].filter((name) => !after.exports.has(name)),
|
|
762
|
+
...[...after.exports].filter((name) => !before.exports.has(name)),
|
|
763
|
+
...[...after.exportTargets].filter(([name, local]) => before.exportTargets.has(name) && before.exportTargets.get(name) !== local).map(([name]) => name),
|
|
764
|
+
];
|
|
765
|
+
for (const name of exported)
|
|
766
|
+
addedOrRemoved.add(name);
|
|
767
|
+
if (exported.length > 0)
|
|
768
|
+
exportsChanged.add(change.file);
|
|
769
|
+
if (exported.length > 0 && before.exports.size === 0)
|
|
770
|
+
exportedNothing = true;
|
|
771
|
+
grow(change.file, changedNames(change.file, before, after));
|
|
772
|
+
}
|
|
773
|
+
if (moved.size > 0) {
|
|
774
|
+
// An import looked up at a stem the change adds or removes may resolve
|
|
775
|
+
// elsewhere now: its file is reached, and so is whoever read the file it
|
|
776
|
+
// resolved to.
|
|
777
|
+
for (const [file, node] of graph.nodes) {
|
|
778
|
+
for (const entry of node.stems) {
|
|
779
|
+
if (!entry.stems.some((stem) => moved.has(stem)))
|
|
780
|
+
continue;
|
|
781
|
+
affected.add(file);
|
|
782
|
+
grow(file, closeOver(node.shape, entry.locals));
|
|
783
|
+
if (entry.target !== undefined)
|
|
784
|
+
grow(entry.target, ['*']);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
if (addedOrRemoved.size > 0) {
|
|
789
|
+
// Which declaration an import of that name resolves to may have moved.
|
|
790
|
+
for (const [file, node] of graph.nodes) {
|
|
791
|
+
const hit = [...node.importedNames].filter(([, imported]) => addedOrRemoved.has(imported)).map(([local]) => local);
|
|
792
|
+
// A whole module reads the new or removed name when it re-exports the
|
|
793
|
+
// changed file, which shows as an origin there unless that file
|
|
794
|
+
// exported nothing before.
|
|
795
|
+
for (const local of node.wholeLocals) {
|
|
796
|
+
if (exportedNothing || node.bindings.get(local).some((origin) => exportsChanged.has(origin.file)))
|
|
797
|
+
hit.push(local);
|
|
798
|
+
}
|
|
799
|
+
if (hit.length === 0)
|
|
800
|
+
continue;
|
|
801
|
+
affected.add(file);
|
|
802
|
+
grow(file, closeOver(node.shape, hit));
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
const reaches = (origin) => {
|
|
806
|
+
const names = changed.get(origin.file);
|
|
807
|
+
if (!names || names.size === 0)
|
|
808
|
+
return false;
|
|
809
|
+
return origin.name === '*' || names.has('*') || names.has(origin.name);
|
|
810
|
+
};
|
|
811
|
+
while (queue.length > 0) {
|
|
812
|
+
const target = queue.shift();
|
|
813
|
+
for (const importer of graph.importers.get(target) ?? []) {
|
|
814
|
+
const node = graph.nodes.get(importer);
|
|
815
|
+
if (!node)
|
|
816
|
+
continue;
|
|
817
|
+
const hit = [];
|
|
818
|
+
for (const [local, origins] of node.bindings)
|
|
819
|
+
if (origins.some(reaches))
|
|
820
|
+
hit.push(local);
|
|
821
|
+
if (hit.length === 0)
|
|
822
|
+
continue;
|
|
823
|
+
affected.add(importer);
|
|
824
|
+
grow(importer, closeOver(node.shape, hit));
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
return { files: affected, exportsMoved: addedOrRemoved.size > 0, augmented };
|
|
828
|
+
}
|
|
829
|
+
// ===========================================================================
|
|
830
|
+
// The graph as data
|
|
831
|
+
// ===========================================================================
|
|
832
|
+
export function serialiseGraph(graph) {
|
|
833
|
+
const strings = [];
|
|
834
|
+
const index = new Map();
|
|
835
|
+
const id = (text) => {
|
|
836
|
+
let at = index.get(text);
|
|
837
|
+
if (at === undefined) {
|
|
838
|
+
at = strings.length;
|
|
839
|
+
strings.push(text);
|
|
840
|
+
index.set(text, at);
|
|
841
|
+
}
|
|
842
|
+
return at;
|
|
843
|
+
};
|
|
844
|
+
const files = [];
|
|
845
|
+
for (const [file, node] of [...graph.nodes].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
846
|
+
const { shape } = node;
|
|
847
|
+
// An identifier matters to the walk only when it is a name that can be
|
|
848
|
+
// changed: a binding, a whole-module local, or a name the file declares.
|
|
849
|
+
const matters = (ref) => node.bindings.has(ref) || shape.declared.has(ref);
|
|
850
|
+
const refs = [];
|
|
851
|
+
for (const [name, mentions] of shape.refs) {
|
|
852
|
+
const kept = [...mentions].filter(matters);
|
|
853
|
+
if (kept.length > 0)
|
|
854
|
+
refs.push([id(name), ...kept.map(id)]);
|
|
855
|
+
}
|
|
856
|
+
const loose = [];
|
|
857
|
+
for (const statement of shape.loose) {
|
|
858
|
+
const kept = [...statement.refs].filter(matters);
|
|
859
|
+
if (kept.length > 0)
|
|
860
|
+
loose.push(kept.map(id));
|
|
861
|
+
}
|
|
862
|
+
files.push({
|
|
863
|
+
p: id(file),
|
|
864
|
+
g: shape.global ? 1 : 0,
|
|
865
|
+
d: [...shape.declared.keys()].map(id),
|
|
866
|
+
r: refs,
|
|
867
|
+
l: loose,
|
|
868
|
+
e: [...shape.exports].map(id),
|
|
869
|
+
t: [...shape.exportTargets].map(([name, local]) => [id(name), id(local)]),
|
|
870
|
+
b: [...node.bindings].map(([local, origins]) => [
|
|
871
|
+
id(local),
|
|
872
|
+
node.importedNames.has(local) ? id(node.importedNames.get(local)) : -1,
|
|
873
|
+
origins.flatMap((origin) => [id(origin.file), id(origin.name)]),
|
|
874
|
+
]),
|
|
875
|
+
w: [...node.wholeLocals].map(id),
|
|
876
|
+
s: node.stems.map((entry) => [
|
|
877
|
+
entry.target === undefined ? -1 : id(entry.target),
|
|
878
|
+
entry.locals.map(id),
|
|
879
|
+
entry.stems.map(id),
|
|
880
|
+
]),
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
return { version: 1, strings, files };
|
|
884
|
+
}
|
|
885
|
+
/** The graph `serialiseGraph` wrote. Throws on an index outside the string table. */
|
|
886
|
+
export function graphFromSerial(serial) {
|
|
887
|
+
const text = (at) => {
|
|
888
|
+
const value = serial.strings[at];
|
|
889
|
+
if (value === undefined)
|
|
890
|
+
throw new Error(`Graph string ${at} is outside the table`);
|
|
891
|
+
return value;
|
|
892
|
+
};
|
|
893
|
+
const graph = { nodes: new Map(), importers: new Map() };
|
|
894
|
+
for (const file of serial.files) {
|
|
895
|
+
const shape = {
|
|
896
|
+
declared: new Map(file.d.map((at) => [text(at), []])),
|
|
897
|
+
refs: new Map(file.r.map(([name, ...mentions]) => [text(name), new Set(mentions.map(text))])),
|
|
898
|
+
loose: file.l.map((mentions, pos) => ({ pos, text: '', refs: new Set(mentions.map(text)) })),
|
|
899
|
+
exports: new Set(file.e.map(text)),
|
|
900
|
+
exportTargets: new Map(file.t.map(([name, local]) => [text(name), text(local)])),
|
|
901
|
+
global: file.g === 1,
|
|
902
|
+
};
|
|
903
|
+
const node = {
|
|
904
|
+
shape,
|
|
905
|
+
bindings: new Map(),
|
|
906
|
+
importedNames: new Map(),
|
|
907
|
+
wholeLocals: new Set(file.w.map(text)),
|
|
908
|
+
stems: file.s.map(([target, locals, stems]) => ({
|
|
909
|
+
stems: stems.map(text),
|
|
910
|
+
...(target === -1 ? {} : { target: text(target) }),
|
|
911
|
+
locals: locals.map(text),
|
|
912
|
+
})),
|
|
913
|
+
};
|
|
914
|
+
for (const [local, imported, origins] of file.b) {
|
|
915
|
+
node.bindings.set(text(local), Array.from({ length: origins.length / 2 }, (_, i) => ({
|
|
916
|
+
file: text(origins[2 * i]),
|
|
917
|
+
name: text(origins[2 * i + 1]),
|
|
918
|
+
})));
|
|
919
|
+
if (imported !== -1)
|
|
920
|
+
node.importedNames.set(text(local), text(imported));
|
|
921
|
+
}
|
|
922
|
+
addNode(graph, text(file.p), node);
|
|
923
|
+
}
|
|
924
|
+
return graph;
|
|
925
|
+
}
|
|
926
|
+
/** `affectedFiles` over a serialised graph, with the answer sorted. */
|
|
927
|
+
export function reach(serial, changes) {
|
|
928
|
+
const answer = affectedFiles(graphFromSerial(serial), changes);
|
|
929
|
+
if (answer === 'all')
|
|
930
|
+
return { reached: 'all', augmented: [], exports_moved: false };
|
|
931
|
+
return {
|
|
932
|
+
reached: [...answer.files].sort(),
|
|
933
|
+
augmented: [...answer.augmented].sort(),
|
|
934
|
+
exports_moved: answer.exportsMoved,
|
|
935
|
+
};
|
|
936
|
+
}
|