vigiles 41.1.0 → 41.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/action.yml +78 -2
- package/dist/cli-flag-check.js +11 -1
- package/dist/cli-main.js +82 -4
- package/dist/core/changed-base.d.ts +34 -0
- package/dist/core/changed-base.js +82 -0
- package/dist/core/config-schema.d.ts +9 -0
- package/dist/core/config-schema.js +7 -0
- package/dist/core/hook-program.d.ts +77 -11
- package/dist/core/hook-program.js +113 -44
- package/dist/core/hook-providers.d.ts +22 -6
- package/dist/core/hook-providers.js +57 -23
- package/dist/core/test-reach.d.ts +99 -0
- package/dist/core/test-reach.js +725 -0
- package/dist/core/test-selection.d.ts +86 -0
- package/dist/core/test-selection.js +176 -0
- package/dist/hook-runtime.js +19 -5
- package/dist/hook.d.ts +1 -1
- package/dist/host/changed-files.io.d.ts +23 -0
- package/dist/host/changed-files.io.js +181 -0
- package/dist/vigilesrc.schema.json +19 -0
- package/package.json +1 -1
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.reachMatch = reachMatch;
|
|
7
|
+
exports.readProject = readProject;
|
|
8
|
+
exports.posixRelative = posixRelative;
|
|
9
|
+
exports.reachFor = reachFor;
|
|
10
|
+
/**
|
|
11
|
+
* What one harness file can REACH — the files whose change can change its
|
|
12
|
+
* outcome — for `vigiles test --changed` (`./test-selection.ts` decides with it).
|
|
13
|
+
*
|
|
14
|
+
* Three edges, followed from the harness through every code file it reaches:
|
|
15
|
+
*
|
|
16
|
+
* 1. IMPORTS — relative `import`/`export … from`/`require`/literal `import()`,
|
|
17
|
+
* found by TypeScript's `preProcessFile` and resolved by `resolveModuleName`.
|
|
18
|
+
* 2. EFFECT CALLS — a call that runs, reads or copies a file: the `spawn`/`exec`
|
|
19
|
+
* family (the command and its argv array), `readFile*`/`createReadStream`,
|
|
20
|
+
* `cp*`/`copyFile*`, and `runHook(command)`. Only CALL ARGUMENTS count: a path
|
|
21
|
+
* inside an object literal (`tool_input.file_path` in a synthetic hook event)
|
|
22
|
+
* is a fixture, never a dependency. A script the call runs is followed like
|
|
23
|
+
* an import, so its own imports and calls count too.
|
|
24
|
+
* 3. BUILD OUTPUT → SOURCE — a path inside the tsconfig's `outDir` is read as its
|
|
25
|
+
* `rootDir` counterpart (`dist/x.js` → `src/x.ts`), so a harness that imports
|
|
26
|
+
* the build is reached by a change to the source behind it.
|
|
27
|
+
*
|
|
28
|
+
* An effect path is EVALUATED, not matched as text: string literals, `+` and
|
|
29
|
+
* template literals, `path.join`/`resolve`/`dirname`, `__dirname`,
|
|
30
|
+
* `import.meta.{url,dirname,filename}`, `new URL(x, import.meta.url)`,
|
|
31
|
+
* `fileURLToPath`, `process.cwd()`, and a `const` bound to any of those. A path
|
|
32
|
+
* whose start is known and whose end is not (`join(ROOT, "data", name)`) is a
|
|
33
|
+
* PREFIX: any change under `data/` reaches the harness. A path with nothing
|
|
34
|
+
* known is UNRESOLVED and reported as such; what to do with it is the
|
|
35
|
+
* selector's decision, not this module's.
|
|
36
|
+
*
|
|
37
|
+
* Pure: every read goes through {@link SourceHost}.
|
|
38
|
+
*/
|
|
39
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
40
|
+
const node_path_1 = require("node:path");
|
|
41
|
+
/** Does a change to `changed` (cwd-relative) reach through `r`, and how? */
|
|
42
|
+
function reachMatch(r, changed) {
|
|
43
|
+
if (r.imports.has(changed))
|
|
44
|
+
return { kind: "import" };
|
|
45
|
+
const dep = r.effects.find((d) => d.prefix
|
|
46
|
+
? changed.startsWith(d.path)
|
|
47
|
+
: changed === d.path || changed.startsWith(`${d.path}/`));
|
|
48
|
+
return dep === undefined ? undefined : { kind: "reads", call: dep.call };
|
|
49
|
+
}
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// tsconfig — outDir → rootDir
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
/**
|
|
54
|
+
* `<cwd>/tsconfig.json` as TypeScript reads it. No tsconfig → undefined. With
|
|
55
|
+
* no `outDir`, `build` is undefined and paths are taken as they are; an unset
|
|
56
|
+
* `rootDir` is tsc's own default (see {@link commonSourceDir}).
|
|
57
|
+
*/
|
|
58
|
+
function readProject(cwd, host) {
|
|
59
|
+
const path = (0, node_path_1.join)(cwd, "tsconfig.json");
|
|
60
|
+
if (!host.isFile(path))
|
|
61
|
+
return undefined;
|
|
62
|
+
const readFile = (p) => host.read(p) ?? undefined;
|
|
63
|
+
const raw = typescript_1.default.readConfigFile(path, readFile);
|
|
64
|
+
if (raw.error !== undefined)
|
|
65
|
+
return undefined;
|
|
66
|
+
const parsed = typescript_1.default.parseJsonConfigFileContent(raw.config, {
|
|
67
|
+
useCaseSensitiveFileNames: true,
|
|
68
|
+
readDirectory: host.readDirectory,
|
|
69
|
+
fileExists: host.isFile,
|
|
70
|
+
readFile,
|
|
71
|
+
}, cwd);
|
|
72
|
+
return { build: buildLayout(parsed, cwd), options: parsed.options };
|
|
73
|
+
}
|
|
74
|
+
function buildLayout(parsed, cwd) {
|
|
75
|
+
const { outDir, rootDir, composite } = parsed.options;
|
|
76
|
+
if (outDir === undefined)
|
|
77
|
+
return undefined;
|
|
78
|
+
return {
|
|
79
|
+
outDir: (0, node_path_1.resolve)(cwd, outDir),
|
|
80
|
+
rootDir: rootDir !== undefined || composite === true
|
|
81
|
+
? (0, node_path_1.resolve)(cwd, rootDir ?? ".")
|
|
82
|
+
: commonSourceDir(parsed.fileNames, cwd),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const DECLARATION = /\.d\.[cm]?ts$/;
|
|
86
|
+
/**
|
|
87
|
+
* tsc's default `rootDir` without `composite`: the deepest folder holding every
|
|
88
|
+
* non-declaration input (measured: `include: ["src/**\/*.ts"]` emits
|
|
89
|
+
* `src/helper.ts` to `dist/helper.js`). No inputs → the tsconfig's folder.
|
|
90
|
+
*/
|
|
91
|
+
function commonSourceDir(files, cwd) {
|
|
92
|
+
const dirs = files
|
|
93
|
+
.filter((f) => !DECLARATION.test(f))
|
|
94
|
+
.map((f) => (0, node_path_1.dirname)((0, node_path_1.resolve)(cwd, f)).split(node_path_1.sep));
|
|
95
|
+
const [first, ...rest] = dirs;
|
|
96
|
+
if (first === undefined)
|
|
97
|
+
return cwd;
|
|
98
|
+
const depth = first.findIndex((part, i) => rest.some((d) => d[i] !== part));
|
|
99
|
+
return first.slice(0, depth < 0 ? first.length : depth).join(node_path_1.sep) || node_path_1.sep;
|
|
100
|
+
}
|
|
101
|
+
/** Output extension → the source extensions that compile to it. */
|
|
102
|
+
const SOURCE_EXTS = {
|
|
103
|
+
".js": [".ts", ".tsx", ".js"],
|
|
104
|
+
".mjs": [".mts", ".mjs"],
|
|
105
|
+
".cjs": [".cts", ".cjs"],
|
|
106
|
+
};
|
|
107
|
+
function isInside(path, dir) {
|
|
108
|
+
return path === dir || path.startsWith(dir + node_path_1.sep);
|
|
109
|
+
}
|
|
110
|
+
/** `dist/x.js` → `src/x.ts` when that source exists; otherwise unchanged. */
|
|
111
|
+
function toSource(abs, layout, host) {
|
|
112
|
+
if (layout === undefined || !isInside(abs, layout.outDir))
|
|
113
|
+
return abs;
|
|
114
|
+
const rel = (0, node_path_1.relative)(layout.outDir, abs);
|
|
115
|
+
const twin = (0, node_path_1.join)(layout.rootDir, rel);
|
|
116
|
+
const dts = rel.endsWith(".d.ts");
|
|
117
|
+
const ext = dts ? ".d.ts" : (0, node_path_1.extname)(rel);
|
|
118
|
+
const stem = (0, node_path_1.join)(layout.rootDir, rel.slice(0, rel.length - ext.length));
|
|
119
|
+
const exts = dts ? [".ts"] : (SOURCE_EXTS[ext] ?? []);
|
|
120
|
+
return exts.map((e) => stem + e).find((p) => host.isFile(p)) ?? twin;
|
|
121
|
+
}
|
|
122
|
+
const UNK = { k: "unk" };
|
|
123
|
+
const DATA = { k: "data" };
|
|
124
|
+
const TMP = { k: "tmp" };
|
|
125
|
+
/** Calls that make or name a temporary directory. */
|
|
126
|
+
const TEMP_DIRS = new Set([
|
|
127
|
+
"mkdtemp",
|
|
128
|
+
"mkdtempSync",
|
|
129
|
+
"tmpdir",
|
|
130
|
+
"makeTmpDir",
|
|
131
|
+
]);
|
|
132
|
+
const str = (s) => ({ k: "str", s });
|
|
133
|
+
function concat(a, b) {
|
|
134
|
+
if (a.k === "tmp")
|
|
135
|
+
return TMP;
|
|
136
|
+
if (a.k === "str")
|
|
137
|
+
return concatKnown(a.s, b);
|
|
138
|
+
if (a.k === "pre")
|
|
139
|
+
return a;
|
|
140
|
+
return a.k === "data" && b.k === "data" ? DATA : UNK;
|
|
141
|
+
}
|
|
142
|
+
/** A known head followed by `b`. `${x}/y` starts with x, so an empty head is `b`. */
|
|
143
|
+
function concatKnown(head, b) {
|
|
144
|
+
if (head === "")
|
|
145
|
+
return b;
|
|
146
|
+
if (b.k === "str")
|
|
147
|
+
return str(head + b.s);
|
|
148
|
+
return { k: "pre", s: b.k === "pre" ? head + b.s : head };
|
|
149
|
+
}
|
|
150
|
+
function unwrap(e) {
|
|
151
|
+
return typescript_1.default.isParenthesizedExpression(e) ||
|
|
152
|
+
typescript_1.default.isAsExpression(e) ||
|
|
153
|
+
typescript_1.default.isNonNullExpression(e) ||
|
|
154
|
+
typescript_1.default.isSatisfiesExpression(e)
|
|
155
|
+
? unwrap(e.expression)
|
|
156
|
+
: e;
|
|
157
|
+
}
|
|
158
|
+
function evalExpr(raw, sc) {
|
|
159
|
+
const e = unwrap(raw);
|
|
160
|
+
if (typescript_1.default.isStringLiteralLike(e))
|
|
161
|
+
return str(e.text);
|
|
162
|
+
if (typescript_1.default.isNumericLiteral(e) || typescript_1.default.isObjectLiteralExpression(e))
|
|
163
|
+
return DATA;
|
|
164
|
+
if (typescript_1.default.isTemplateExpression(e))
|
|
165
|
+
return evalTemplate(e, sc);
|
|
166
|
+
if (typescript_1.default.isBinaryExpression(e))
|
|
167
|
+
return evalBinary(e, sc);
|
|
168
|
+
if (typescript_1.default.isIdentifier(e))
|
|
169
|
+
return evalIdent(e.text, sc);
|
|
170
|
+
if (typescript_1.default.isPropertyAccessExpression(e))
|
|
171
|
+
return evalProp(e, sc);
|
|
172
|
+
if (typescript_1.default.isCallExpression(e))
|
|
173
|
+
return evalCall(e, sc);
|
|
174
|
+
if (typescript_1.default.isNewExpression(e))
|
|
175
|
+
return evalNewUrl(e, sc);
|
|
176
|
+
return UNK;
|
|
177
|
+
}
|
|
178
|
+
const FALLBACK_OPS = new Set([
|
|
179
|
+
typescript_1.default.SyntaxKind.QuestionQuestionToken,
|
|
180
|
+
typescript_1.default.SyntaxKind.BarBarToken,
|
|
181
|
+
]);
|
|
182
|
+
/**
|
|
183
|
+
* `process.env.X ?? process.cwd()`: when the left side is not a known path the
|
|
184
|
+
* fallback is taken. An override that points somewhere else is not modelled —
|
|
185
|
+
* the folder rule and the full run on the default branch remain behind it.
|
|
186
|
+
*/
|
|
187
|
+
function evalBinary(e, sc) {
|
|
188
|
+
const op = e.operatorToken.kind;
|
|
189
|
+
if (op === typescript_1.default.SyntaxKind.PlusToken)
|
|
190
|
+
return concat(evalExpr(e.left, sc), evalExpr(e.right, sc));
|
|
191
|
+
return FALLBACK_OPS.has(op) ? evalFallback(e, sc) : UNK;
|
|
192
|
+
}
|
|
193
|
+
function evalFallback(e, sc) {
|
|
194
|
+
const left = evalExpr(e.left, sc);
|
|
195
|
+
return left.k === "str" ? left : evalExpr(e.right, sc);
|
|
196
|
+
}
|
|
197
|
+
function evalTemplate(e, sc) {
|
|
198
|
+
return e.templateSpans.reduce((acc, span) => concat(concat(acc, evalExpr(span.expression, sc)), str(span.literal.text)), str(e.head.text));
|
|
199
|
+
}
|
|
200
|
+
function evalIdent(name, sc) {
|
|
201
|
+
if (name === "__dirname")
|
|
202
|
+
return str((0, node_path_1.dirname)(sc.file));
|
|
203
|
+
if (name === "__filename")
|
|
204
|
+
return str(sc.file);
|
|
205
|
+
if (name === "undefined")
|
|
206
|
+
return DATA;
|
|
207
|
+
// A name already being evaluated on this path is a cycle (`x = x + "/a"`):
|
|
208
|
+
// unknown. Without this the sources fan out at every level — measured at 44 s
|
|
209
|
+
// for one harness whose loop variable has several assignments.
|
|
210
|
+
if (sc.visiting.has(name))
|
|
211
|
+
return UNK;
|
|
212
|
+
const sources = sc.consts(name);
|
|
213
|
+
if (sources.length === 0)
|
|
214
|
+
return UNK;
|
|
215
|
+
const inner = { ...sc, visiting: new Set([...sc.visiting, name]) };
|
|
216
|
+
return merge(sources.map((e) => evalExpr(e, inner)));
|
|
217
|
+
}
|
|
218
|
+
/** A variable with several sources is known only when they all agree. */
|
|
219
|
+
function merge(vals) {
|
|
220
|
+
const [first, ...rest] = vals;
|
|
221
|
+
if (first === undefined)
|
|
222
|
+
return UNK;
|
|
223
|
+
const same = rest.every((v) => v.k === first.k &&
|
|
224
|
+
(v.k === "str" || v.k === "pre"
|
|
225
|
+
? first.k === v.k && first.s === v.s
|
|
226
|
+
: true));
|
|
227
|
+
return same ? first : UNK;
|
|
228
|
+
}
|
|
229
|
+
const META = {
|
|
230
|
+
url: (sc) => str(sc.file),
|
|
231
|
+
filename: (sc) => str(sc.file),
|
|
232
|
+
dirname: (sc) => str((0, node_path_1.dirname)(sc.file)),
|
|
233
|
+
};
|
|
234
|
+
function evalProp(e, sc) {
|
|
235
|
+
const name = e.name.text;
|
|
236
|
+
if (typescript_1.default.isMetaProperty(e.expression))
|
|
237
|
+
return META[name]?.(sc) ?? UNK;
|
|
238
|
+
if (typescript_1.default.isIdentifier(e.expression) && e.expression.text === "process")
|
|
239
|
+
return name === "execPath" || name === "argv0" ? DATA : UNK;
|
|
240
|
+
return name === "pathname" || name === "href"
|
|
241
|
+
? evalExpr(e.expression, sc)
|
|
242
|
+
: UNK;
|
|
243
|
+
}
|
|
244
|
+
function calleeName(e) {
|
|
245
|
+
const c = e.expression;
|
|
246
|
+
if (typescript_1.default.isIdentifier(c))
|
|
247
|
+
return c.text;
|
|
248
|
+
return typescript_1.default.isPropertyAccessExpression(c) ? c.name.text : "";
|
|
249
|
+
}
|
|
250
|
+
function evalPathCall(name, args, cwd) {
|
|
251
|
+
if (args[0]?.k === "tmp")
|
|
252
|
+
return TMP;
|
|
253
|
+
const firstUnknown = args.findIndex((v) => v.k !== "str");
|
|
254
|
+
const known = args
|
|
255
|
+
.slice(0, firstUnknown < 0 ? args.length : firstUnknown)
|
|
256
|
+
.flatMap((v) => (v.k === "str" ? [v.s] : []));
|
|
257
|
+
const joined = name === "resolve" ? (0, node_path_1.resolve)(cwd, ...known) : (0, node_path_1.join)(...known);
|
|
258
|
+
if (firstUnknown < 0)
|
|
259
|
+
return str(joined);
|
|
260
|
+
const tail = args[firstUnknown];
|
|
261
|
+
if (tail?.k === "pre")
|
|
262
|
+
return { k: "pre", s: (0, node_path_1.join)(joined, tail.s) };
|
|
263
|
+
return known.length === 0 ? UNK : { k: "pre", s: joined + node_path_1.sep };
|
|
264
|
+
}
|
|
265
|
+
/** Methods taken to return the path they are called on (a trailing-slash trim, mostly). */
|
|
266
|
+
const SAME_PATH = new Set([
|
|
267
|
+
"toString",
|
|
268
|
+
"replace",
|
|
269
|
+
"trim",
|
|
270
|
+
"normalize",
|
|
271
|
+
]);
|
|
272
|
+
/** Calls whose result is decided by their arguments alone. */
|
|
273
|
+
const ARG_CALLS = {
|
|
274
|
+
join: (args, sc) => evalPathCall("join", args, sc.cwd),
|
|
275
|
+
resolve: (args, sc) => evalPathCall("resolve", args, sc.cwd),
|
|
276
|
+
dirname: ([v]) => (v?.k === "str" ? str((0, node_path_1.dirname)(v.s)) : UNK),
|
|
277
|
+
fileURLToPath: ([v]) => v ?? UNK,
|
|
278
|
+
String: ([v]) => v ?? UNK,
|
|
279
|
+
stringify: () => DATA,
|
|
280
|
+
cwd: (_, sc) => str(sc.cwd),
|
|
281
|
+
};
|
|
282
|
+
function evalCall(e, sc) {
|
|
283
|
+
const name = calleeName(e);
|
|
284
|
+
if (TEMP_DIRS.has(name))
|
|
285
|
+
return TMP;
|
|
286
|
+
if (SAME_PATH.has(name) && typescript_1.default.isPropertyAccessExpression(e.expression))
|
|
287
|
+
return evalExpr(e.expression.expression, sc);
|
|
288
|
+
const byArgs = ARG_CALLS[name];
|
|
289
|
+
return byArgs === undefined
|
|
290
|
+
? UNK
|
|
291
|
+
: byArgs(e.arguments.map((a) => evalExpr(a, sc)), sc);
|
|
292
|
+
}
|
|
293
|
+
/** `new URL(rel, base)` → a path; a URL is carried as the path it names. */
|
|
294
|
+
function evalNewUrl(e, sc) {
|
|
295
|
+
if (calleeName(e) !== "URL")
|
|
296
|
+
return UNK;
|
|
297
|
+
const [rel, base] = (e.arguments ?? []).map((a) => evalExpr(a, sc));
|
|
298
|
+
const r = rel?.k === "str" ? str(rel.s.replace(/^file:\/\//, "")) : UNK;
|
|
299
|
+
if (base === undefined || r.k !== "str")
|
|
300
|
+
return r;
|
|
301
|
+
if (base.k !== "str")
|
|
302
|
+
return UNK;
|
|
303
|
+
const dir = base.s.endsWith("/") ? base.s : (0, node_path_1.dirname)(base.s);
|
|
304
|
+
return str((0, node_path_1.resolve)(dir, r.s) + (r.s.endsWith("/") ? "/" : ""));
|
|
305
|
+
}
|
|
306
|
+
// ---------------------------------------------------------------------------
|
|
307
|
+
// Effect calls — which arguments are paths
|
|
308
|
+
// ---------------------------------------------------------------------------
|
|
309
|
+
const READS = new Set([
|
|
310
|
+
"readFile",
|
|
311
|
+
"readFileSync",
|
|
312
|
+
"createReadStream",
|
|
313
|
+
"readdir",
|
|
314
|
+
"readdirSync",
|
|
315
|
+
"existsSync",
|
|
316
|
+
"statSync",
|
|
317
|
+
"lstatSync",
|
|
318
|
+
"cp",
|
|
319
|
+
"cpSync",
|
|
320
|
+
"copyFile",
|
|
321
|
+
"copyFileSync",
|
|
322
|
+
]);
|
|
323
|
+
const SPAWNS = new Set([
|
|
324
|
+
"spawn",
|
|
325
|
+
"spawnSync",
|
|
326
|
+
"execFile",
|
|
327
|
+
"execFileSync",
|
|
328
|
+
"fork",
|
|
329
|
+
]);
|
|
330
|
+
const SHELLS = new Set(["exec", "execSync", "runHook"]);
|
|
331
|
+
function argvOf(e, sc) {
|
|
332
|
+
if (e === undefined || typescript_1.default.isObjectLiteralExpression(e))
|
|
333
|
+
return [];
|
|
334
|
+
const arr = typescript_1.default.isIdentifier(e) ? sc.consts(e.text)[0] : e;
|
|
335
|
+
if (arr === undefined || !typescript_1.default.isArrayLiteralExpression(arr))
|
|
336
|
+
return [UNK];
|
|
337
|
+
return arr.elements.map((el) => typescript_1.default.isSpreadElement(el) ? UNK : evalExpr(el, sc));
|
|
338
|
+
}
|
|
339
|
+
function tokensOf(v) {
|
|
340
|
+
if (v.k === "unk")
|
|
341
|
+
return [UNK];
|
|
342
|
+
if (v.k === "data" || v.k === "tmp")
|
|
343
|
+
return [];
|
|
344
|
+
const words = v.s
|
|
345
|
+
.split(/\s+/)
|
|
346
|
+
.filter((w) => w !== "")
|
|
347
|
+
.map((w) => str(w.replace(/^["']|["']$/g, "")));
|
|
348
|
+
if (v.k === "str")
|
|
349
|
+
return words;
|
|
350
|
+
if (/\s$/.test(v.s) || words.length === 0)
|
|
351
|
+
return [...words, UNK];
|
|
352
|
+
const last = words[words.length - 1];
|
|
353
|
+
return [
|
|
354
|
+
...words.slice(0, -1),
|
|
355
|
+
{ k: "pre", s: last?.k === "str" ? last.s : "" },
|
|
356
|
+
];
|
|
357
|
+
}
|
|
358
|
+
/** The `cwd` option: run cwd when absent, its value when known, else unnameable. */
|
|
359
|
+
function cwdOption(opts, sc) {
|
|
360
|
+
if (opts === undefined || !typescript_1.default.isObjectLiteralExpression(opts))
|
|
361
|
+
return sc.cwd;
|
|
362
|
+
const prop = opts.properties.find((p) => p.name !== undefined && typescript_1.default.isIdentifier(p.name) && p.name.text === "cwd");
|
|
363
|
+
if (prop === undefined)
|
|
364
|
+
return sc.cwd;
|
|
365
|
+
if (!typescript_1.default.isPropertyAssignment(prop))
|
|
366
|
+
return undefined;
|
|
367
|
+
const v = evalExpr(prop.initializer, sc);
|
|
368
|
+
return v.k === "str" ? (0, node_path_1.resolve)(sc.cwd, v.s) : undefined;
|
|
369
|
+
}
|
|
370
|
+
function effectArgs(e, name, sc) {
|
|
371
|
+
const [a0, a1, a2] = e.arguments;
|
|
372
|
+
const first = a0 === undefined ? DATA : evalExpr(a0, sc);
|
|
373
|
+
if (READS.has(name))
|
|
374
|
+
return [{ val: first, base: sc.cwd }];
|
|
375
|
+
if (SPAWNS.has(name)) {
|
|
376
|
+
const opts = a1 !== undefined && typescript_1.default.isObjectLiteralExpression(a1) ? a1 : a2;
|
|
377
|
+
const base = cwdOption(opts, sc);
|
|
378
|
+
return [first, ...argvOf(a1, sc)].map((val) => ({ val, base }));
|
|
379
|
+
}
|
|
380
|
+
const base = name === "runHook" ? sc.cwd : cwdOption(a1, sc);
|
|
381
|
+
return tokensOf(first).map((val) => ({ val, base }));
|
|
382
|
+
}
|
|
383
|
+
function allNodes(n, sf) {
|
|
384
|
+
return [n, ...n.getChildren(sf).flatMap((c) => allNodes(c, sf))];
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Every variable's sources: the initializer of each declaration of the name and
|
|
388
|
+
* the right side of each `name = …`. Function parameters have none, so they
|
|
389
|
+
* stay unknown. Shadowed names merge, which can only make a value unknown.
|
|
390
|
+
*/
|
|
391
|
+
function bindingsOf(nodes) {
|
|
392
|
+
const pairs = nodes.flatMap((n) => {
|
|
393
|
+
if (typescript_1.default.isVariableDeclaration(n) &&
|
|
394
|
+
typescript_1.default.isIdentifier(n.name) &&
|
|
395
|
+
n.initializer)
|
|
396
|
+
return [[n.name.text, n.initializer]];
|
|
397
|
+
if (typescript_1.default.isBinaryExpression(n) &&
|
|
398
|
+
n.operatorToken.kind === typescript_1.default.SyntaxKind.EqualsToken &&
|
|
399
|
+
typescript_1.default.isIdentifier(n.left))
|
|
400
|
+
return [[n.left.text, n.right]];
|
|
401
|
+
return [];
|
|
402
|
+
});
|
|
403
|
+
// Looked up on demand: only the names an effect argument mentions are ever
|
|
404
|
+
// asked for, and grouping every binding of a 3,000-line harness up front was
|
|
405
|
+
// quadratic (measured: a minute over 155 harnesses).
|
|
406
|
+
return (name) => pairs.flatMap(([n, e]) => (n === name ? [e] : []));
|
|
407
|
+
}
|
|
408
|
+
function depOf(arg, call) {
|
|
409
|
+
const { val, base } = arg;
|
|
410
|
+
// Under a `cwd` option nobody can name (a temp sandbox), the call works on
|
|
411
|
+
// files the harness made: an unknown argument there names no repo file.
|
|
412
|
+
if (val.k === "unk")
|
|
413
|
+
return base === undefined ? undefined : "unresolved";
|
|
414
|
+
if (val.k === "data" || val.k === "tmp")
|
|
415
|
+
return undefined;
|
|
416
|
+
return pathDep(val.s, val.k === "pre", base, call);
|
|
417
|
+
}
|
|
418
|
+
/** A flag (`-x`) or an empty string names nothing; a relative path needs a base. */
|
|
419
|
+
function pathDep(s, prefix, base, call) {
|
|
420
|
+
const raw = s.replace(/^file:\/\//, "");
|
|
421
|
+
if (raw === "" || raw.startsWith("-"))
|
|
422
|
+
return undefined;
|
|
423
|
+
if (!(0, node_path_1.isAbsolute)(raw) && base === undefined)
|
|
424
|
+
return undefined;
|
|
425
|
+
const abs = (0, node_path_1.resolve)(base ?? "/", raw);
|
|
426
|
+
return { abs: prefix && raw.endsWith(node_path_1.sep) ? abs + node_path_1.sep : abs, prefix, call };
|
|
427
|
+
}
|
|
428
|
+
const CHILD_PROCESS = new Set(["child_process", "node:child_process"]);
|
|
429
|
+
const EFFECT_MODULES = new Set([
|
|
430
|
+
...CHILD_PROCESS,
|
|
431
|
+
"fs",
|
|
432
|
+
"node:fs",
|
|
433
|
+
"fs/promises",
|
|
434
|
+
"node:fs/promises",
|
|
435
|
+
]);
|
|
436
|
+
/** Node's file and process modules, and vigiles (whose `runHook` spawns). */
|
|
437
|
+
function isEffectModule(m) {
|
|
438
|
+
return EFFECT_MODULES.has(m) || m === "vigiles" || m.startsWith("vigiles/");
|
|
439
|
+
}
|
|
440
|
+
/** `{ exec as run }` / `{ exec: run }` → [run, exec]. */
|
|
441
|
+
function aliasPairs(els) {
|
|
442
|
+
return els.flatMap((el) => {
|
|
443
|
+
if (!typescript_1.default.isIdentifier(el.name))
|
|
444
|
+
return [];
|
|
445
|
+
const original = el.propertyName && typescript_1.default.isIdentifier(el.propertyName)
|
|
446
|
+
? el.propertyName.text
|
|
447
|
+
: el.name.text;
|
|
448
|
+
return [[el.name.text, original]];
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
function importUse(d) {
|
|
452
|
+
if (!typescript_1.default.isStringLiteral(d.moduleSpecifier))
|
|
453
|
+
return undefined;
|
|
454
|
+
const clause = d.importClause;
|
|
455
|
+
const nb = clause?.namedBindings;
|
|
456
|
+
return {
|
|
457
|
+
module: d.moduleSpecifier.text,
|
|
458
|
+
aliases: nb && typescript_1.default.isNamedImports(nb) ? aliasPairs(nb.elements) : [],
|
|
459
|
+
whole: [
|
|
460
|
+
...(clause?.name ? [clause.name.text] : []),
|
|
461
|
+
...(nb && typescript_1.default.isNamespaceImport(nb) ? [nb.name.text] : []),
|
|
462
|
+
],
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
function requireUse(n) {
|
|
466
|
+
if (!typescript_1.default.isVariableDeclaration(n) || n.initializer === undefined)
|
|
467
|
+
return undefined;
|
|
468
|
+
const init = n.initializer;
|
|
469
|
+
if (!typescript_1.default.isCallExpression(init) || !typescript_1.default.isIdentifier(init.expression))
|
|
470
|
+
return undefined;
|
|
471
|
+
const arg = init.arguments[0];
|
|
472
|
+
if (init.expression.text !== "require" ||
|
|
473
|
+
!arg ||
|
|
474
|
+
!typescript_1.default.isStringLiteralLike(arg))
|
|
475
|
+
return undefined;
|
|
476
|
+
return {
|
|
477
|
+
module: arg.text,
|
|
478
|
+
aliases: typescript_1.default.isObjectBindingPattern(n.name)
|
|
479
|
+
? aliasPairs(n.name.elements)
|
|
480
|
+
: [],
|
|
481
|
+
whole: typescript_1.default.isIdentifier(n.name) ? [n.name.text] : [],
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
function apiBindings(nodes) {
|
|
485
|
+
const uses = [
|
|
486
|
+
...nodes.filter(typescript_1.default.isImportDeclaration).map(importUse),
|
|
487
|
+
...nodes.map(requireUse),
|
|
488
|
+
].filter((u) => u !== undefined && isEffectModule(u.module));
|
|
489
|
+
return {
|
|
490
|
+
aliases: new Map(uses.flatMap((u) => u.aliases)),
|
|
491
|
+
processModules: new Set(uses.filter((u) => CHILD_PROCESS.has(u.module)).flatMap((u) => u.whole)),
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* The API a call stands for, or "" when it is not one. A bare name imported
|
|
496
|
+
* from an effect module is read as what it was imported as (`exec as run` →
|
|
497
|
+
* `exec`). `exec` itself must come from `child_process`, because
|
|
498
|
+
* `RegExp.prototype.exec` shares the name; every other API name is taken as it
|
|
499
|
+
* is (an extra dependency costs a run, not a miss).
|
|
500
|
+
*/
|
|
501
|
+
function apiName(call, b) {
|
|
502
|
+
const c = call.expression;
|
|
503
|
+
if (typescript_1.default.isIdentifier(c))
|
|
504
|
+
return b.aliases.get(c.text) ?? (c.text === "exec" ? "" : c.text);
|
|
505
|
+
if (!typescript_1.default.isPropertyAccessExpression(c))
|
|
506
|
+
return "";
|
|
507
|
+
const onProcess = typescript_1.default.isIdentifier(c.expression) && b.processModules.has(c.expression.text);
|
|
508
|
+
return c.name.text === "exec" && !onProcess ? "" : c.name.text;
|
|
509
|
+
}
|
|
510
|
+
function isEffectApi(name) {
|
|
511
|
+
return READS.has(name) || SPAWNS.has(name) || SHELLS.has(name);
|
|
512
|
+
}
|
|
513
|
+
function effectsIn(abs, sf, ctx) {
|
|
514
|
+
const nodes = allNodes(sf, sf);
|
|
515
|
+
const sc = {
|
|
516
|
+
file: abs,
|
|
517
|
+
cwd: ctx.cwd,
|
|
518
|
+
consts: bindingsOf(nodes),
|
|
519
|
+
visiting: new Set(),
|
|
520
|
+
};
|
|
521
|
+
const api = apiBindings(nodes);
|
|
522
|
+
const found = nodes.filter(typescript_1.default.isCallExpression).flatMap((call) => {
|
|
523
|
+
const name = apiName(call, api);
|
|
524
|
+
if (!isEffectApi(name))
|
|
525
|
+
return [];
|
|
526
|
+
const line = sf.getLineAndCharacterOfPosition(call.getStart(sf)).line + 1;
|
|
527
|
+
const at = (a) => ({ d: depOf(a, name), name, line });
|
|
528
|
+
return effectArgs(call, name, sc).map(at);
|
|
529
|
+
});
|
|
530
|
+
const rel = posixRelative(ctx.cwd, abs);
|
|
531
|
+
return {
|
|
532
|
+
deps: found.flatMap((f) => (typeof f.d === "object" ? [f.d] : [])),
|
|
533
|
+
unresolved: found
|
|
534
|
+
.filter((f) => f.d === "unresolved")
|
|
535
|
+
.map((f) => ({ call: f.name, file: rel, line: f.line })),
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* `typescript` is already a runtime dependency, and `preProcessFile` is its
|
|
540
|
+
* import scanner. Resolution goes through `resolveModuleName` in Bundler mode,
|
|
541
|
+
* so `./x.js` finds `x.ts` the way a NodeNext project writes it, and an
|
|
542
|
+
* extensionless specifier resolves. The literal path is kept too.
|
|
543
|
+
*/
|
|
544
|
+
const RESOLVE_OPTIONS = {
|
|
545
|
+
allowJs: true,
|
|
546
|
+
resolveJsonModule: true,
|
|
547
|
+
module: typescript_1.default.ModuleKind.ESNext,
|
|
548
|
+
moduleResolution: typescript_1.default.ModuleResolutionKind.Bundler,
|
|
549
|
+
};
|
|
550
|
+
const CODE_EXT = /\.(?:[cm]?[jt]s|[jt]sx)$/;
|
|
551
|
+
/** `p` read through the build mapping, flagged when the mapping moved it. */
|
|
552
|
+
function viaBuild(p, built, ctx) {
|
|
553
|
+
const abs = toSource(p, ctx.layout, ctx.host);
|
|
554
|
+
return { abs, built: built || abs !== p };
|
|
555
|
+
}
|
|
556
|
+
function isRelative(spec) {
|
|
557
|
+
return spec.startsWith("./") || spec.startsWith("../");
|
|
558
|
+
}
|
|
559
|
+
function resolveSpec(spec, from, ctx) {
|
|
560
|
+
const r = typescript_1.default.resolveModuleName(spec, from, ctx.resolveOptions, ctx.resolver, ctx.cache).resolvedModule?.resolvedFileName;
|
|
561
|
+
return r === undefined ? undefined : (0, node_path_1.resolve)(r);
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* A relative specifier: the literal path and the resolver's answer, both read
|
|
565
|
+
* through the build mapping (`../dist/lib`, `../dist` resolve to
|
|
566
|
+
* `dist/lib.js`/`dist/index.js`, whose source is what changes). Nothing found
|
|
567
|
+
* → every path the resolver would have tried (see {@link candidatesOf}).
|
|
568
|
+
*/
|
|
569
|
+
function relativeImport(at, spec, ctx) {
|
|
570
|
+
const literal = viaBuild((0, node_path_1.resolve)((0, node_path_1.dirname)(at.abs), spec), at.built, ctx);
|
|
571
|
+
const viaTs = resolveSpec(spec, at.abs, ctx);
|
|
572
|
+
const resolved = viaTs === undefined ? [] : [viaBuild(viaTs, at.built, ctx)];
|
|
573
|
+
const found = [literal, ...resolved].filter((v) => ctx.host.isFile(v.abs));
|
|
574
|
+
return found.length > 0
|
|
575
|
+
? found
|
|
576
|
+
: candidatesOf(literal.abs).map((abs) => ({ abs, built: at.built }));
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* A bare specifier is followed when it resolves INSIDE the run's tree and not
|
|
580
|
+
* under node_modules: a workspace package (its node_modules link realpathed),
|
|
581
|
+
* a tsconfig `paths` alias, a package.json `#imports` entry, a package
|
|
582
|
+
* importing itself. A real dependency stays out — a lockfile or package.json
|
|
583
|
+
* change already runs everything.
|
|
584
|
+
*/
|
|
585
|
+
function bareImport(at, spec, ctx) {
|
|
586
|
+
const r = resolveSpec(spec, at.abs, ctx);
|
|
587
|
+
if (r === undefined || !isInside(r, ctx.cwd))
|
|
588
|
+
return [];
|
|
589
|
+
return r.split(node_path_1.sep).includes("node_modules")
|
|
590
|
+
? []
|
|
591
|
+
: [viaBuild(r, at.built, ctx)];
|
|
592
|
+
}
|
|
593
|
+
function importsIn(at, text, ctx) {
|
|
594
|
+
return typescript_1.default
|
|
595
|
+
.preProcessFile(text, true, true)
|
|
596
|
+
.importedFiles.map((i) => i.fileName)
|
|
597
|
+
.flatMap((spec) => isRelative(spec)
|
|
598
|
+
? relativeImport(at, spec, ctx)
|
|
599
|
+
: bareImport(at, spec, ctx));
|
|
600
|
+
}
|
|
601
|
+
const JS_TO_TS = {
|
|
602
|
+
".js": [".ts", ".tsx"],
|
|
603
|
+
".jsx": [".tsx"],
|
|
604
|
+
".mjs": [".mts"],
|
|
605
|
+
".cjs": [".cts"],
|
|
606
|
+
};
|
|
607
|
+
const TRIED_EXTS = [
|
|
608
|
+
".ts",
|
|
609
|
+
".tsx",
|
|
610
|
+
".mts",
|
|
611
|
+
".cts",
|
|
612
|
+
".js",
|
|
613
|
+
".mjs",
|
|
614
|
+
".cjs",
|
|
615
|
+
".json",
|
|
616
|
+
];
|
|
617
|
+
/**
|
|
618
|
+
* Every file the resolver would have tried for an import that resolves to
|
|
619
|
+
* nothing: the literal path, its TypeScript twins, and for an extensionless
|
|
620
|
+
* specifier each extension and `index` file. 🔴 A MISSING TARGET IS STILL A
|
|
621
|
+
* DEPENDENCY. A change that deletes or renames it is exactly the change that
|
|
622
|
+
* breaks the harness; dropping the edge skipped the harness and passed green.
|
|
623
|
+
* The paths are kept for matching and never visited.
|
|
624
|
+
*/
|
|
625
|
+
function candidatesOf(abs) {
|
|
626
|
+
const ext = (0, node_path_1.extname)(abs);
|
|
627
|
+
const stem = abs.slice(0, abs.length - ext.length);
|
|
628
|
+
const twins = (JS_TO_TS[ext] ?? []).map((e) => stem + e);
|
|
629
|
+
const bare = TRIED_EXTS.includes(ext) || ext === ".jsx"
|
|
630
|
+
? []
|
|
631
|
+
: TRIED_EXTS.flatMap((e) => [abs + e, (0, node_path_1.join)(abs, "index" + e)]);
|
|
632
|
+
return [abs, ...twins, ...bare];
|
|
633
|
+
}
|
|
634
|
+
const NO_FACTS = { next: [], deps: [], unresolved: [] };
|
|
635
|
+
/**
|
|
636
|
+
* One file's edges. 🔴 A BUILD'S SOURCE CONTRIBUTES IMPORTS ONLY. Code reached
|
|
637
|
+
* through `outDir` → `rootDir` is the program under test, and its file reads
|
|
638
|
+
* take their paths from the harness at run time (`join(cwd, name)` over a
|
|
639
|
+
* sandbox); scanning them made every harness of a library repository depend on
|
|
640
|
+
* the whole tree, a docs edit included (measured: 14 of 16). Its source files
|
|
641
|
+
* are dependencies through the import edge, which is the part that is known.
|
|
642
|
+
*/
|
|
643
|
+
function factsOf(at, ctx) {
|
|
644
|
+
const text = CODE_EXT.test(at.abs) ? ctx.host.read(at.abs) : null;
|
|
645
|
+
if (text === null)
|
|
646
|
+
return NO_FACTS;
|
|
647
|
+
const imports = importsIn(at, text, ctx);
|
|
648
|
+
if (at.built)
|
|
649
|
+
return { ...NO_FACTS, next: imports };
|
|
650
|
+
const sf = typescript_1.default.createSourceFile(at.abs, text, typescript_1.default.ScriptTarget.Latest, true);
|
|
651
|
+
const { deps: raw, unresolved } = effectsIn(at.abs, sf, ctx);
|
|
652
|
+
const scripts = raw
|
|
653
|
+
.filter((d) => !d.prefix && CODE_EXT.test(d.abs))
|
|
654
|
+
.map((d) => viaBuild(d.abs, false, ctx))
|
|
655
|
+
.filter((v) => ctx.host.isFile(v.abs));
|
|
656
|
+
const deps = raw.map((d) => d.prefix ? d : { ...d, abs: toSource(d.abs, ctx.layout, ctx.host) });
|
|
657
|
+
return { next: [...imports, ...scripts], deps, unresolved };
|
|
658
|
+
}
|
|
659
|
+
function walk(frontier, acc, ctx) {
|
|
660
|
+
if (frontier.length === 0)
|
|
661
|
+
return acc;
|
|
662
|
+
const facts = frontier.map((f) => factsOf(f, ctx));
|
|
663
|
+
const fresh = facts
|
|
664
|
+
.flatMap((f) => f.next)
|
|
665
|
+
.filter((v) => !acc.seen.has(v.abs));
|
|
666
|
+
const next = [...new Map(fresh.map((v) => [v.abs, v])).values()];
|
|
667
|
+
return walk(next, {
|
|
668
|
+
seen: new Set([...acc.seen, ...next.map((v) => v.abs)]),
|
|
669
|
+
deps: [...acc.deps, ...facts.flatMap((f) => f.deps)],
|
|
670
|
+
unresolved: [...acc.unresolved, ...facts.flatMap((f) => f.unresolved)],
|
|
671
|
+
}, ctx);
|
|
672
|
+
}
|
|
673
|
+
/** `relative`, in POSIX separators. */
|
|
674
|
+
function posixRelative(from, to) {
|
|
675
|
+
return (0, node_path_1.relative)(from, to).split(node_path_1.sep).join("/");
|
|
676
|
+
}
|
|
677
|
+
/** A dependency as a cwd-relative match; the cwd or a folder above it is "". */
|
|
678
|
+
function toEffectDep(d, cwd) {
|
|
679
|
+
const bare = d.abs.endsWith(node_path_1.sep) ? d.abs.slice(0, -1) : d.abs;
|
|
680
|
+
const covers = cwd === bare || cwd.startsWith(bare + node_path_1.sep);
|
|
681
|
+
if (covers)
|
|
682
|
+
return { path: "", prefix: true, call: d.call };
|
|
683
|
+
const rel = posixRelative(cwd, d.abs);
|
|
684
|
+
return {
|
|
685
|
+
path: d.prefix && d.abs.endsWith(node_path_1.sep) ? `${rel}/` : rel,
|
|
686
|
+
prefix: d.prefix,
|
|
687
|
+
call: d.call,
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
function ctxOf(input) {
|
|
691
|
+
const { cwd, host, project } = input;
|
|
692
|
+
return {
|
|
693
|
+
cwd,
|
|
694
|
+
host,
|
|
695
|
+
layout: project?.build,
|
|
696
|
+
resolveOptions: { ...project?.options, ...RESOLVE_OPTIONS },
|
|
697
|
+
resolver: {
|
|
698
|
+
fileExists: host.isFile,
|
|
699
|
+
readFile: (p) => host.read(p) ?? undefined,
|
|
700
|
+
realpath: host.realpath,
|
|
701
|
+
},
|
|
702
|
+
cache: typescript_1.default.createModuleResolutionCache(cwd, (f) => f),
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* What each harness file reaches, for one run. The resolution cache is shared
|
|
707
|
+
* by every file the returned function is asked about.
|
|
708
|
+
*/
|
|
709
|
+
function reachFor(setup) {
|
|
710
|
+
const ctx = ctxOf(setup);
|
|
711
|
+
return (file) => reachOne(ctx, file);
|
|
712
|
+
}
|
|
713
|
+
/** Everything `file` (relative to the cwd) reaches. See the module header for the three edges. */
|
|
714
|
+
function reachOne(ctx, file) {
|
|
715
|
+
const start = (0, node_path_1.resolve)(ctx.cwd, file);
|
|
716
|
+
const w = walk([{ abs: start, built: false }], { seen: new Set([start]), deps: [], unresolved: [] }, ctx);
|
|
717
|
+
return {
|
|
718
|
+
imports: new Set([...w.seen]
|
|
719
|
+
.filter((p) => p !== start)
|
|
720
|
+
.map((p) => posixRelative(ctx.cwd, p))),
|
|
721
|
+
effects: w.deps.map((d) => toEffectDep(d, ctx.cwd)),
|
|
722
|
+
unresolved: w.unresolved,
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
//# sourceMappingURL=test-reach.js.map
|