bivium 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +283 -45
- package/dist/cli.js +1854 -0
- package/{index.d.ts → dist/index.d.ts} +148 -151
- package/dist/index.js +8 -0
- package/dist/shared.js +4745 -0
- package/package.json +24 -20
- package/CHANGELOG.md +0 -315
- package/cli.js +0 -62
- package/index.js +0 -1
- package/shared-nbrvkwk9.js +0 -10
package/dist/shared.js
ADDED
|
@@ -0,0 +1,4745 @@
|
|
|
1
|
+
// src/compute.ts
|
|
2
|
+
import { readFile as readFile3, stat } from "node:fs/promises";
|
|
3
|
+
import { join as join4, posix as posix6, resolve as resolve2 } from "node:path";
|
|
4
|
+
|
|
5
|
+
// src/cache.ts
|
|
6
|
+
import { existsSync } from "node:fs";
|
|
7
|
+
import { mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
8
|
+
import { isAbsolute, join } from "node:path";
|
|
9
|
+
|
|
10
|
+
// package.json
|
|
11
|
+
var version = "0.5.0";
|
|
12
|
+
|
|
13
|
+
// src/cache.ts
|
|
14
|
+
var SCANNER_VERSION = 6;
|
|
15
|
+
var MAGIC = "bivium-extractions";
|
|
16
|
+
var FILE = "extractions.json";
|
|
17
|
+
function isHeader(value) {
|
|
18
|
+
return typeof value === "object" && value !== null && "magic" in value && value.magic === MAGIC && "bivium" in value && value.bivium === version && "scanner" in value && value.scanner === SCANNER_VERSION && "rows" in value && typeof value.rows === "number" && "strings" in value && typeof value.strings === "number";
|
|
19
|
+
}
|
|
20
|
+
function cacheDir(root, cache) {
|
|
21
|
+
if (cache === false)
|
|
22
|
+
return;
|
|
23
|
+
if (typeof cache === "string") {
|
|
24
|
+
return isAbsolute(cache) ? cache : join(root, cache);
|
|
25
|
+
}
|
|
26
|
+
const modules = join(root, "node_modules");
|
|
27
|
+
return existsSync(modules) ? join(modules, ".cache", "bivium") : join(root, ".bivium", "cache");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class ExtractionCache {
|
|
31
|
+
#rows = new Map;
|
|
32
|
+
#strings = [];
|
|
33
|
+
#used = new Map;
|
|
34
|
+
#dirty = false;
|
|
35
|
+
dir;
|
|
36
|
+
constructor(dir) {
|
|
37
|
+
this.dir = dir;
|
|
38
|
+
}
|
|
39
|
+
static async open(dir) {
|
|
40
|
+
const cache = new ExtractionCache(dir);
|
|
41
|
+
if (!dir)
|
|
42
|
+
return cache;
|
|
43
|
+
const text = await readFile(join(dir, FILE), "utf-8").catch(() => "");
|
|
44
|
+
if (text)
|
|
45
|
+
cache.#load(text);
|
|
46
|
+
return cache;
|
|
47
|
+
}
|
|
48
|
+
#load(text) {
|
|
49
|
+
try {
|
|
50
|
+
const parsed = JSON.parse(text);
|
|
51
|
+
if (!Array.isArray(parsed))
|
|
52
|
+
return;
|
|
53
|
+
const [header, strings, rows] = parsed;
|
|
54
|
+
if (!isHeader(header) || !Array.isArray(strings) || !Array.isArray(rows) || header.strings !== strings.length || header.rows !== rows.length) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
this.#strings = strings;
|
|
58
|
+
for (const row of rows) {
|
|
59
|
+
if (Array.isArray(row) && typeof row[0] === "string") {
|
|
60
|
+
this.#rows.set(row[0], row);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} catch {}
|
|
64
|
+
}
|
|
65
|
+
get size() {
|
|
66
|
+
return this.#rows.size;
|
|
67
|
+
}
|
|
68
|
+
get(oid, format) {
|
|
69
|
+
const key = `${format}:${oid}`;
|
|
70
|
+
const used = this.#used.get(key);
|
|
71
|
+
if (used)
|
|
72
|
+
return used;
|
|
73
|
+
const row = this.#rows.get(key);
|
|
74
|
+
if (!row)
|
|
75
|
+
return;
|
|
76
|
+
let extraction;
|
|
77
|
+
try {
|
|
78
|
+
extraction = decode(row, this.#strings);
|
|
79
|
+
} catch {
|
|
80
|
+
this.#rows.delete(key);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.#used.set(key, extraction);
|
|
84
|
+
return extraction;
|
|
85
|
+
}
|
|
86
|
+
set(oid, format, extraction) {
|
|
87
|
+
const key = `${format}:${oid}`;
|
|
88
|
+
this.#used.set(key, extraction);
|
|
89
|
+
this.#dirty = true;
|
|
90
|
+
}
|
|
91
|
+
async save() {
|
|
92
|
+
if (!this.dir || !this.#dirty)
|
|
93
|
+
return;
|
|
94
|
+
const table = new Strings;
|
|
95
|
+
const rows = [];
|
|
96
|
+
for (const [key, extraction] of this.#used) {
|
|
97
|
+
rows.push(encode(key, extraction, table));
|
|
98
|
+
}
|
|
99
|
+
let stale = Math.floor(this.#used.size / 2);
|
|
100
|
+
for (const [key, row] of this.#rows) {
|
|
101
|
+
if (stale === 0)
|
|
102
|
+
break;
|
|
103
|
+
if (this.#used.has(key))
|
|
104
|
+
continue;
|
|
105
|
+
try {
|
|
106
|
+
rows.push(encode(key, decode(row, this.#strings), table));
|
|
107
|
+
stale--;
|
|
108
|
+
} catch {}
|
|
109
|
+
}
|
|
110
|
+
const header = {
|
|
111
|
+
magic: MAGIC,
|
|
112
|
+
bivium: version,
|
|
113
|
+
scanner: SCANNER_VERSION,
|
|
114
|
+
rows: rows.length,
|
|
115
|
+
strings: table.list.length
|
|
116
|
+
};
|
|
117
|
+
const file = join(this.dir, FILE);
|
|
118
|
+
const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
119
|
+
try {
|
|
120
|
+
await mkdir(this.dir, { recursive: true });
|
|
121
|
+
const ignore = join(this.dir, ".gitignore");
|
|
122
|
+
if (!existsSync(ignore))
|
|
123
|
+
await writeFile(ignore, `*
|
|
124
|
+
`);
|
|
125
|
+
await writeFile(temp, JSON.stringify([header, table.list, rows]));
|
|
126
|
+
await rename(temp, file);
|
|
127
|
+
this.#dirty = false;
|
|
128
|
+
} catch {
|
|
129
|
+
await unlink(temp).catch(() => {
|
|
130
|
+
return;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
class Strings {
|
|
137
|
+
list = [];
|
|
138
|
+
#ids = new Map;
|
|
139
|
+
id(value) {
|
|
140
|
+
let id = this.#ids.get(value);
|
|
141
|
+
if (id === undefined) {
|
|
142
|
+
id = this.list.length;
|
|
143
|
+
this.list.push(value);
|
|
144
|
+
this.#ids.set(value, id);
|
|
145
|
+
}
|
|
146
|
+
return id;
|
|
147
|
+
}
|
|
148
|
+
ids(values) {
|
|
149
|
+
return values.length === 0 ? 0 : values.map((value) => this.id(value));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function typeIndexes(imports) {
|
|
153
|
+
const indexes = [];
|
|
154
|
+
for (const [i, ref] of imports.entries())
|
|
155
|
+
if (ref.type)
|
|
156
|
+
indexes.push(i);
|
|
157
|
+
return indexes.length === 0 ? 0 : indexes;
|
|
158
|
+
}
|
|
159
|
+
function encode(key, e, table) {
|
|
160
|
+
const imports = e.imports.map(({ specifier, names }) => names ? [table.id(specifier), ...names.map((n) => table.id(n))] : table.id(specifier));
|
|
161
|
+
const barrel = e.barrel && [
|
|
162
|
+
[...e.barrel.names].flatMap(([name, from]) => [
|
|
163
|
+
table.id(name),
|
|
164
|
+
table.id(from)
|
|
165
|
+
]),
|
|
166
|
+
table.ids(e.barrel.stars)
|
|
167
|
+
];
|
|
168
|
+
return [
|
|
169
|
+
key,
|
|
170
|
+
imports.length === 0 ? 0 : imports,
|
|
171
|
+
table.ids(e.globs),
|
|
172
|
+
table.ids(e.gotos),
|
|
173
|
+
table.ids(e.dependsOn),
|
|
174
|
+
table.ids(e.reads),
|
|
175
|
+
table.ids(e.unknown),
|
|
176
|
+
e.fallback === undefined ? 0 : table.id(e.fallback),
|
|
177
|
+
barrel ?? 0,
|
|
178
|
+
typeIndexes(e.imports),
|
|
179
|
+
table.ids(e.lists)
|
|
180
|
+
];
|
|
181
|
+
}
|
|
182
|
+
function decode(row, strings) {
|
|
183
|
+
const str = (id) => {
|
|
184
|
+
const value = typeof id === "number" ? strings[id] : undefined;
|
|
185
|
+
if (typeof value !== "string")
|
|
186
|
+
throw new Error("bad string id");
|
|
187
|
+
return value;
|
|
188
|
+
};
|
|
189
|
+
const list = (ids) => {
|
|
190
|
+
if (ids === 0)
|
|
191
|
+
return [];
|
|
192
|
+
if (!Array.isArray(ids))
|
|
193
|
+
throw new Error("bad list");
|
|
194
|
+
return ids.map(str);
|
|
195
|
+
};
|
|
196
|
+
const refs = row[1] === 0 ? [] : row[1];
|
|
197
|
+
if (!Array.isArray(refs))
|
|
198
|
+
throw new Error("bad imports");
|
|
199
|
+
const imports = refs.map((item) => Array.isArray(item) ? { specifier: str(item[0]), names: item.slice(1).map(str) } : { specifier: str(item) });
|
|
200
|
+
if (row[9] !== 0) {
|
|
201
|
+
if (!Array.isArray(row[9]))
|
|
202
|
+
throw new Error("bad types");
|
|
203
|
+
for (const i of row[9]) {
|
|
204
|
+
const ref = typeof i === "number" ? imports[i] : undefined;
|
|
205
|
+
if (!ref)
|
|
206
|
+
throw new Error("bad type index");
|
|
207
|
+
ref.type = true;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const extraction = {
|
|
211
|
+
imports,
|
|
212
|
+
globs: list(row[2]),
|
|
213
|
+
gotos: list(row[3]),
|
|
214
|
+
dependsOn: list(row[4]),
|
|
215
|
+
reads: list(row[5]),
|
|
216
|
+
lists: list(row[10]),
|
|
217
|
+
unknown: list(row[6])
|
|
218
|
+
};
|
|
219
|
+
if (row[7] !== 0)
|
|
220
|
+
extraction.fallback = str(row[7]);
|
|
221
|
+
if (row[8] !== 0) {
|
|
222
|
+
if (!Array.isArray(row[8]))
|
|
223
|
+
throw new Error("bad barrel");
|
|
224
|
+
const [pairs, stars] = row[8];
|
|
225
|
+
if (!Array.isArray(pairs))
|
|
226
|
+
throw new Error("bad barrel names");
|
|
227
|
+
const names = new Map;
|
|
228
|
+
for (let i = 0;i < pairs.length; i += 2) {
|
|
229
|
+
names.set(str(pairs[i]), str(pairs[i + 1]));
|
|
230
|
+
}
|
|
231
|
+
extraction.barrel = { names, stars: list(stars) };
|
|
232
|
+
}
|
|
233
|
+
return extraction;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// src/config.ts
|
|
237
|
+
import { existsSync as existsSync2, readFileSync } from "node:fs";
|
|
238
|
+
import { isAbsolute as isAbsolute2, join as join2, relative, resolve } from "node:path";
|
|
239
|
+
import { pathToFileURL } from "node:url";
|
|
240
|
+
|
|
241
|
+
// src/errors.ts
|
|
242
|
+
class BiviumError extends Error {
|
|
243
|
+
kind;
|
|
244
|
+
constructor(kind, message, options) {
|
|
245
|
+
super(message, options);
|
|
246
|
+
this.kind = kind;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
function distance(a, b) {
|
|
250
|
+
const width = b.length + 1;
|
|
251
|
+
const cells = new Uint32Array((a.length + 1) * width);
|
|
252
|
+
const d = (i, j) => cells[i * width + j] ?? 0;
|
|
253
|
+
for (let i = 0;i <= a.length; i++)
|
|
254
|
+
cells[i * width] = i;
|
|
255
|
+
for (let j = 0;j <= b.length; j++)
|
|
256
|
+
cells[j] = j;
|
|
257
|
+
for (let i = 1;i <= a.length; i++) {
|
|
258
|
+
for (let j = 1;j <= b.length; j++) {
|
|
259
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
260
|
+
let best = Math.min(d(i - 1, j) + 1, d(i, j - 1) + 1, d(i - 1, j - 1) + cost);
|
|
261
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
262
|
+
best = Math.min(best, d(i - 2, j - 2) + 1);
|
|
263
|
+
}
|
|
264
|
+
cells[i * width + j] = best;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return d(a.length, b.length);
|
|
268
|
+
}
|
|
269
|
+
function closest(input, candidates) {
|
|
270
|
+
const most = Math.max(1, Math.floor(input.length / 3));
|
|
271
|
+
let best;
|
|
272
|
+
let bestDistance = most + 1;
|
|
273
|
+
for (const candidate of candidates) {
|
|
274
|
+
const d = distance(input, candidate);
|
|
275
|
+
if (d < bestDistance) {
|
|
276
|
+
best = candidate;
|
|
277
|
+
bestDistance = d;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return best;
|
|
281
|
+
}
|
|
282
|
+
function didYouMean(input, candidates) {
|
|
283
|
+
const match = closest(input, candidates);
|
|
284
|
+
return match === undefined ? "" : ` (did you mean ${match}?)`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// src/glob.ts
|
|
288
|
+
var GLOB_LITERAL = /[.+^$()|[\]\\]/g;
|
|
289
|
+
var REGEX_SPECIAL = /[.*+?^${}()|[\]\\]/g;
|
|
290
|
+
var DIGIT = /[0-9]/;
|
|
291
|
+
var CAPTURE_REFERENCE = /\$(\d+)/g;
|
|
292
|
+
function compile(glob, {
|
|
293
|
+
capture = false,
|
|
294
|
+
captures
|
|
295
|
+
} = {}) {
|
|
296
|
+
let source = "";
|
|
297
|
+
let wildcards = 0;
|
|
298
|
+
const group = (body) => {
|
|
299
|
+
wildcards++;
|
|
300
|
+
return capture ? `(${body})` : body;
|
|
301
|
+
};
|
|
302
|
+
for (let i = 0;i < glob.length; i++) {
|
|
303
|
+
const char = glob[i];
|
|
304
|
+
if (captures && char === "$" && DIGIT.test(glob[i + 1] ?? "")) {
|
|
305
|
+
let end = i + 1;
|
|
306
|
+
while (DIGIT.test(glob[end] ?? ""))
|
|
307
|
+
end++;
|
|
308
|
+
const text = captures[Number(glob.slice(i + 1, end)) - 1];
|
|
309
|
+
source += text === undefined ? ".*" : text.replace(REGEX_SPECIAL, "\\$&");
|
|
310
|
+
i = end - 1;
|
|
311
|
+
} else if (char === "*" && glob[i + 1] === "*") {
|
|
312
|
+
const slash = glob[i + 2] === "/";
|
|
313
|
+
source += group(slash ? "(?:.*/)?" : ".*");
|
|
314
|
+
i += slash ? 2 : 1;
|
|
315
|
+
} else if (char === "*")
|
|
316
|
+
source += group("[^/]*");
|
|
317
|
+
else if (char === "?")
|
|
318
|
+
source += group("[^/]");
|
|
319
|
+
else if (char === "{") {
|
|
320
|
+
wildcards++;
|
|
321
|
+
source += capture ? "(" : "(?:";
|
|
322
|
+
} else if (char === "}")
|
|
323
|
+
source += ")";
|
|
324
|
+
else if (char === ",")
|
|
325
|
+
source += "|";
|
|
326
|
+
else
|
|
327
|
+
source += char.replace(GLOB_LITERAL, "\\$&");
|
|
328
|
+
}
|
|
329
|
+
return { source: `^${source}$`, wildcards };
|
|
330
|
+
}
|
|
331
|
+
function globToRegExp(glob) {
|
|
332
|
+
return new RegExp(compile(glob).source);
|
|
333
|
+
}
|
|
334
|
+
function captureGlob(glob) {
|
|
335
|
+
const { source, wildcards } = compile(glob, { capture: true });
|
|
336
|
+
return { pattern: new RegExp(source), wildcards };
|
|
337
|
+
}
|
|
338
|
+
function captureReferences(glob) {
|
|
339
|
+
return [...glob.matchAll(CAPTURE_REFERENCE)].map((match) => Number(match[1]));
|
|
340
|
+
}
|
|
341
|
+
function expandGlob(glob, captures) {
|
|
342
|
+
return new RegExp(compile(glob, { captures }).source);
|
|
343
|
+
}
|
|
344
|
+
function matcher(globs = []) {
|
|
345
|
+
const list = typeof globs === "string" ? [globs] : globs;
|
|
346
|
+
if (list.length === 0)
|
|
347
|
+
return () => false;
|
|
348
|
+
const res = list.map(globToRegExp);
|
|
349
|
+
return (path) => res.some((re) => re.test(path));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// src/config.ts
|
|
353
|
+
function defineConfig2(config) {
|
|
354
|
+
return config;
|
|
355
|
+
}
|
|
356
|
+
var DEFAULT_EXTENSIONS = [
|
|
357
|
+
".ts",
|
|
358
|
+
".tsx",
|
|
359
|
+
".mts",
|
|
360
|
+
".cts",
|
|
361
|
+
".js",
|
|
362
|
+
".jsx",
|
|
363
|
+
".mjs",
|
|
364
|
+
".cjs",
|
|
365
|
+
".d.ts",
|
|
366
|
+
".svelte",
|
|
367
|
+
".vue",
|
|
368
|
+
".json"
|
|
369
|
+
];
|
|
370
|
+
function list(value, fallback) {
|
|
371
|
+
if (value === undefined)
|
|
372
|
+
return [...fallback];
|
|
373
|
+
return typeof value === "string" ? [value] : [...value];
|
|
374
|
+
}
|
|
375
|
+
var LEADING_DOT_SLASH = /^\.\//;
|
|
376
|
+
var TRAILING_SLASHES = /\/+$/;
|
|
377
|
+
function normalizeRoot(root) {
|
|
378
|
+
return root.replace(LEADING_DOT_SLASH, "").replace(TRAILING_SLASHES, "") || ".";
|
|
379
|
+
}
|
|
380
|
+
function pathMaps(record = {}) {
|
|
381
|
+
return Object.entries(record).map(([key, globs]) => {
|
|
382
|
+
const { pattern, wildcards } = captureGlob(key);
|
|
383
|
+
const values = list(globs, []);
|
|
384
|
+
const fixed = values.map((value) => captureReferences(value).length === 0 ? globToRegExp(value) : undefined);
|
|
385
|
+
return {
|
|
386
|
+
key,
|
|
387
|
+
exact: wildcards === 0,
|
|
388
|
+
pattern,
|
|
389
|
+
values,
|
|
390
|
+
targets: (path) => {
|
|
391
|
+
const match = pattern.exec(path);
|
|
392
|
+
if (!match)
|
|
393
|
+
return;
|
|
394
|
+
const captures = match.slice(1);
|
|
395
|
+
return values.map((value, i) => fixed[i] ?? expandGlob(value, captures));
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
function isInertOf(inert) {
|
|
401
|
+
const listed = matcher(inert);
|
|
402
|
+
return (name) => name === "bivium" || listed(name);
|
|
403
|
+
}
|
|
404
|
+
function resolveConfig(config) {
|
|
405
|
+
const names = Object.keys(config.suites);
|
|
406
|
+
if (names.length === 0)
|
|
407
|
+
throw new Error("bivium config: no suites");
|
|
408
|
+
const suites = Object.entries(config.suites).map(([name, suite]) => ({
|
|
409
|
+
name,
|
|
410
|
+
isTest: matcher(suite.tests),
|
|
411
|
+
run: suite.run,
|
|
412
|
+
roots: list(suite.root, ["."]).map(normalizeRoot),
|
|
413
|
+
urls: Object.entries(suite.urls ?? {}).map(([key, targets]) => ({
|
|
414
|
+
key,
|
|
415
|
+
targets: list(targets, [])
|
|
416
|
+
})),
|
|
417
|
+
isPage: matcher(suite.pages),
|
|
418
|
+
isWiring: matcher(suite.wiring),
|
|
419
|
+
isSetup: matcher(suite.setup),
|
|
420
|
+
setup: list(suite.setup, []),
|
|
421
|
+
isNonInput: matcher(suite.nonInputs),
|
|
422
|
+
output: suite.output ?? `${name}-tests`,
|
|
423
|
+
types: suite.types ?? true
|
|
424
|
+
}));
|
|
425
|
+
const narrowers = (config.narrowers ?? []).map((narrower) => {
|
|
426
|
+
const unknown = narrower.suites?.find((name) => !names.includes(name));
|
|
427
|
+
if (unknown !== undefined) {
|
|
428
|
+
throw new Error(`bivium config: narrower ${narrower.name} names no suite ${unknown}`);
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
name: narrower.name,
|
|
432
|
+
match: matcher(narrower.match),
|
|
433
|
+
opaque: matcher(narrower.opaque),
|
|
434
|
+
suites: [...narrower.suites ?? names],
|
|
435
|
+
narrow: async (context) => checkResult(await narrower.narrow(context))
|
|
436
|
+
};
|
|
437
|
+
});
|
|
438
|
+
return {
|
|
439
|
+
suites,
|
|
440
|
+
narrowers,
|
|
441
|
+
roots: list(config.roots, ["."]).map(normalizeRoot),
|
|
442
|
+
isIgnored: matcher(config.ignore),
|
|
443
|
+
alias: Object.entries(config.alias ?? {}).map(([key, path]) => [
|
|
444
|
+
key.replace(TRAILING_SLASHES, "") || key,
|
|
445
|
+
normalizeRoot(path)
|
|
446
|
+
]).sort(([a], [b]) => b.length - a.length),
|
|
447
|
+
generated: pathMaps(config.edges),
|
|
448
|
+
reads: pathMaps(config.reads),
|
|
449
|
+
isSelf: matcher(config.self),
|
|
450
|
+
isImportOnly: matcher(config.importOnly),
|
|
451
|
+
isHermetic: matcher(config.hermetic),
|
|
452
|
+
isBarrel: config.barrels ? matcher(config.barrels) : () => true,
|
|
453
|
+
external: new Set(config.external),
|
|
454
|
+
isInert: isInertOf(config.inert),
|
|
455
|
+
...config.loadedByName !== undefined && {
|
|
456
|
+
isLoadedByName: matcher(config.loadedByName)
|
|
457
|
+
},
|
|
458
|
+
extensions: config.extensions ?? DEFAULT_EXTENSIONS,
|
|
459
|
+
deadGlobs: config.deadGlobs ?? [],
|
|
460
|
+
...config.knownUnknowns && { knownUnknowns: config.knownUnknowns },
|
|
461
|
+
base: config.base,
|
|
462
|
+
cache: config.cache
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
function inRoot(file, root) {
|
|
466
|
+
return root === "." || file === root || file.startsWith(`${root}/`);
|
|
467
|
+
}
|
|
468
|
+
function isRecord(value) {
|
|
469
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
470
|
+
}
|
|
471
|
+
function isStrings(value) {
|
|
472
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
473
|
+
}
|
|
474
|
+
function isGlobs(value) {
|
|
475
|
+
return typeof value === "string" || isStrings(value);
|
|
476
|
+
}
|
|
477
|
+
function isStringRecord(value) {
|
|
478
|
+
return isRecord(value) && Object.values(value).every((item) => typeof item === "string");
|
|
479
|
+
}
|
|
480
|
+
function isGlobsRecord(value) {
|
|
481
|
+
return isRecord(value) && Object.values(value).every(isGlobs);
|
|
482
|
+
}
|
|
483
|
+
function isBoolean(value) {
|
|
484
|
+
return typeof value === "boolean";
|
|
485
|
+
}
|
|
486
|
+
function isString(value) {
|
|
487
|
+
return typeof value === "string";
|
|
488
|
+
}
|
|
489
|
+
function isCache(value) {
|
|
490
|
+
return typeof value === "boolean" || typeof value === "string";
|
|
491
|
+
}
|
|
492
|
+
function field(record, key, is, path, expected) {
|
|
493
|
+
const value = record[key];
|
|
494
|
+
if (value === undefined || is(value))
|
|
495
|
+
return value;
|
|
496
|
+
throw new Error(`bivium config: ${path}${key} must be ${expected}`);
|
|
497
|
+
}
|
|
498
|
+
var SUITE_KEYS = new Set([
|
|
499
|
+
"tests",
|
|
500
|
+
"run",
|
|
501
|
+
"root",
|
|
502
|
+
"urls",
|
|
503
|
+
"pages",
|
|
504
|
+
"wiring",
|
|
505
|
+
"setup",
|
|
506
|
+
"nonInputs",
|
|
507
|
+
"output",
|
|
508
|
+
"types"
|
|
509
|
+
]);
|
|
510
|
+
var NARROWER_KEYS = new Set(["name", "match", "opaque", "suites", "narrow"]);
|
|
511
|
+
var DEAD_GLOB_KEYS = new Set(["file", "glob", "reason"]);
|
|
512
|
+
var KNOWN_UNKNOWN_KEYS = new Set(["file", "reason"]);
|
|
513
|
+
var CONFIG_KEYS = new Set([
|
|
514
|
+
"suites",
|
|
515
|
+
"narrowers",
|
|
516
|
+
"roots",
|
|
517
|
+
"ignore",
|
|
518
|
+
"alias",
|
|
519
|
+
"edges",
|
|
520
|
+
"reads",
|
|
521
|
+
"self",
|
|
522
|
+
"importOnly",
|
|
523
|
+
"hermetic",
|
|
524
|
+
"barrels",
|
|
525
|
+
"external",
|
|
526
|
+
"inert",
|
|
527
|
+
"loadedByName",
|
|
528
|
+
"extensions",
|
|
529
|
+
"deadGlobs",
|
|
530
|
+
"knownUnknowns",
|
|
531
|
+
"base",
|
|
532
|
+
"cache"
|
|
533
|
+
]);
|
|
534
|
+
function unknownKeys(value) {
|
|
535
|
+
const found = [];
|
|
536
|
+
const visit = (record, known, path) => {
|
|
537
|
+
if (!isRecord(record))
|
|
538
|
+
return;
|
|
539
|
+
for (const key of Object.keys(record)) {
|
|
540
|
+
if (!known.has(key))
|
|
541
|
+
found.push(`${path}${key}${didYouMean(key, known)}`);
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
visit(value, CONFIG_KEYS, "");
|
|
545
|
+
if (isRecord(value.suites)) {
|
|
546
|
+
for (const [name, suite] of Object.entries(value.suites)) {
|
|
547
|
+
visit(suite, SUITE_KEYS, `suites.${name}.`);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
const lists = [
|
|
551
|
+
["narrowers", NARROWER_KEYS],
|
|
552
|
+
["deadGlobs", DEAD_GLOB_KEYS],
|
|
553
|
+
["knownUnknowns", KNOWN_UNKNOWN_KEYS]
|
|
554
|
+
];
|
|
555
|
+
for (const [key, known] of lists) {
|
|
556
|
+
const entries = value[key];
|
|
557
|
+
if (!Array.isArray(entries))
|
|
558
|
+
continue;
|
|
559
|
+
for (const [i, entry] of entries.entries()) {
|
|
560
|
+
visit(entry, known, `${key}[${i}].`);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return found;
|
|
564
|
+
}
|
|
565
|
+
function checkSuite(name, value) {
|
|
566
|
+
if (!isRecord(value)) {
|
|
567
|
+
throw new Error(`bivium config: suites.${name} must be an object`);
|
|
568
|
+
}
|
|
569
|
+
const at = `suites.${name}.`;
|
|
570
|
+
const tests = field(value, "tests", isGlobs, at, "a glob or a list of globs");
|
|
571
|
+
if (tests === undefined || tests.length === 0) {
|
|
572
|
+
throw new Error(`bivium config: suite ${name} has no tests`);
|
|
573
|
+
}
|
|
574
|
+
const globs = "a glob or a list of globs";
|
|
575
|
+
return {
|
|
576
|
+
tests,
|
|
577
|
+
run: field(value, "run", isString, at, "a command"),
|
|
578
|
+
root: paths(value, "root", at),
|
|
579
|
+
urls: field(value, "urls", isGlobsRecord, at, "a map of URL patterns"),
|
|
580
|
+
pages: field(value, "pages", isGlobs, at, globs),
|
|
581
|
+
wiring: field(value, "wiring", isGlobs, at, globs),
|
|
582
|
+
setup: field(value, "setup", isGlobs, at, globs),
|
|
583
|
+
nonInputs: field(value, "nonInputs", isGlobs, at, globs),
|
|
584
|
+
output: field(value, "output", isString, at, "an output name"),
|
|
585
|
+
types: field(value, "types", isBoolean, at, "a boolean")
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
var WILDCARD = /[*?{}[\]]/;
|
|
589
|
+
function paths(record, key, at) {
|
|
590
|
+
const value = field(record, key, isGlobs, at, "a path or a list of paths");
|
|
591
|
+
for (const path of typeof value === "string" ? [value] : value ?? []) {
|
|
592
|
+
const wildcard = WILDCARD.exec(path);
|
|
593
|
+
if (!wildcard)
|
|
594
|
+
continue;
|
|
595
|
+
const prefix = normalizeRoot(path.slice(0, wildcard.index));
|
|
596
|
+
throw new Error(`bivium config: ${at}${key} takes paths, not globs: "${path}" (the directory is "${prefix}")`);
|
|
597
|
+
}
|
|
598
|
+
return value;
|
|
599
|
+
}
|
|
600
|
+
function isResult(value) {
|
|
601
|
+
if (!isRecord(value))
|
|
602
|
+
return false;
|
|
603
|
+
if ("all" in value)
|
|
604
|
+
return typeof value.all === "string";
|
|
605
|
+
return "seeds" in value && isStrings(value.seeds) && (!("note" in value) || typeof value.note === "string");
|
|
606
|
+
}
|
|
607
|
+
function checkResult(value) {
|
|
608
|
+
if (isResult(value))
|
|
609
|
+
return value;
|
|
610
|
+
throw new Error("returned neither { seeds: string[] } nor { all: string }");
|
|
611
|
+
}
|
|
612
|
+
function checkNarrower(value, at) {
|
|
613
|
+
if (!isRecord(value)) {
|
|
614
|
+
throw new Error(`bivium config: ${at} must be an object`);
|
|
615
|
+
}
|
|
616
|
+
const name = field(value, "name", isString, `${at}.`, "a string");
|
|
617
|
+
const match = field(value, "match", isGlobs, `${at}.`, "globs");
|
|
618
|
+
const opaque = field(value, "opaque", isGlobs, `${at}.`, "globs");
|
|
619
|
+
const suites = field(value, "suites", isStrings, `${at}.`, "suite names");
|
|
620
|
+
const { narrow } = value;
|
|
621
|
+
if (name === undefined || match === undefined || opaque === undefined) {
|
|
622
|
+
throw new Error(`bivium config: ${at} needs a name, match and opaque`);
|
|
623
|
+
}
|
|
624
|
+
if (typeof narrow !== "function") {
|
|
625
|
+
throw new Error(`bivium config: ${at}.narrow must be a function`);
|
|
626
|
+
}
|
|
627
|
+
return {
|
|
628
|
+
name,
|
|
629
|
+
match,
|
|
630
|
+
opaque,
|
|
631
|
+
suites,
|
|
632
|
+
narrow: async (context) => {
|
|
633
|
+
const result = await Reflect.apply(narrow, value, [context]);
|
|
634
|
+
return checkResult(result);
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
function checkDeadGlob(value, at) {
|
|
639
|
+
if (!isRecord(value)) {
|
|
640
|
+
throw new Error(`bivium config: ${at} must be an object`);
|
|
641
|
+
}
|
|
642
|
+
const file = field(value, "file", isString, `${at}.`, "a path");
|
|
643
|
+
const glob = field(value, "glob", isString, `${at}.`, "a glob");
|
|
644
|
+
const reason = field(value, "reason", isString, `${at}.`, "a string");
|
|
645
|
+
if (!(file && glob && reason)) {
|
|
646
|
+
throw new Error(`bivium config: ${at} needs a file, glob and reason`);
|
|
647
|
+
}
|
|
648
|
+
return { file, glob, reason };
|
|
649
|
+
}
|
|
650
|
+
function checkKnownUnknown(value, at) {
|
|
651
|
+
if (!isRecord(value)) {
|
|
652
|
+
throw new Error(`bivium config: ${at} must be an object`);
|
|
653
|
+
}
|
|
654
|
+
const file = field(value, "file", isString, `${at}.`, "a path");
|
|
655
|
+
const reason = field(value, "reason", isString, `${at}.`, "a string");
|
|
656
|
+
if (!(file && reason)) {
|
|
657
|
+
throw new Error(`bivium config: ${at} needs a file and reason`);
|
|
658
|
+
}
|
|
659
|
+
return { file, reason };
|
|
660
|
+
}
|
|
661
|
+
function checkCaptures(name, record) {
|
|
662
|
+
for (const [key, globs] of Object.entries(record ?? {})) {
|
|
663
|
+
const { wildcards } = captureGlob(key);
|
|
664
|
+
for (const value of list(globs, [])) {
|
|
665
|
+
const bad = captureReferences(value).find((n) => n < 1 || n > wildcards);
|
|
666
|
+
if (bad === undefined)
|
|
667
|
+
continue;
|
|
668
|
+
const has = `${wildcards} ${wildcards === 1 ? "wildcard" : "wildcards"}`;
|
|
669
|
+
const why = bad < 1 ? "captures count from $1" : `its key has ${has}`;
|
|
670
|
+
throw new Error(`bivium config: ${name}["${key}"] refers to $${bad} in ${value}, but ${why}`);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
function checkConfig(value) {
|
|
675
|
+
if (!isRecord(value))
|
|
676
|
+
throw new Error("bivium config: not an object");
|
|
677
|
+
if (!isRecord(value.suites)) {
|
|
678
|
+
throw new Error("bivium config: suites must be an object");
|
|
679
|
+
}
|
|
680
|
+
const unknown = unknownKeys(value);
|
|
681
|
+
if (unknown.length === 1) {
|
|
682
|
+
throw new Error(`bivium config: unknown key ${unknown[0]}`);
|
|
683
|
+
}
|
|
684
|
+
if (unknown.length > 1) {
|
|
685
|
+
throw new Error(`bivium config: unknown keys:
|
|
686
|
+
${unknown.map((key) => ` ${key}`).join(`
|
|
687
|
+
`)}`);
|
|
688
|
+
}
|
|
689
|
+
const { narrowers, deadGlobs, knownUnknowns } = value;
|
|
690
|
+
if (narrowers !== undefined && !Array.isArray(narrowers)) {
|
|
691
|
+
throw new Error("bivium config: narrowers must be a list");
|
|
692
|
+
}
|
|
693
|
+
if (deadGlobs !== undefined && !Array.isArray(deadGlobs)) {
|
|
694
|
+
throw new Error("bivium config: deadGlobs must be a list");
|
|
695
|
+
}
|
|
696
|
+
if (knownUnknowns !== undefined && !Array.isArray(knownUnknowns)) {
|
|
697
|
+
throw new Error("bivium config: knownUnknowns must be a list");
|
|
698
|
+
}
|
|
699
|
+
const suites = {};
|
|
700
|
+
for (const [name, suite] of Object.entries(value.suites)) {
|
|
701
|
+
suites[name] = checkSuite(name, suite);
|
|
702
|
+
}
|
|
703
|
+
const globs = "a glob or a list of globs";
|
|
704
|
+
const edges = field(value, "edges", isGlobsRecord, "", "a map of globs");
|
|
705
|
+
const reads = field(value, "reads", isGlobsRecord, "", "a map of globs");
|
|
706
|
+
checkCaptures("edges", edges);
|
|
707
|
+
checkCaptures("reads", reads);
|
|
708
|
+
return {
|
|
709
|
+
suites,
|
|
710
|
+
narrowers: narrowers?.map((narrower, i) => checkNarrower(narrower, `narrowers[${i}]`)),
|
|
711
|
+
roots: paths(value, "roots", ""),
|
|
712
|
+
ignore: field(value, "ignore", isGlobs, "", globs),
|
|
713
|
+
alias: field(value, "alias", isStringRecord, "", "a map of paths"),
|
|
714
|
+
edges,
|
|
715
|
+
reads,
|
|
716
|
+
self: field(value, "self", isGlobs, "", globs),
|
|
717
|
+
importOnly: field(value, "importOnly", isGlobs, "", globs),
|
|
718
|
+
hermetic: field(value, "hermetic", isGlobs, "", globs),
|
|
719
|
+
barrels: field(value, "barrels", isGlobs, "", globs),
|
|
720
|
+
external: field(value, "external", isStrings, "", "a list of packages"),
|
|
721
|
+
inert: field(value, "inert", isGlobs, "", globs),
|
|
722
|
+
loadedByName: field(value, "loadedByName", isGlobs, "", globs),
|
|
723
|
+
extensions: field(value, "extensions", isStrings, "", "a list"),
|
|
724
|
+
deadGlobs: deadGlobs?.map((entry, i) => checkDeadGlob(entry, `deadGlobs[${i}]`)),
|
|
725
|
+
knownUnknowns: knownUnknowns?.map((entry, i) => checkKnownUnknown(entry, `knownUnknowns[${i}]`)),
|
|
726
|
+
base: field(value, "base", isString, "", "a ref"),
|
|
727
|
+
cache: field(value, "cache", isCache, "", "a directory or a boolean")
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
var CONFIG_FILES = [
|
|
731
|
+
"bivium.config.ts",
|
|
732
|
+
"bivium.config.mts",
|
|
733
|
+
"bivium.config.mjs",
|
|
734
|
+
"bivium.config.js",
|
|
735
|
+
"bivium.config.json"
|
|
736
|
+
];
|
|
737
|
+
async function readConfig(file) {
|
|
738
|
+
if (file.endsWith(".json"))
|
|
739
|
+
return JSON.parse(readFileSync(file, "utf-8"));
|
|
740
|
+
const module = await import(pathToFileURL(file).href);
|
|
741
|
+
return isRecord(module) ? module.default : undefined;
|
|
742
|
+
}
|
|
743
|
+
var CONFIG_PREFIX = /^bivium config: /;
|
|
744
|
+
async function loadConfig(root, path) {
|
|
745
|
+
const file = path === undefined ? CONFIG_FILES.map((name) => join2(root, name)).find((f) => existsSync2(f)) : resolve(process.cwd(), path);
|
|
746
|
+
if (!file || !existsSync2(file)) {
|
|
747
|
+
throw new BiviumError("config", path ? `bivium: no config found at ${path}` : `bivium: no config found in ${root}: looked for ${CONFIG_FILES.join(", ")} (--config names another file, --cwd another repository)`);
|
|
748
|
+
}
|
|
749
|
+
const inside = relative(root, file);
|
|
750
|
+
const shown = inside.startsWith("..") || isAbsolute2(inside) ? file : inside;
|
|
751
|
+
let config;
|
|
752
|
+
try {
|
|
753
|
+
config = resolveConfig(checkConfig(await readConfig(file)));
|
|
754
|
+
} catch (error) {
|
|
755
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
756
|
+
throw new BiviumError("config", `bivium config: ${shown}: ${message.replace(CONFIG_PREFIX, "")}`, { cause: error });
|
|
757
|
+
}
|
|
758
|
+
return { config, file: shown };
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// src/git.ts
|
|
762
|
+
import { spawn } from "node:child_process";
|
|
763
|
+
import { readFile as readFile2 } from "node:fs/promises";
|
|
764
|
+
import { join as join3 } from "node:path";
|
|
765
|
+
|
|
766
|
+
// src/sorted.ts
|
|
767
|
+
function byCodeUnit(a, b) {
|
|
768
|
+
if (a === b)
|
|
769
|
+
return 0;
|
|
770
|
+
return a < b ? -1 : 1;
|
|
771
|
+
}
|
|
772
|
+
function sorted(values) {
|
|
773
|
+
return [...values].sort(byCodeUnit);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
// src/git.ts
|
|
777
|
+
function git(cwd, ...args) {
|
|
778
|
+
return new Promise((resolve, reject) => {
|
|
779
|
+
const child = spawn("git", args, { cwd });
|
|
780
|
+
const chunks = [];
|
|
781
|
+
const errors = [];
|
|
782
|
+
child.stdout.on("data", (chunk) => chunks.push(chunk));
|
|
783
|
+
child.stderr.on("data", (chunk) => errors.push(chunk));
|
|
784
|
+
child.on("error", reject);
|
|
785
|
+
child.on("close", (status) => {
|
|
786
|
+
resolve({
|
|
787
|
+
status: status ?? 1,
|
|
788
|
+
stdout: Buffer.concat(chunks).toString("utf-8"),
|
|
789
|
+
stderr: Buffer.concat(errors).toString("utf-8")
|
|
790
|
+
});
|
|
791
|
+
});
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
async function gitOrThrow(cwd, ...args) {
|
|
795
|
+
const result = await git(cwd, ...args);
|
|
796
|
+
if (result.status !== 0) {
|
|
797
|
+
const said = result.stderr.trim().split(`
|
|
798
|
+
`)[0];
|
|
799
|
+
throw new BiviumError("git", `bivium: git ${args.join(" ")} failed${said ? `: ${said}` : ""}`);
|
|
800
|
+
}
|
|
801
|
+
return result.stdout;
|
|
802
|
+
}
|
|
803
|
+
function lines(output) {
|
|
804
|
+
return output.split(`
|
|
805
|
+
`).filter(Boolean);
|
|
806
|
+
}
|
|
807
|
+
var INDEX_ENTRY = /^(\S) (\d{6}) ([0-9a-f]+) (\d)\t(.*)$/;
|
|
808
|
+
var RAW_ENTRY = /^:(\d{6}) \d{6} ([0-9a-f]+) [0-9a-f]+ ([A-Z])\d*\t(.*)$/;
|
|
809
|
+
async function missingHistory(cwd, base) {
|
|
810
|
+
const [known, forked, shallow] = await Promise.all([
|
|
811
|
+
git(cwd, "rev-parse", "--verify", "--quiet", `${base}^{commit}`),
|
|
812
|
+
git(cwd, "merge-base", base, "HEAD"),
|
|
813
|
+
git(cwd, "rev-parse", "--is-shallow-repository")
|
|
814
|
+
]);
|
|
815
|
+
const fix = shallow.stdout.trim() === "true" ? " The clone is shallow: fetch the history (actions/checkout: `fetch-depth: 0`)." : "";
|
|
816
|
+
if (known.status !== 0) {
|
|
817
|
+
return new BiviumError("git", `bivium: no commit ${base} here to diff against: fetch it.${fix}`);
|
|
818
|
+
}
|
|
819
|
+
if (forked.status !== 0) {
|
|
820
|
+
return new BiviumError("git", `bivium: ${base} and HEAD share no commit here, so there's no merge base to diff against.${fix}`);
|
|
821
|
+
}
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
function mergeBaseOf(cwd, base) {
|
|
825
|
+
let pending;
|
|
826
|
+
return () => {
|
|
827
|
+
if (pending === undefined) {
|
|
828
|
+
pending = gitOrThrow(cwd, "merge-base", base, "HEAD").then((out) => out.trim());
|
|
829
|
+
pending.catch(() => {
|
|
830
|
+
return;
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
return pending;
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
async function readRevision(cwd, base, roots, {
|
|
837
|
+
blobs = false,
|
|
838
|
+
mergeBase: eager = false
|
|
839
|
+
} = {}) {
|
|
840
|
+
const mergeBase = mergeBaseOf(cwd, base);
|
|
841
|
+
if (eager)
|
|
842
|
+
mergeBase();
|
|
843
|
+
const diffArgs = ["--no-renames", "--merge-base", base];
|
|
844
|
+
const [listing, diff, staged] = await Promise.all([
|
|
845
|
+
gitOrThrow(cwd, "ls-files", "-s", "-v", "-o", "--exclude-standard"),
|
|
846
|
+
gitOrThrow(cwd, "diff", "--raw", "--no-abbrev", ...diffArgs),
|
|
847
|
+
gitOrThrow(cwd, "diff", "--cached", "--name-only", ...diffArgs)
|
|
848
|
+
]).catch(async (error) => {
|
|
849
|
+
throw await missingHistory(cwd, base) ?? error;
|
|
850
|
+
});
|
|
851
|
+
const entries = lines(listing);
|
|
852
|
+
const untracked = [];
|
|
853
|
+
for (const line of entries) {
|
|
854
|
+
if (line.startsWith("? "))
|
|
855
|
+
untracked.push(line.slice(2));
|
|
856
|
+
}
|
|
857
|
+
const before = new Map;
|
|
858
|
+
const diffed = [];
|
|
859
|
+
const layout = new Set(untracked);
|
|
860
|
+
for (const line of lines(diff)) {
|
|
861
|
+
const entry = RAW_ENTRY.exec(line);
|
|
862
|
+
if (!entry)
|
|
863
|
+
throw new Error(`git diff: unexpected line ${line}`);
|
|
864
|
+
const [, mode, oid, status, path] = entry;
|
|
865
|
+
diffed.push(path);
|
|
866
|
+
if (status === "A" || status === "D")
|
|
867
|
+
layout.add(path);
|
|
868
|
+
if (status === "A")
|
|
869
|
+
before.set(path, undefined);
|
|
870
|
+
else if ("MTD".includes(status) && mode !== "160000") {
|
|
871
|
+
before.set(path, oid);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
const reads = new Map;
|
|
875
|
+
const readBase = (path) => {
|
|
876
|
+
let read = reads.get(path);
|
|
877
|
+
if (read === undefined) {
|
|
878
|
+
read = readBaseOnce(path);
|
|
879
|
+
reads.set(path, read);
|
|
880
|
+
}
|
|
881
|
+
return read;
|
|
882
|
+
};
|
|
883
|
+
const readBaseOnce = (path) => {
|
|
884
|
+
if (!before.has(path)) {
|
|
885
|
+
if (present.has(path)) {
|
|
886
|
+
return readFile2(join3(cwd, path), "utf-8").catch(() => mergeBase().then((commit) => show(cwd, `${commit}:${path}`)));
|
|
887
|
+
}
|
|
888
|
+
return mergeBase().then((commit) => show(cwd, `${commit}:${path}`));
|
|
889
|
+
}
|
|
890
|
+
const oid = before.get(path);
|
|
891
|
+
return oid === undefined ? Promise.resolve(undefined) : show(cwd, oid);
|
|
892
|
+
};
|
|
893
|
+
const changed = [...new Set([...diffed, ...untracked])];
|
|
894
|
+
const dirty = new Set([...changed, ...lines(staged)]);
|
|
895
|
+
const underRoots = (path) => roots.some((root) => inRoot(path, root));
|
|
896
|
+
const files = new Set;
|
|
897
|
+
const tracked = new Set;
|
|
898
|
+
const present = new Set;
|
|
899
|
+
const oids = new Map;
|
|
900
|
+
for (const line of entries) {
|
|
901
|
+
const entry = INDEX_ENTRY.exec(line);
|
|
902
|
+
if (!entry) {
|
|
903
|
+
if (line.startsWith("? ") && underRoots(line.slice(2))) {
|
|
904
|
+
files.add(line.slice(2));
|
|
905
|
+
}
|
|
906
|
+
continue;
|
|
907
|
+
}
|
|
908
|
+
const [, tag, mode, oid, stage, path] = entry;
|
|
909
|
+
tracked.add(path);
|
|
910
|
+
if (mode === "160000" || !underRoots(path))
|
|
911
|
+
continue;
|
|
912
|
+
files.add(path);
|
|
913
|
+
if (dirty.has(path) || stage !== "0" || tag !== "H")
|
|
914
|
+
continue;
|
|
915
|
+
if (mode !== "100644" && mode !== "100755")
|
|
916
|
+
continue;
|
|
917
|
+
present.add(path);
|
|
918
|
+
if (blobs)
|
|
919
|
+
oids.set(path, oid);
|
|
920
|
+
}
|
|
921
|
+
return {
|
|
922
|
+
files: sorted(files),
|
|
923
|
+
listed: [...tracked, ...untracked],
|
|
924
|
+
changed,
|
|
925
|
+
layout,
|
|
926
|
+
mergeBase,
|
|
927
|
+
readBase,
|
|
928
|
+
blobs: oids,
|
|
929
|
+
present
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
async function show(cwd, object) {
|
|
933
|
+
const result = await git(cwd, "show", object);
|
|
934
|
+
return result.status === 0 ? result.stdout : undefined;
|
|
935
|
+
}
|
|
936
|
+
async function defaultBase(cwd) {
|
|
937
|
+
const head = await git(cwd, "symbolic-ref", "--short", "refs/remotes/origin/HEAD");
|
|
938
|
+
if (head.status === 0 && head.stdout.trim())
|
|
939
|
+
return head.stdout.trim();
|
|
940
|
+
const refs = ["origin/main", "origin/master", "main", "master"];
|
|
941
|
+
const found = await Promise.all(refs.map((ref) => git(cwd, "rev-parse", "--verify", "--quiet", ref)));
|
|
942
|
+
const branch = refs.find((_, i) => found[i].status === 0);
|
|
943
|
+
if (branch)
|
|
944
|
+
return branch;
|
|
945
|
+
throw new BiviumError("git", `bivium: no base given and no default branch found (tried origin/HEAD, ${refs.join(", ")}): pass one, as in \`bivium plan origin/main\`, or set base in the config`);
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// src/barrel.ts
|
|
949
|
+
var COMMENTS = /\/\/.*$|\/\*[\s\S]*?\*\//gm;
|
|
950
|
+
var AS = /\s+as\s+/;
|
|
951
|
+
var TYPE_PREFIX = /^type\s+/;
|
|
952
|
+
var NAMED_EXPORT = /export\s+(?:type\s+)?\{([^}]*)\}\s*from\s*["']([^"']+)["']\s*;?/g;
|
|
953
|
+
var STAR_EXPORT = /export\s+(?:type\s+)?\*(?:\s+as\s+([\w$]+))?\s+from\s*["']([^"']+)["']\s*;?/g;
|
|
954
|
+
var BARREL_FILE = /\.[cm]?[jt]sx?$/;
|
|
955
|
+
var ONLY_SEPARATORS = /^[\s;]*$/;
|
|
956
|
+
var STARTS_WITH_EXPORT = /^(?:\s+|\/\/[^\n]*|\/\*[\s\S]*?\*\/)*export\b/;
|
|
957
|
+
function parseBarrel(path, source) {
|
|
958
|
+
return BARREL_FILE.test(path) ? parseReExports(source) : undefined;
|
|
959
|
+
}
|
|
960
|
+
function parseReExports(source) {
|
|
961
|
+
if (!STARTS_WITH_EXPORT.test(source))
|
|
962
|
+
return;
|
|
963
|
+
const code = source.replace(COMMENTS, "");
|
|
964
|
+
const barrel = { names: new Map, stars: [] };
|
|
965
|
+
let rest = code.replace(NAMED_EXPORT, (_, list, from) => {
|
|
966
|
+
for (const part of list.split(",")) {
|
|
967
|
+
const name = part.trim().replace(TYPE_PREFIX, "").split(AS).pop();
|
|
968
|
+
if (name)
|
|
969
|
+
barrel.names.set(name, from);
|
|
970
|
+
}
|
|
971
|
+
return "";
|
|
972
|
+
});
|
|
973
|
+
rest = rest.replace(STAR_EXPORT, (_, name, from) => {
|
|
974
|
+
if (name)
|
|
975
|
+
barrel.names.set(name, from);
|
|
976
|
+
else
|
|
977
|
+
barrel.stars.push(from);
|
|
978
|
+
return "";
|
|
979
|
+
});
|
|
980
|
+
if (barrel.names.size + barrel.stars.length === 0)
|
|
981
|
+
return;
|
|
982
|
+
return ONLY_SEPARATORS.test(rest) ? barrel : undefined;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
// src/resolve.ts
|
|
986
|
+
import { builtinModules } from "node:module";
|
|
987
|
+
import { posix } from "node:path";
|
|
988
|
+
|
|
989
|
+
// src/syntax.ts
|
|
990
|
+
var QUERY = /[?#].*$/;
|
|
991
|
+
var UNDECLARED_READS = "reads the disk without @depends-on";
|
|
992
|
+
var RELATIVE = /^\.\.?(?:\/|$)/;
|
|
993
|
+
var URL_REF = /^[a-z]+:|^\/\//;
|
|
994
|
+
var LIST_SEPARATOR = /[\s,]+(?![^{]*\})/;
|
|
995
|
+
var DEPENDS_ON = /^\s*(?:\/\/|\/?\*+)\s*@depends-on\s+(.+?)\s*(?:\*\/)?\s*$/gm;
|
|
996
|
+
var READS = /^\s*(?:\/\/|\/?\*+)\s*@reads\s+(.+?)\s*(?:\*\/)?\s*$/gm;
|
|
997
|
+
var LISTS = /^\s*(?:\/\/|\/?\*+)\s*@lists\s+(.+?)\s*(?:\*\/)?\s*$/gm;
|
|
998
|
+
var GLOB_REGISTRY = /^\s*(?:\/\/|\/?\*+)\s*@glob-registry\b/m;
|
|
999
|
+
var AS2 = /\s+as\s+/;
|
|
1000
|
+
var TYPE_PREFIX2 = /^type\s+/;
|
|
1001
|
+
var NAMED_CLAUSE = /\{([^}]*)\}/;
|
|
1002
|
+
var DEFAULT_OR_NAMESPACE = /^\s*[\w$*]/;
|
|
1003
|
+
function namedBindings(clause) {
|
|
1004
|
+
const named = NAMED_CLAUSE.exec(clause);
|
|
1005
|
+
if (!named || DEFAULT_OR_NAMESPACE.test(clause.replace(NAMED_CLAUSE, ""))) {
|
|
1006
|
+
return;
|
|
1007
|
+
}
|
|
1008
|
+
return named[1].split(",").map((part) => part.trim().replace(TYPE_PREFIX2, "").split(AS2)[0].trim()).filter(Boolean);
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
// src/resolve.ts
|
|
1012
|
+
function isRecord2(value) {
|
|
1013
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1014
|
+
}
|
|
1015
|
+
function isStringRecord2(value) {
|
|
1016
|
+
return isRecord2(value) && Object.values(value).every((item) => typeof item === "string");
|
|
1017
|
+
}
|
|
1018
|
+
function toPackageJson(value) {
|
|
1019
|
+
if (!isRecord2(value))
|
|
1020
|
+
return {};
|
|
1021
|
+
const json = {};
|
|
1022
|
+
if (typeof value.name === "string")
|
|
1023
|
+
json.name = value.name;
|
|
1024
|
+
if (value.exports !== undefined)
|
|
1025
|
+
json.exports = value.exports;
|
|
1026
|
+
if (isRecord2(value.imports))
|
|
1027
|
+
json.imports = value.imports;
|
|
1028
|
+
if (typeof value.main === "string")
|
|
1029
|
+
json.main = value.main;
|
|
1030
|
+
const { sideEffects } = value;
|
|
1031
|
+
if (typeof sideEffects === "boolean" || Array.isArray(sideEffects) && sideEffects.every((glob) => typeof glob === "string")) {
|
|
1032
|
+
json.sideEffects = sideEffects;
|
|
1033
|
+
}
|
|
1034
|
+
for (const field of DEP_FIELDS) {
|
|
1035
|
+
const deps = value[field];
|
|
1036
|
+
if (isStringRecord2(deps))
|
|
1037
|
+
json[field] = deps;
|
|
1038
|
+
}
|
|
1039
|
+
return json;
|
|
1040
|
+
}
|
|
1041
|
+
var BUILTINS = new Set(builtinModules);
|
|
1042
|
+
var RUNTIME_BUILTIN = /^(?:node:|bun(?:$|:))/;
|
|
1043
|
+
var JS_EXTENSION = /\.(?:js|jsx|mjs|cjs)$/;
|
|
1044
|
+
var TS_SOURCES = {
|
|
1045
|
+
".js": [".ts", ".tsx", ".d.ts"],
|
|
1046
|
+
".jsx": [".tsx", ".ts", ".d.ts"],
|
|
1047
|
+
".mjs": [".mts", ".d.mts"],
|
|
1048
|
+
".cjs": [".cts", ".d.cts"]
|
|
1049
|
+
};
|
|
1050
|
+
var NODE_PREFIX = /^node:/;
|
|
1051
|
+
var INVALID_IMPORT_TARGET = /^[./#]/;
|
|
1052
|
+
var SVELTEKIT_ASSETS = "./%sveltekit.assets%/";
|
|
1053
|
+
var INDEX = "/index";
|
|
1054
|
+
var SVELTE_CONFIGS = ["js", "mjs", "cjs", "ts", "mts"].map((ext) => `svelte.config.${ext}`);
|
|
1055
|
+
var DEP_FIELDS = [
|
|
1056
|
+
"dependencies",
|
|
1057
|
+
"devDependencies",
|
|
1058
|
+
"peerDependencies",
|
|
1059
|
+
"optionalDependencies"
|
|
1060
|
+
];
|
|
1061
|
+
function packageName(specifier) {
|
|
1062
|
+
const parts = specifier.split("/");
|
|
1063
|
+
return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
1064
|
+
}
|
|
1065
|
+
function builtin(specifier) {
|
|
1066
|
+
if (RUNTIME_BUILTIN.test(specifier)) {
|
|
1067
|
+
const name = specifier.replace(NODE_PREFIX, "");
|
|
1068
|
+
return specifier.startsWith("bun") ? specifier : `node:${name.split("/")[0]}`;
|
|
1069
|
+
}
|
|
1070
|
+
const name = specifier.split("/")[0];
|
|
1071
|
+
return BUILTINS.has(specifier) || BUILTINS.has(name) ? `node:${name}` : undefined;
|
|
1072
|
+
}
|
|
1073
|
+
function targets(value, out = []) {
|
|
1074
|
+
if (typeof value === "string")
|
|
1075
|
+
out.push(value);
|
|
1076
|
+
else if (Array.isArray(value))
|
|
1077
|
+
for (const item of value)
|
|
1078
|
+
targets(item, out);
|
|
1079
|
+
else if (value && typeof value === "object") {
|
|
1080
|
+
for (const item of Object.values(value))
|
|
1081
|
+
targets(item, out);
|
|
1082
|
+
}
|
|
1083
|
+
return out;
|
|
1084
|
+
}
|
|
1085
|
+
function subpathMap(exports) {
|
|
1086
|
+
if (typeof exports === "string" || Array.isArray(exports)) {
|
|
1087
|
+
return { ".": exports };
|
|
1088
|
+
}
|
|
1089
|
+
if (!isRecord2(exports))
|
|
1090
|
+
return;
|
|
1091
|
+
const bySubpath = Object.keys(exports).some((key) => key.startsWith("."));
|
|
1092
|
+
return bySubpath ? exports : { ".": exports };
|
|
1093
|
+
}
|
|
1094
|
+
function mapTargets(map, subpath) {
|
|
1095
|
+
if (subpath in map && !subpath.includes("*"))
|
|
1096
|
+
return targets(map[subpath]);
|
|
1097
|
+
let best;
|
|
1098
|
+
for (const key of Object.keys(map)) {
|
|
1099
|
+
const star = key.indexOf("*");
|
|
1100
|
+
if (star === -1)
|
|
1101
|
+
continue;
|
|
1102
|
+
const prefix = key.slice(0, star);
|
|
1103
|
+
const suffix = key.slice(star + 1);
|
|
1104
|
+
if (subpath.length >= key.length - 1 && subpath.startsWith(prefix) && subpath.endsWith(suffix) && (!best || prefix.length > best.key.indexOf("*") || prefix.length === best.key.indexOf("*") && key.length > best.key.length)) {
|
|
1105
|
+
best = {
|
|
1106
|
+
key,
|
|
1107
|
+
match: subpath.slice(prefix.length, subpath.length - suffix.length)
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
if (!best)
|
|
1112
|
+
return;
|
|
1113
|
+
const { match } = best;
|
|
1114
|
+
return targets(map[best.key]).map((target) => target.replaceAll("*", match));
|
|
1115
|
+
}
|
|
1116
|
+
function exportTargets(exports, subpath) {
|
|
1117
|
+
const map = subpathMap(exports);
|
|
1118
|
+
return map && mapTargets(map, subpath);
|
|
1119
|
+
}
|
|
1120
|
+
function byDepth(packages) {
|
|
1121
|
+
return [...packages].sort(([a], [b]) => b.length - a.length);
|
|
1122
|
+
}
|
|
1123
|
+
function owner(packages, file) {
|
|
1124
|
+
return byDepth(packages).find(([dir]) => dir === "." || file.startsWith(`${dir}/`));
|
|
1125
|
+
}
|
|
1126
|
+
function aliasPath([key, target], specifier) {
|
|
1127
|
+
if (specifier !== key && !specifier.startsWith(`${key}/`))
|
|
1128
|
+
return;
|
|
1129
|
+
const rest = specifier.slice(key.length + 1);
|
|
1130
|
+
if (target === ".")
|
|
1131
|
+
return posix.normalize(rest || ".");
|
|
1132
|
+
return posix.normalize(rest ? `${target}/${rest}` : target);
|
|
1133
|
+
}
|
|
1134
|
+
function createResolver({
|
|
1135
|
+
files,
|
|
1136
|
+
config,
|
|
1137
|
+
packages,
|
|
1138
|
+
tsconfigs = new Map
|
|
1139
|
+
}) {
|
|
1140
|
+
const generated = (path) => config.generated.some(({ pattern }) => pattern.test(path));
|
|
1141
|
+
const { extensions } = config;
|
|
1142
|
+
const exists = (path) => files.has(path);
|
|
1143
|
+
const suffixed = new Map;
|
|
1144
|
+
const indexes = new Map;
|
|
1145
|
+
const byTail = [];
|
|
1146
|
+
const undotted = [];
|
|
1147
|
+
for (let i = 0;i < extensions.length; i++) {
|
|
1148
|
+
const ext = extensions[i];
|
|
1149
|
+
const dot = ext.lastIndexOf(".");
|
|
1150
|
+
if (dot === -1)
|
|
1151
|
+
undotted.push(i);
|
|
1152
|
+
else {
|
|
1153
|
+
const list = byTail[ext.length - dot];
|
|
1154
|
+
if (list)
|
|
1155
|
+
list.push(i);
|
|
1156
|
+
else
|
|
1157
|
+
byTail[ext.length - dot] = [i];
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
const addSuffixed = (i, file) => {
|
|
1161
|
+
const ext = extensions[i];
|
|
1162
|
+
if (!file.endsWith(ext))
|
|
1163
|
+
return;
|
|
1164
|
+
const base = file.slice(0, file.length - ext.length);
|
|
1165
|
+
const list = suffixed.get(base);
|
|
1166
|
+
if (list)
|
|
1167
|
+
list.push([i, file]);
|
|
1168
|
+
else {
|
|
1169
|
+
const created = [[i, file]];
|
|
1170
|
+
suffixed.set(base, created);
|
|
1171
|
+
if (base.endsWith(INDEX)) {
|
|
1172
|
+
indexes.set(base.slice(0, -INDEX.length), created);
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
};
|
|
1176
|
+
for (const file of files) {
|
|
1177
|
+
const dot = file.lastIndexOf(".");
|
|
1178
|
+
if (dot !== -1) {
|
|
1179
|
+
for (const i of byTail[file.length - dot] ?? [])
|
|
1180
|
+
addSuffixed(i, file);
|
|
1181
|
+
}
|
|
1182
|
+
for (const i of undotted)
|
|
1183
|
+
addSuffixed(i, file);
|
|
1184
|
+
}
|
|
1185
|
+
for (const list of suffixed.values()) {
|
|
1186
|
+
if (list.length > 1)
|
|
1187
|
+
list.sort(([a], [b]) => a - b);
|
|
1188
|
+
}
|
|
1189
|
+
const deepestFirst = byDepth(packages);
|
|
1190
|
+
const local = new Map;
|
|
1191
|
+
for (const [dir, json] of deepestFirst) {
|
|
1192
|
+
if (json.name && !local.has(json.name))
|
|
1193
|
+
local.set(json.name, dir);
|
|
1194
|
+
}
|
|
1195
|
+
const declaredIn = (dir) => {
|
|
1196
|
+
const names = new Set;
|
|
1197
|
+
for (const [pkgDir, json] of deepestFirst) {
|
|
1198
|
+
if (pkgDir !== "." && dir !== pkgDir && !dir.startsWith(`${pkgDir}/`))
|
|
1199
|
+
continue;
|
|
1200
|
+
for (const field of DEP_FIELDS) {
|
|
1201
|
+
for (const name of Object.keys(json[field] ?? {}))
|
|
1202
|
+
names.add(name);
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
return names;
|
|
1206
|
+
};
|
|
1207
|
+
const tsSources = (path) => {
|
|
1208
|
+
const js = JS_EXTENSION.exec(path);
|
|
1209
|
+
if (!js)
|
|
1210
|
+
return [];
|
|
1211
|
+
const stem = path.slice(0, js.index);
|
|
1212
|
+
return (TS_SOURCES[js[0]] ?? []).map((ext) => stem + ext).filter((file) => generated(file) || exists(file));
|
|
1213
|
+
};
|
|
1214
|
+
const exact = (path) => generated(path) || exists(path) ? [path] : tsSources(path);
|
|
1215
|
+
const probe = (base, guess = false) => {
|
|
1216
|
+
if (generated(base))
|
|
1217
|
+
return [base];
|
|
1218
|
+
const found = [];
|
|
1219
|
+
if (exists(base))
|
|
1220
|
+
found.push(base);
|
|
1221
|
+
for (const [, file] of suffixed.get(base) ?? [])
|
|
1222
|
+
found.push(file);
|
|
1223
|
+
if (found.length === 0)
|
|
1224
|
+
found.push(...tsSources(base));
|
|
1225
|
+
for (const [, file] of indexes.get(base) ?? [])
|
|
1226
|
+
found.push(file);
|
|
1227
|
+
if (found.length === 0 && !guess && config.generated.length > 0) {
|
|
1228
|
+
for (const ext of extensions) {
|
|
1229
|
+
if (generated(base + ext))
|
|
1230
|
+
found.push(base + ext);
|
|
1231
|
+
}
|
|
1232
|
+
for (const ext of extensions) {
|
|
1233
|
+
if (generated(`${base}/index${ext}`))
|
|
1234
|
+
found.push(`${base}/index${ext}`);
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
return found;
|
|
1238
|
+
};
|
|
1239
|
+
const tsDeepestFirst = [...tsconfigs].sort(([a], [b]) => b.length - a.length);
|
|
1240
|
+
const scopes = new Map;
|
|
1241
|
+
const scopeOf = (from) => {
|
|
1242
|
+
const contains = ([dir]) => dir === "." || from.startsWith(`${dir}/`);
|
|
1243
|
+
const pkg = deepestFirst.findIndex(contains);
|
|
1244
|
+
const ts = tsDeepestFirst.findIndex(contains);
|
|
1245
|
+
const key = `${pkg} ${ts}`;
|
|
1246
|
+
let scope = scopes.get(key);
|
|
1247
|
+
if (!scope) {
|
|
1248
|
+
const owning = pkg === -1 ? undefined : deepestFirst[pkg];
|
|
1249
|
+
scope = {
|
|
1250
|
+
owning,
|
|
1251
|
+
ts: ts === -1 ? undefined : tsDeepestFirst[ts][1],
|
|
1252
|
+
declared: declaredIn(owning?.[0] ?? "."),
|
|
1253
|
+
resolved: new Map
|
|
1254
|
+
};
|
|
1255
|
+
scopes.set(key, scope);
|
|
1256
|
+
}
|
|
1257
|
+
return scope;
|
|
1258
|
+
};
|
|
1259
|
+
const svelteKitRoot = (dir) => {
|
|
1260
|
+
for (let at = dir;; at = posix.dirname(at)) {
|
|
1261
|
+
if (at === "." || packages.has(at))
|
|
1262
|
+
return at;
|
|
1263
|
+
if (SVELTE_CONFIGS.some((name) => exists(`${at}/${name}`)))
|
|
1264
|
+
return at;
|
|
1265
|
+
}
|
|
1266
|
+
};
|
|
1267
|
+
const inPackage = (dir, path) => dir === "." ? path : posix.join(dir, path);
|
|
1268
|
+
const resolveLocal = (dir, json, subpath) => {
|
|
1269
|
+
if (json.exports !== undefined) {
|
|
1270
|
+
const list = exportTargets(json.exports, subpath);
|
|
1271
|
+
return [
|
|
1272
|
+
...new Set((list ?? []).flatMap((target) => exact(inPackage(dir, posix.normalize(target)))))
|
|
1273
|
+
];
|
|
1274
|
+
}
|
|
1275
|
+
if (subpath === ".") {
|
|
1276
|
+
return probe(inPackage(dir, posix.normalize(json.main ?? "index")));
|
|
1277
|
+
}
|
|
1278
|
+
return probe(inPackage(dir, subpath.slice(2)));
|
|
1279
|
+
};
|
|
1280
|
+
const resolveImport = (scope, bare) => {
|
|
1281
|
+
const { owning } = scope;
|
|
1282
|
+
const imports = owning?.[1].imports;
|
|
1283
|
+
const list = owning && imports && mapTargets(imports, bare);
|
|
1284
|
+
if (!list)
|
|
1285
|
+
return;
|
|
1286
|
+
const found = [];
|
|
1287
|
+
let pkg;
|
|
1288
|
+
for (const target of list) {
|
|
1289
|
+
if (target.startsWith("./")) {
|
|
1290
|
+
for (const path of exact(inPackage(owning[0], posix.normalize(target)))) {
|
|
1291
|
+
if (!found.includes(path))
|
|
1292
|
+
found.push(path);
|
|
1293
|
+
}
|
|
1294
|
+
continue;
|
|
1295
|
+
}
|
|
1296
|
+
if (INVALID_IMPORT_TARGET.test(target))
|
|
1297
|
+
continue;
|
|
1298
|
+
const specifier = target.replace(QUERY, "");
|
|
1299
|
+
const resolved = specifier === "" ? undefined : resolveBare(scope, specifier);
|
|
1300
|
+
if (resolved === undefined)
|
|
1301
|
+
return;
|
|
1302
|
+
if ("pkg" in resolved)
|
|
1303
|
+
pkg ??= resolved.pkg;
|
|
1304
|
+
else {
|
|
1305
|
+
for (const file of resolved.files) {
|
|
1306
|
+
if (!found.includes(file))
|
|
1307
|
+
found.push(file);
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
if (found.length > 0)
|
|
1312
|
+
return { files: found };
|
|
1313
|
+
return pkg === undefined ? undefined : { pkg };
|
|
1314
|
+
};
|
|
1315
|
+
const resolveRelative = (fromDir, bare) => {
|
|
1316
|
+
if (bare.startsWith(SVELTEKIT_ASSETS)) {
|
|
1317
|
+
const rest = bare.slice(SVELTEKIT_ASSETS.length);
|
|
1318
|
+
const found = probe(posix.join(svelteKitRoot(fromDir), "static", rest));
|
|
1319
|
+
return found.length > 0 ? { files: found } : undefined;
|
|
1320
|
+
}
|
|
1321
|
+
const found = probe(posix.join(fromDir, bare));
|
|
1322
|
+
return found.length > 0 ? { files: found } : undefined;
|
|
1323
|
+
};
|
|
1324
|
+
const resolveBare = (scope, bare) => {
|
|
1325
|
+
for (const alias of config.alias) {
|
|
1326
|
+
const path = aliasPath(alias, bare);
|
|
1327
|
+
if (path === undefined)
|
|
1328
|
+
continue;
|
|
1329
|
+
const found = probe(path);
|
|
1330
|
+
if (found.length > 0)
|
|
1331
|
+
return { files: found };
|
|
1332
|
+
}
|
|
1333
|
+
const { ts } = scope;
|
|
1334
|
+
const mapped = ts && mapTargets(ts.paths, bare);
|
|
1335
|
+
if (mapped) {
|
|
1336
|
+
const found = [
|
|
1337
|
+
...new Set(mapped.flatMap((path) => probe(posix.normalize(path))))
|
|
1338
|
+
];
|
|
1339
|
+
if (found.length > 0)
|
|
1340
|
+
return { files: found };
|
|
1341
|
+
}
|
|
1342
|
+
if (bare.startsWith("#"))
|
|
1343
|
+
return resolveImport(scope, bare);
|
|
1344
|
+
const name = packageName(bare);
|
|
1345
|
+
const dir = local.get(name);
|
|
1346
|
+
if (dir !== undefined) {
|
|
1347
|
+
const subpath = `.${bare.slice(name.length)}`;
|
|
1348
|
+
const found = resolveLocal(dir, packages.get(dir) ?? {}, subpath);
|
|
1349
|
+
return found.length > 0 ? { files: found } : undefined;
|
|
1350
|
+
}
|
|
1351
|
+
if (ts?.baseUrl !== undefined) {
|
|
1352
|
+
const found = probe(posix.join(ts.baseUrl, bare), true);
|
|
1353
|
+
if (found.length > 0)
|
|
1354
|
+
return { files: found };
|
|
1355
|
+
}
|
|
1356
|
+
const node = builtin(bare);
|
|
1357
|
+
if (node)
|
|
1358
|
+
return { pkg: node };
|
|
1359
|
+
if (scope.declared.has(name) || config.external.has(name)) {
|
|
1360
|
+
return { pkg: name };
|
|
1361
|
+
}
|
|
1362
|
+
return;
|
|
1363
|
+
};
|
|
1364
|
+
const dirs = new Map;
|
|
1365
|
+
const directoryOf = (from) => {
|
|
1366
|
+
const dir = posix.dirname(from);
|
|
1367
|
+
let found = dirs.get(dir);
|
|
1368
|
+
if (!found) {
|
|
1369
|
+
found = { dir, relative: new Map, scope: scopeOf(from) };
|
|
1370
|
+
dirs.set(dir, found);
|
|
1371
|
+
}
|
|
1372
|
+
return found;
|
|
1373
|
+
};
|
|
1374
|
+
let lastFrom;
|
|
1375
|
+
let last;
|
|
1376
|
+
return (from, specifier) => {
|
|
1377
|
+
if (from !== lastFrom || !last) {
|
|
1378
|
+
lastFrom = from;
|
|
1379
|
+
last = directoryOf(from);
|
|
1380
|
+
}
|
|
1381
|
+
const { dir, relative, scope } = last;
|
|
1382
|
+
const hash = specifier.startsWith("#") ? "#" : "";
|
|
1383
|
+
const isRelative = RELATIVE.test(specifier);
|
|
1384
|
+
const memo = isRelative ? relative : scope.resolved;
|
|
1385
|
+
if (memo.has(specifier))
|
|
1386
|
+
return memo.get(specifier);
|
|
1387
|
+
const bare = hash + specifier.slice(hash.length).replace(QUERY, "");
|
|
1388
|
+
let resolved;
|
|
1389
|
+
if (bare !== "" && bare !== "#") {
|
|
1390
|
+
resolved = isRelative ? resolveRelative(dir, bare) : resolveBare(scope, bare);
|
|
1391
|
+
}
|
|
1392
|
+
memo.set(specifier, resolved);
|
|
1393
|
+
return resolved;
|
|
1394
|
+
};
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
// src/scan.ts
|
|
1398
|
+
import { posix as posix2 } from "node:path";
|
|
1399
|
+
|
|
1400
|
+
// src/lex.ts
|
|
1401
|
+
function createLexed() {
|
|
1402
|
+
return {
|
|
1403
|
+
imports: [],
|
|
1404
|
+
globs: [],
|
|
1405
|
+
gotos: [],
|
|
1406
|
+
dependsOn: [],
|
|
1407
|
+
reads: [],
|
|
1408
|
+
lists: [],
|
|
1409
|
+
registry: false,
|
|
1410
|
+
nonLiteralImport: false,
|
|
1411
|
+
nonLiteralGlob: false,
|
|
1412
|
+
readsDisk: false
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
class LexError extends Error {
|
|
1417
|
+
}
|
|
1418
|
+
var TS = { jsx: false, tsx: false };
|
|
1419
|
+
var JS = { jsx: true, tsx: false };
|
|
1420
|
+
var TSX = { jsx: true, tsx: true };
|
|
1421
|
+
var JS_FILE = /\.[cm]?jsx?$/;
|
|
1422
|
+
function dialectOf(path) {
|
|
1423
|
+
if (path.endsWith(".tsx"))
|
|
1424
|
+
return TSX;
|
|
1425
|
+
if (JS_FILE.test(path))
|
|
1426
|
+
return JS;
|
|
1427
|
+
return TS;
|
|
1428
|
+
}
|
|
1429
|
+
var T_START = 0;
|
|
1430
|
+
var T_PUNCT = 1;
|
|
1431
|
+
var T_OPERAND = 2;
|
|
1432
|
+
var T_IDENT = 3;
|
|
1433
|
+
var T_DOT = 4;
|
|
1434
|
+
var T_CLOSE_PAREN = 5;
|
|
1435
|
+
var T_CLOSE_KW = 6;
|
|
1436
|
+
var T_CLOSE_BLOCK = 7;
|
|
1437
|
+
var T_ARROW = 8;
|
|
1438
|
+
var T_SEMI = 9;
|
|
1439
|
+
var T_LBRACE = 10;
|
|
1440
|
+
var M_PAREN = 0;
|
|
1441
|
+
var M_PAREN_KW = 1;
|
|
1442
|
+
var M_BRACKET = 2;
|
|
1443
|
+
var M_BLOCK = 3;
|
|
1444
|
+
var M_EXPR = 4;
|
|
1445
|
+
var M_TEMPLATE = 5;
|
|
1446
|
+
var DISK = new Set([
|
|
1447
|
+
"fs",
|
|
1448
|
+
"node:fs",
|
|
1449
|
+
"fs/promises",
|
|
1450
|
+
"node:fs/promises",
|
|
1451
|
+
"child_process",
|
|
1452
|
+
"node:child_process"
|
|
1453
|
+
]);
|
|
1454
|
+
var VI_CALLS = new Set([
|
|
1455
|
+
"mock",
|
|
1456
|
+
"doMock",
|
|
1457
|
+
"unmock",
|
|
1458
|
+
"importActual",
|
|
1459
|
+
"importMock"
|
|
1460
|
+
]);
|
|
1461
|
+
var CALL_IMPORT = 0;
|
|
1462
|
+
var CALL_PROPERTY = 1;
|
|
1463
|
+
var CALL_MOCK = 2;
|
|
1464
|
+
var CALL_ACTUAL = 3;
|
|
1465
|
+
var src = "";
|
|
1466
|
+
var end = 0;
|
|
1467
|
+
var out = createLexed();
|
|
1468
|
+
var dialect = TS;
|
|
1469
|
+
var nextAt = -1;
|
|
1470
|
+
var nextImport = -1;
|
|
1471
|
+
var LS = String.fromCharCode(8232);
|
|
1472
|
+
var PS = String.fromCharCode(8233);
|
|
1473
|
+
var oddLines = false;
|
|
1474
|
+
var templateOpen = false;
|
|
1475
|
+
function fail(message, at) {
|
|
1476
|
+
throw new LexError(`${message} at ${at}`);
|
|
1477
|
+
}
|
|
1478
|
+
function openFile(source, sink) {
|
|
1479
|
+
src = source;
|
|
1480
|
+
out = sink;
|
|
1481
|
+
nextAt = -1;
|
|
1482
|
+
nextImport = -1;
|
|
1483
|
+
oddLines = source.includes("\r") || source.includes(LS) || source.includes(PS);
|
|
1484
|
+
}
|
|
1485
|
+
function lexRange(start, stop, language = TS) {
|
|
1486
|
+
dialect = language;
|
|
1487
|
+
end = stop;
|
|
1488
|
+
let i = start;
|
|
1489
|
+
if (src.charCodeAt(i) === 65279)
|
|
1490
|
+
i++;
|
|
1491
|
+
if (src.charCodeAt(i) === 35 && src.charCodeAt(i + 1) === 33) {
|
|
1492
|
+
i = lineEnd(i);
|
|
1493
|
+
}
|
|
1494
|
+
code(i, false);
|
|
1495
|
+
}
|
|
1496
|
+
function lexExpression(start, stop) {
|
|
1497
|
+
dialect = TS;
|
|
1498
|
+
end = stop;
|
|
1499
|
+
return code(start, true);
|
|
1500
|
+
}
|
|
1501
|
+
function isUnicodeSpace(c) {
|
|
1502
|
+
return c === 160 || c === 65279 || c === 5760 || c >= 8192 && c <= 8202 || c === 8232 || c === 8233 || c === 8239 || c === 8287 || c === 12288;
|
|
1503
|
+
}
|
|
1504
|
+
var IDENT = new Uint8Array(128);
|
|
1505
|
+
for (let c = 0;c < 128; c++) {
|
|
1506
|
+
const letter = c >= 97 && c <= 122 || c >= 65 && c <= 90 || c === 36 || c === 95;
|
|
1507
|
+
if (letter)
|
|
1508
|
+
IDENT[c] = 3;
|
|
1509
|
+
else if (c >= 48 && c <= 57)
|
|
1510
|
+
IDENT[c] = 2;
|
|
1511
|
+
}
|
|
1512
|
+
function isIdentStart(c) {
|
|
1513
|
+
return c < 128 ? (IDENT[c] & 1) !== 0 : c >= 128 && !isUnicodeSpace(c);
|
|
1514
|
+
}
|
|
1515
|
+
function isIdentPart(c) {
|
|
1516
|
+
return c < 128 ? IDENT[c] !== 0 : c >= 128 && !isUnicodeSpace(c);
|
|
1517
|
+
}
|
|
1518
|
+
function identEnd(start) {
|
|
1519
|
+
let i = start;
|
|
1520
|
+
const s = src;
|
|
1521
|
+
const n = end;
|
|
1522
|
+
while (i < n) {
|
|
1523
|
+
const c = s.charCodeAt(i);
|
|
1524
|
+
if (c < 128 ? IDENT[c] === 0 : isUnicodeSpace(c))
|
|
1525
|
+
break;
|
|
1526
|
+
i++;
|
|
1527
|
+
}
|
|
1528
|
+
return i;
|
|
1529
|
+
}
|
|
1530
|
+
function isWord(i, word) {
|
|
1531
|
+
return i + word.length <= end && src.startsWith(word, i) && !isIdentPart(src.charCodeAt(i + word.length));
|
|
1532
|
+
}
|
|
1533
|
+
function lineEnd(start) {
|
|
1534
|
+
let i = start;
|
|
1535
|
+
if (oddLines) {
|
|
1536
|
+
while (i < end) {
|
|
1537
|
+
const c = src.charCodeAt(i);
|
|
1538
|
+
if (c === 10 || c === 13 || c === 8232 || c === 8233)
|
|
1539
|
+
return i;
|
|
1540
|
+
i++;
|
|
1541
|
+
}
|
|
1542
|
+
return end;
|
|
1543
|
+
}
|
|
1544
|
+
const e = src.indexOf(`
|
|
1545
|
+
`, i);
|
|
1546
|
+
return e === -1 || e > end ? end : e;
|
|
1547
|
+
}
|
|
1548
|
+
function skipTrivia(start) {
|
|
1549
|
+
let i = start;
|
|
1550
|
+
while (i < end) {
|
|
1551
|
+
const c = src.charCodeAt(i);
|
|
1552
|
+
if (c === 32 || c >= 9 && c <= 13 || c >= 128 && isUnicodeSpace(c)) {
|
|
1553
|
+
i++;
|
|
1554
|
+
} else if (c === 47 && src.charCodeAt(i + 1) === 47) {
|
|
1555
|
+
i = lineEnd(i + 2);
|
|
1556
|
+
} else if (c === 47 && src.charCodeAt(i + 1) === 42) {
|
|
1557
|
+
const e = src.indexOf("*/", i + 2);
|
|
1558
|
+
if (e === -1 || e + 2 > end)
|
|
1559
|
+
return end;
|
|
1560
|
+
i = e + 2;
|
|
1561
|
+
} else {
|
|
1562
|
+
break;
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
return i;
|
|
1566
|
+
}
|
|
1567
|
+
function stringEnd(i) {
|
|
1568
|
+
const quote = src.charCodeAt(i);
|
|
1569
|
+
let j = i + 1;
|
|
1570
|
+
while (j < end) {
|
|
1571
|
+
const c = src.charCodeAt(j);
|
|
1572
|
+
if (c === quote)
|
|
1573
|
+
return j + 1;
|
|
1574
|
+
if (c === 92) {
|
|
1575
|
+
j += src.charCodeAt(j + 1) === 13 && src.charCodeAt(j + 2) === 10 ? 3 : 2;
|
|
1576
|
+
continue;
|
|
1577
|
+
}
|
|
1578
|
+
if (c === 10 || c === 13)
|
|
1579
|
+
return -1;
|
|
1580
|
+
j++;
|
|
1581
|
+
}
|
|
1582
|
+
return -1;
|
|
1583
|
+
}
|
|
1584
|
+
function template(start) {
|
|
1585
|
+
let j = start;
|
|
1586
|
+
while (j < end) {
|
|
1587
|
+
const c = src.charCodeAt(j);
|
|
1588
|
+
if (c === 96) {
|
|
1589
|
+
templateOpen = false;
|
|
1590
|
+
return j + 1;
|
|
1591
|
+
}
|
|
1592
|
+
if (c === 92) {
|
|
1593
|
+
j += 2;
|
|
1594
|
+
continue;
|
|
1595
|
+
}
|
|
1596
|
+
if (c === 36 && src.charCodeAt(j + 1) === 123) {
|
|
1597
|
+
templateOpen = true;
|
|
1598
|
+
return j + 2;
|
|
1599
|
+
}
|
|
1600
|
+
j++;
|
|
1601
|
+
}
|
|
1602
|
+
return fail("unterminated template", j);
|
|
1603
|
+
}
|
|
1604
|
+
function templatePart(start, stack) {
|
|
1605
|
+
const e = template(start);
|
|
1606
|
+
if (out.strings && !stack.includes(M_BRACKET)) {
|
|
1607
|
+
out.strings.push(src.slice(start, templateOpen ? e - 2 : e - 1));
|
|
1608
|
+
}
|
|
1609
|
+
return e;
|
|
1610
|
+
}
|
|
1611
|
+
function regex(i) {
|
|
1612
|
+
let j = i + 1;
|
|
1613
|
+
let inClass = false;
|
|
1614
|
+
for (;; ) {
|
|
1615
|
+
if (j >= end)
|
|
1616
|
+
fail("unterminated regex", i);
|
|
1617
|
+
const c = src.charCodeAt(j);
|
|
1618
|
+
if (c === 92) {
|
|
1619
|
+
j += 2;
|
|
1620
|
+
continue;
|
|
1621
|
+
}
|
|
1622
|
+
if (c === 10 || c === 13)
|
|
1623
|
+
fail("unterminated regex", i);
|
|
1624
|
+
if (inClass) {
|
|
1625
|
+
if (c === 93)
|
|
1626
|
+
inClass = false;
|
|
1627
|
+
} else if (c === 47) {
|
|
1628
|
+
break;
|
|
1629
|
+
} else if (c === 91) {
|
|
1630
|
+
inClass = true;
|
|
1631
|
+
}
|
|
1632
|
+
j++;
|
|
1633
|
+
}
|
|
1634
|
+
return identEnd(j + 1);
|
|
1635
|
+
}
|
|
1636
|
+
function isOneOf(start, stop, words) {
|
|
1637
|
+
if (stop - start !== words[0].length)
|
|
1638
|
+
return false;
|
|
1639
|
+
const c = src.charCodeAt(start);
|
|
1640
|
+
for (const word of words) {
|
|
1641
|
+
if (word.charCodeAt(0) === c && src.startsWith(word, start))
|
|
1642
|
+
return true;
|
|
1643
|
+
}
|
|
1644
|
+
return false;
|
|
1645
|
+
}
|
|
1646
|
+
var EXPRESSION_KEYWORDS = [];
|
|
1647
|
+
var PAREN_KEYWORDS = [];
|
|
1648
|
+
var BLOCK_KEYWORDS = [];
|
|
1649
|
+
function byLength(table, words) {
|
|
1650
|
+
for (const word of words) {
|
|
1651
|
+
table[word.length] = [...table[word.length] ?? [], word];
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
byLength(EXPRESSION_KEYWORDS, [
|
|
1655
|
+
...["in", "do", "new", "case", "else", "void", "throw", "yield"],
|
|
1656
|
+
...["await", "return", "typeof", "delete", "default", "instanceof"]
|
|
1657
|
+
]);
|
|
1658
|
+
byLength(PAREN_KEYWORDS, ["if", "for", "with", "while"]);
|
|
1659
|
+
byLength(BLOCK_KEYWORDS, ["do", "try", "else", "finally"]);
|
|
1660
|
+
function isKeyword(table, start, stop) {
|
|
1661
|
+
const words = table[stop - start];
|
|
1662
|
+
return words !== undefined && isOneOf(start, stop, words);
|
|
1663
|
+
}
|
|
1664
|
+
function isExpressionKeyword(start, stop) {
|
|
1665
|
+
return isKeyword(EXPRESSION_KEYWORDS, start, stop);
|
|
1666
|
+
}
|
|
1667
|
+
function valueEnded(last, wordStart, wordEnd, wordIsProperty) {
|
|
1668
|
+
return last === T_OPERAND || last === T_CLOSE_PAREN || last === T_IDENT && (wordIsProperty || !isExpressionKeyword(wordStart, wordEnd));
|
|
1669
|
+
}
|
|
1670
|
+
function isParenKeyword(start, stop) {
|
|
1671
|
+
return isKeyword(PAREN_KEYWORDS, start, stop);
|
|
1672
|
+
}
|
|
1673
|
+
function isBlockKeyword(start, stop) {
|
|
1674
|
+
return isKeyword(BLOCK_KEYWORDS, start, stop);
|
|
1675
|
+
}
|
|
1676
|
+
function isWordAt(start, stop, word) {
|
|
1677
|
+
return stop - start === word.length && src.startsWith(word, start);
|
|
1678
|
+
}
|
|
1679
|
+
var KEYWORD_SHAPE = new Uint8Array(8 << 7);
|
|
1680
|
+
for (const word of ["vi", "Bun", "goto", "import", "export", "require"]) {
|
|
1681
|
+
KEYWORD_SHAPE[word.length << 7 | word.charCodeAt(0)] = 1;
|
|
1682
|
+
}
|
|
1683
|
+
function keyword(c, i, j, property) {
|
|
1684
|
+
switch (j - i) {
|
|
1685
|
+
case 2:
|
|
1686
|
+
if (src.charCodeAt(i + 1) === 105)
|
|
1687
|
+
vi(j);
|
|
1688
|
+
break;
|
|
1689
|
+
case 3:
|
|
1690
|
+
if (src.startsWith("Bun", i))
|
|
1691
|
+
bun(j);
|
|
1692
|
+
break;
|
|
1693
|
+
case 4:
|
|
1694
|
+
if (property && src.startsWith("goto", i))
|
|
1695
|
+
goto(j);
|
|
1696
|
+
break;
|
|
1697
|
+
case 6:
|
|
1698
|
+
if (!property) {
|
|
1699
|
+
if (c === 105 && src.startsWith("import", i))
|
|
1700
|
+
importAt(j);
|
|
1701
|
+
else if (c === 101 && src.startsWith("export", i))
|
|
1702
|
+
exportAt(j);
|
|
1703
|
+
}
|
|
1704
|
+
break;
|
|
1705
|
+
case 7:
|
|
1706
|
+
if (src.startsWith("require", i))
|
|
1707
|
+
requireAt(j, property);
|
|
1708
|
+
break;
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
function code(start, brace) {
|
|
1712
|
+
let i = start;
|
|
1713
|
+
const s = src;
|
|
1714
|
+
const n = end;
|
|
1715
|
+
const stack = [];
|
|
1716
|
+
let last = T_START;
|
|
1717
|
+
let wordStart = 0;
|
|
1718
|
+
let wordEnd = 0;
|
|
1719
|
+
let wordIsProperty = false;
|
|
1720
|
+
while (i < n) {
|
|
1721
|
+
const c = s.charCodeAt(i);
|
|
1722
|
+
if (c < 128 ? (IDENT[c] & 1) !== 0 : !isUnicodeSpace(c)) {
|
|
1723
|
+
let j = i + 1;
|
|
1724
|
+
while (j < n) {
|
|
1725
|
+
const d = s.charCodeAt(j);
|
|
1726
|
+
if (d < 128 ? IDENT[d] === 0 : isUnicodeSpace(d))
|
|
1727
|
+
break;
|
|
1728
|
+
j++;
|
|
1729
|
+
}
|
|
1730
|
+
const property = last === T_DOT;
|
|
1731
|
+
if (c < 128 && j - i < 8 && KEYWORD_SHAPE[j - i << 7 | c] !== 0) {
|
|
1732
|
+
keyword(c, i, j, property);
|
|
1733
|
+
}
|
|
1734
|
+
last = T_IDENT;
|
|
1735
|
+
wordStart = i;
|
|
1736
|
+
wordEnd = j;
|
|
1737
|
+
wordIsProperty = property;
|
|
1738
|
+
i = j;
|
|
1739
|
+
continue;
|
|
1740
|
+
}
|
|
1741
|
+
switch (c) {
|
|
1742
|
+
case 32:
|
|
1743
|
+
case 9:
|
|
1744
|
+
case 10:
|
|
1745
|
+
case 11:
|
|
1746
|
+
case 12:
|
|
1747
|
+
case 13:
|
|
1748
|
+
i++;
|
|
1749
|
+
continue;
|
|
1750
|
+
case 47: {
|
|
1751
|
+
const next = src.charCodeAt(i + 1);
|
|
1752
|
+
if (next === 47) {
|
|
1753
|
+
const e = lineEnd(i + 2);
|
|
1754
|
+
comment(i, e, false);
|
|
1755
|
+
i = e;
|
|
1756
|
+
} else if (next === 42) {
|
|
1757
|
+
const e = src.indexOf("*/", i + 2);
|
|
1758
|
+
if (e === -1 || e + 2 > end)
|
|
1759
|
+
fail("unterminated comment", i);
|
|
1760
|
+
comment(i, e + 2, true);
|
|
1761
|
+
i = e + 2;
|
|
1762
|
+
} else if (valueEnded(last, wordStart, wordEnd, wordIsProperty)) {
|
|
1763
|
+
i++;
|
|
1764
|
+
last = T_PUNCT;
|
|
1765
|
+
} else {
|
|
1766
|
+
i = regex(i);
|
|
1767
|
+
last = T_OPERAND;
|
|
1768
|
+
}
|
|
1769
|
+
continue;
|
|
1770
|
+
}
|
|
1771
|
+
case 39:
|
|
1772
|
+
case 34: {
|
|
1773
|
+
const e = stringEnd(i);
|
|
1774
|
+
if (e === -1)
|
|
1775
|
+
fail("unterminated string", i);
|
|
1776
|
+
if (out.strings && !stack.includes(M_BRACKET)) {
|
|
1777
|
+
out.strings.push(src.slice(i + 1, e - 1));
|
|
1778
|
+
}
|
|
1779
|
+
i = e;
|
|
1780
|
+
last = T_OPERAND;
|
|
1781
|
+
continue;
|
|
1782
|
+
}
|
|
1783
|
+
case 96:
|
|
1784
|
+
i = templatePart(i + 1, stack);
|
|
1785
|
+
if (templateOpen) {
|
|
1786
|
+
stack.push(M_TEMPLATE);
|
|
1787
|
+
last = T_PUNCT;
|
|
1788
|
+
} else {
|
|
1789
|
+
last = T_OPERAND;
|
|
1790
|
+
}
|
|
1791
|
+
continue;
|
|
1792
|
+
case 123: {
|
|
1793
|
+
const block = last === T_START || last === T_SEMI || last === T_LBRACE || last === T_CLOSE_PAREN || last === T_CLOSE_KW || last === T_CLOSE_BLOCK || last === T_ARROW || last === T_IDENT && !wordIsProperty && isBlockKeyword(wordStart, wordEnd);
|
|
1794
|
+
stack.push(block ? M_BLOCK : M_EXPR);
|
|
1795
|
+
last = T_LBRACE;
|
|
1796
|
+
i++;
|
|
1797
|
+
continue;
|
|
1798
|
+
}
|
|
1799
|
+
case 125: {
|
|
1800
|
+
if (stack.length === 0) {
|
|
1801
|
+
if (brace)
|
|
1802
|
+
return i + 1;
|
|
1803
|
+
fail("unbalanced }", i);
|
|
1804
|
+
}
|
|
1805
|
+
const open = stack.pop();
|
|
1806
|
+
if (open === M_TEMPLATE) {
|
|
1807
|
+
i = templatePart(i + 1, stack);
|
|
1808
|
+
if (templateOpen) {
|
|
1809
|
+
stack.push(M_TEMPLATE);
|
|
1810
|
+
last = T_PUNCT;
|
|
1811
|
+
} else {
|
|
1812
|
+
last = T_OPERAND;
|
|
1813
|
+
}
|
|
1814
|
+
continue;
|
|
1815
|
+
}
|
|
1816
|
+
if (open === M_BLOCK)
|
|
1817
|
+
last = T_CLOSE_BLOCK;
|
|
1818
|
+
else if (open === M_EXPR)
|
|
1819
|
+
last = T_OPERAND;
|
|
1820
|
+
else
|
|
1821
|
+
fail("unbalanced }", i);
|
|
1822
|
+
i++;
|
|
1823
|
+
continue;
|
|
1824
|
+
}
|
|
1825
|
+
case 40:
|
|
1826
|
+
stack.push(last === T_IDENT && !wordIsProperty && isParenKeyword(wordStart, wordEnd) ? M_PAREN_KW : M_PAREN);
|
|
1827
|
+
last = T_PUNCT;
|
|
1828
|
+
i++;
|
|
1829
|
+
continue;
|
|
1830
|
+
case 41: {
|
|
1831
|
+
const open = stack.pop();
|
|
1832
|
+
if (open === M_PAREN)
|
|
1833
|
+
last = T_CLOSE_PAREN;
|
|
1834
|
+
else if (open === M_PAREN_KW)
|
|
1835
|
+
last = T_CLOSE_KW;
|
|
1836
|
+
else
|
|
1837
|
+
fail("unbalanced )", i);
|
|
1838
|
+
i++;
|
|
1839
|
+
continue;
|
|
1840
|
+
}
|
|
1841
|
+
case 91:
|
|
1842
|
+
stack.push(M_BRACKET);
|
|
1843
|
+
last = T_PUNCT;
|
|
1844
|
+
i++;
|
|
1845
|
+
continue;
|
|
1846
|
+
case 93:
|
|
1847
|
+
if (stack.pop() !== M_BRACKET)
|
|
1848
|
+
fail("unbalanced ]", i);
|
|
1849
|
+
last = T_OPERAND;
|
|
1850
|
+
i++;
|
|
1851
|
+
continue;
|
|
1852
|
+
case 59:
|
|
1853
|
+
last = T_SEMI;
|
|
1854
|
+
i++;
|
|
1855
|
+
continue;
|
|
1856
|
+
case 46:
|
|
1857
|
+
if (src.charCodeAt(i + 1) === 46 && src.charCodeAt(i + 2) === 46) {
|
|
1858
|
+
last = T_PUNCT;
|
|
1859
|
+
i += 3;
|
|
1860
|
+
} else if (isDigit(src.charCodeAt(i + 1))) {
|
|
1861
|
+
i = numberEnd(i + 1);
|
|
1862
|
+
last = T_OPERAND;
|
|
1863
|
+
} else {
|
|
1864
|
+
last = T_DOT;
|
|
1865
|
+
i++;
|
|
1866
|
+
}
|
|
1867
|
+
continue;
|
|
1868
|
+
case 61:
|
|
1869
|
+
if (src.charCodeAt(i + 1) === 62) {
|
|
1870
|
+
last = T_ARROW;
|
|
1871
|
+
i += 2;
|
|
1872
|
+
} else {
|
|
1873
|
+
last = T_PUNCT;
|
|
1874
|
+
i++;
|
|
1875
|
+
}
|
|
1876
|
+
continue;
|
|
1877
|
+
case 43:
|
|
1878
|
+
case 45:
|
|
1879
|
+
if (src.charCodeAt(i + 1) === c) {
|
|
1880
|
+
last = last === T_OPERAND || last === T_IDENT || last === T_CLOSE_PAREN ? T_OPERAND : T_PUNCT;
|
|
1881
|
+
i += 2;
|
|
1882
|
+
} else {
|
|
1883
|
+
last = T_PUNCT;
|
|
1884
|
+
i++;
|
|
1885
|
+
}
|
|
1886
|
+
continue;
|
|
1887
|
+
case 33:
|
|
1888
|
+
last = valueEnded(last, wordStart, wordEnd, wordIsProperty) && src.charCodeAt(i + 1) !== 61 ? T_OPERAND : T_PUNCT;
|
|
1889
|
+
i++;
|
|
1890
|
+
continue;
|
|
1891
|
+
case 60:
|
|
1892
|
+
if (src.charCodeAt(i + 1) === 60 || src.charCodeAt(i + 1) === 61) {
|
|
1893
|
+
last = T_PUNCT;
|
|
1894
|
+
i += 2;
|
|
1895
|
+
} else if (!valueEnded(last, wordStart, wordEnd, wordIsProperty) && dialect.jsx && isJsxStart(i)) {
|
|
1896
|
+
i = jsxElement(i);
|
|
1897
|
+
last = T_OPERAND;
|
|
1898
|
+
} else {
|
|
1899
|
+
last = T_PUNCT;
|
|
1900
|
+
i++;
|
|
1901
|
+
}
|
|
1902
|
+
continue;
|
|
1903
|
+
case 35:
|
|
1904
|
+
if (isIdentStart(src.charCodeAt(i + 1))) {
|
|
1905
|
+
i = identEnd(i + 1);
|
|
1906
|
+
last = T_OPERAND;
|
|
1907
|
+
} else {
|
|
1908
|
+
last = T_PUNCT;
|
|
1909
|
+
i++;
|
|
1910
|
+
}
|
|
1911
|
+
continue;
|
|
1912
|
+
default:
|
|
1913
|
+
if (isDigit(c)) {
|
|
1914
|
+
i = numberEnd(i + 1);
|
|
1915
|
+
last = T_OPERAND;
|
|
1916
|
+
continue;
|
|
1917
|
+
}
|
|
1918
|
+
if (c < 128)
|
|
1919
|
+
last = T_PUNCT;
|
|
1920
|
+
i++;
|
|
1921
|
+
continue;
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
if (brace)
|
|
1925
|
+
fail("unterminated expression", i);
|
|
1926
|
+
if (stack.length > 0) {
|
|
1927
|
+
fail(stack[stack.length - 1] === M_TEMPLATE ? "unterminated template" : "unbalanced brackets", i);
|
|
1928
|
+
}
|
|
1929
|
+
return i;
|
|
1930
|
+
}
|
|
1931
|
+
function isDigit(c) {
|
|
1932
|
+
return c >= 48 && c <= 57;
|
|
1933
|
+
}
|
|
1934
|
+
function numberEnd(start) {
|
|
1935
|
+
let i = start;
|
|
1936
|
+
while (i < end) {
|
|
1937
|
+
const c = src.charCodeAt(i);
|
|
1938
|
+
if (isIdentPart(c) || c === 46)
|
|
1939
|
+
i++;
|
|
1940
|
+
else
|
|
1941
|
+
break;
|
|
1942
|
+
}
|
|
1943
|
+
return i;
|
|
1944
|
+
}
|
|
1945
|
+
function comment(start, stop, block) {
|
|
1946
|
+
if (nextAt < start)
|
|
1947
|
+
nextAt = indexOrEnd("@", start);
|
|
1948
|
+
while (nextAt < stop) {
|
|
1949
|
+
if (src.startsWith("@depends-on", nextAt) || src.startsWith("@reads", nextAt) || src.startsWith("@lists", nextAt) || src.startsWith("@glob-registry", nextAt)) {
|
|
1950
|
+
annotations(start, stop);
|
|
1951
|
+
nextAt = indexOrEnd("@", stop);
|
|
1952
|
+
break;
|
|
1953
|
+
}
|
|
1954
|
+
nextAt = indexOrEnd("@", nextAt + 1);
|
|
1955
|
+
}
|
|
1956
|
+
if (block && src.charCodeAt(start + 2) === 42) {
|
|
1957
|
+
if (nextImport < start)
|
|
1958
|
+
nextImport = indexOrEnd("import", start);
|
|
1959
|
+
while (nextImport < stop) {
|
|
1960
|
+
jsdocImport(start, nextImport, stop);
|
|
1961
|
+
nextImport = indexOrEnd("import", nextImport + 6);
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
function indexOrEnd(text, from) {
|
|
1966
|
+
const at = src.indexOf(text, from);
|
|
1967
|
+
return at === -1 ? src.length : at;
|
|
1968
|
+
}
|
|
1969
|
+
var BLANK = /^[ \t]*$/;
|
|
1970
|
+
function annotations(start, stop) {
|
|
1971
|
+
const lineStart = src.lastIndexOf(`
|
|
1972
|
+
`, start - 1) + 1;
|
|
1973
|
+
const prefix = src.slice(lineStart, start);
|
|
1974
|
+
const text = (BLANK.test(prefix) ? prefix : "x") + src.slice(start, stop);
|
|
1975
|
+
for (const [, list] of text.matchAll(DEPENDS_ON)) {
|
|
1976
|
+
out.dependsOn.push(...list.split(LIST_SEPARATOR).filter(Boolean));
|
|
1977
|
+
}
|
|
1978
|
+
for (const [, list] of text.matchAll(READS)) {
|
|
1979
|
+
out.reads.push(...list.split(LIST_SEPARATOR).filter(Boolean));
|
|
1980
|
+
}
|
|
1981
|
+
for (const [, list] of text.matchAll(LISTS)) {
|
|
1982
|
+
out.lists.push(...list.split(LIST_SEPARATOR).filter(Boolean));
|
|
1983
|
+
}
|
|
1984
|
+
if (GLOB_REGISTRY.test(text))
|
|
1985
|
+
out.registry = true;
|
|
1986
|
+
}
|
|
1987
|
+
var JSDOC_IMPORT_TYPE = /import\s*\(\s*(["'`])([^"'`\n]+)\1\s*\)/y;
|
|
1988
|
+
var JSDOC_IMPORT_TAG = /@import\s+([^;"'`]*?)\s*from\s*(["'])([^"'\n]+)\2/y;
|
|
1989
|
+
var JSDOC_LINE_STAR = /\n\s*\*/g;
|
|
1990
|
+
function inTagType(start, at) {
|
|
1991
|
+
let depth = 0;
|
|
1992
|
+
let outer = -1;
|
|
1993
|
+
for (let i = start;i < at; i++) {
|
|
1994
|
+
const c = src.charCodeAt(i);
|
|
1995
|
+
if (c === 123) {
|
|
1996
|
+
if (depth++ === 0)
|
|
1997
|
+
outer = i;
|
|
1998
|
+
} else if (c === 125 && depth > 0) {
|
|
1999
|
+
depth--;
|
|
2000
|
+
}
|
|
2001
|
+
}
|
|
2002
|
+
if (depth === 0)
|
|
2003
|
+
return false;
|
|
2004
|
+
let p = outer - 1;
|
|
2005
|
+
while (p > start && isSpace(src.charCodeAt(p)))
|
|
2006
|
+
p--;
|
|
2007
|
+
while (p > start && isIdentPart(src.charCodeAt(p)))
|
|
2008
|
+
p--;
|
|
2009
|
+
return src.charCodeAt(p) === 64 && p + 1 < outer;
|
|
2010
|
+
}
|
|
2011
|
+
function jsdocImport(start, at, stop) {
|
|
2012
|
+
if (src.charCodeAt(at - 1) === 64) {
|
|
2013
|
+
JSDOC_IMPORT_TAG.lastIndex = at - 1;
|
|
2014
|
+
const tag = JSDOC_IMPORT_TAG.exec(src);
|
|
2015
|
+
if (tag && JSDOC_IMPORT_TAG.lastIndex <= stop) {
|
|
2016
|
+
const clause = tag[1].replace(JSDOC_LINE_STAR, `
|
|
2017
|
+
`);
|
|
2018
|
+
const names = clause.includes("{") ? namedBindings(clause) : undefined;
|
|
2019
|
+
out.imports.push(names ? { specifier: tag[3], names, type: true } : { specifier: tag[3], type: true });
|
|
2020
|
+
}
|
|
2021
|
+
return;
|
|
2022
|
+
}
|
|
2023
|
+
if (isIdentPart(src.charCodeAt(at - 1)) || src.charCodeAt(at - 1) === 46) {
|
|
2024
|
+
return;
|
|
2025
|
+
}
|
|
2026
|
+
if (!inTagType(start, at))
|
|
2027
|
+
return;
|
|
2028
|
+
JSDOC_IMPORT_TYPE.lastIndex = at;
|
|
2029
|
+
const type = JSDOC_IMPORT_TYPE.exec(src);
|
|
2030
|
+
if (type && JSDOC_IMPORT_TYPE.lastIndex <= stop) {
|
|
2031
|
+
out.imports.push({ specifier: type[2], type: true });
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
function record(specifier, names, disk, type = false) {
|
|
2035
|
+
if (specifier === "")
|
|
2036
|
+
return;
|
|
2037
|
+
const ref = names ? { specifier, names } : { specifier };
|
|
2038
|
+
if (type)
|
|
2039
|
+
ref.type = true;
|
|
2040
|
+
out.imports.push(ref);
|
|
2041
|
+
if (disk && !type && DISK.has(specifier))
|
|
2042
|
+
out.readsDisk = true;
|
|
2043
|
+
}
|
|
2044
|
+
function isSpace(c) {
|
|
2045
|
+
return c === 32 || c >= 9 && c <= 13;
|
|
2046
|
+
}
|
|
2047
|
+
function isQuote(c) {
|
|
2048
|
+
return c === 39 || c === 34;
|
|
2049
|
+
}
|
|
2050
|
+
function importAt(j) {
|
|
2051
|
+
const k = skipTrivia(j);
|
|
2052
|
+
const c = src.charCodeAt(k);
|
|
2053
|
+
if (c === 40) {
|
|
2054
|
+
call(k, CALL_IMPORT);
|
|
2055
|
+
} else if (c === 46) {
|
|
2056
|
+
meta(k);
|
|
2057
|
+
} else if (isQuote(c)) {
|
|
2058
|
+
const e = stringEnd(k);
|
|
2059
|
+
if (e !== -1)
|
|
2060
|
+
record(src.slice(k + 1, e - 1), undefined, true);
|
|
2061
|
+
} else {
|
|
2062
|
+
declaration(k);
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
function declaration(k) {
|
|
2066
|
+
let clauseStart = k;
|
|
2067
|
+
let type = false;
|
|
2068
|
+
if (src.startsWith("type", k) && isSpace(src.charCodeAt(k + 4))) {
|
|
2069
|
+
clauseStart = k + 5;
|
|
2070
|
+
while (clauseStart < end && isSpace(src.charCodeAt(clauseStart))) {
|
|
2071
|
+
clauseStart++;
|
|
2072
|
+
}
|
|
2073
|
+
const next = skipTrivia(k + 4);
|
|
2074
|
+
type = !(isWord(next, "from") && isQuote(src.charCodeAt(skipTrivia(next + 4))));
|
|
2075
|
+
}
|
|
2076
|
+
let p = k;
|
|
2077
|
+
for (let steps = 0;steps < 256; steps++) {
|
|
2078
|
+
p = skipTrivia(p);
|
|
2079
|
+
if (p >= end)
|
|
2080
|
+
return;
|
|
2081
|
+
const c = src.charCodeAt(p);
|
|
2082
|
+
if (isIdentStart(c)) {
|
|
2083
|
+
const q = identEnd(p + 1);
|
|
2084
|
+
if (q - p === 4 && src.startsWith("from", p)) {
|
|
2085
|
+
const r = skipTrivia(q);
|
|
2086
|
+
if (isQuote(src.charCodeAt(r))) {
|
|
2087
|
+
const e = stringEnd(r);
|
|
2088
|
+
if (e !== -1) {
|
|
2089
|
+
record(src.slice(r + 1, e - 1), clauseNames(clauseStart, p), true, type);
|
|
2090
|
+
}
|
|
2091
|
+
return;
|
|
2092
|
+
}
|
|
2093
|
+
}
|
|
2094
|
+
p = q;
|
|
2095
|
+
} else if (c === 123) {
|
|
2096
|
+
p = braceEnd(p);
|
|
2097
|
+
if (p === -1)
|
|
2098
|
+
return;
|
|
2099
|
+
} else if (c === 42 || c === 44) {
|
|
2100
|
+
p++;
|
|
2101
|
+
} else {
|
|
2102
|
+
return;
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
function clauseNames(start, stop) {
|
|
2107
|
+
const clause = src.slice(start, stop);
|
|
2108
|
+
if (!clause.includes("{"))
|
|
2109
|
+
return;
|
|
2110
|
+
return namedBindings(clause.replace(COMMENTS2, ""));
|
|
2111
|
+
}
|
|
2112
|
+
var COMMENTS2 = /\/\/.*$|\/\*[\s\S]*?\*\//gm;
|
|
2113
|
+
function braceEnd(open) {
|
|
2114
|
+
let p = open;
|
|
2115
|
+
let depth = 0;
|
|
2116
|
+
while (p < end) {
|
|
2117
|
+
const c = src.charCodeAt(p);
|
|
2118
|
+
if (c === 123) {
|
|
2119
|
+
depth++;
|
|
2120
|
+
p++;
|
|
2121
|
+
} else if (c === 125) {
|
|
2122
|
+
p++;
|
|
2123
|
+
if (--depth === 0)
|
|
2124
|
+
return p;
|
|
2125
|
+
} else if (isQuote(c)) {
|
|
2126
|
+
p = stringEnd(p);
|
|
2127
|
+
if (p === -1)
|
|
2128
|
+
return -1;
|
|
2129
|
+
} else if (c === 47 && src.charCodeAt(p + 1) === 47) {
|
|
2130
|
+
p = lineEnd(p + 2);
|
|
2131
|
+
} else if (c === 47 && src.charCodeAt(p + 1) === 42) {
|
|
2132
|
+
const e = src.indexOf("*/", p + 2);
|
|
2133
|
+
if (e === -1)
|
|
2134
|
+
return -1;
|
|
2135
|
+
p = e + 2;
|
|
2136
|
+
} else if (c === 59 || c === 40 || c === 96) {
|
|
2137
|
+
return -1;
|
|
2138
|
+
} else {
|
|
2139
|
+
p++;
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
return -1;
|
|
2143
|
+
}
|
|
2144
|
+
function exportAt(j) {
|
|
2145
|
+
let p = skipTrivia(j);
|
|
2146
|
+
let type = false;
|
|
2147
|
+
if (isWord(p, "type")) {
|
|
2148
|
+
const q = skipTrivia(p + 4);
|
|
2149
|
+
const c = src.charCodeAt(q);
|
|
2150
|
+
if (c === 123 || c === 42) {
|
|
2151
|
+
p = q;
|
|
2152
|
+
type = true;
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
const clauseStart = p;
|
|
2156
|
+
const c = src.charCodeAt(p);
|
|
2157
|
+
let q;
|
|
2158
|
+
if (c === 123) {
|
|
2159
|
+
q = braceEnd(p);
|
|
2160
|
+
if (q === -1)
|
|
2161
|
+
return;
|
|
2162
|
+
} else if (c === 42) {
|
|
2163
|
+
q = skipTrivia(p + 1);
|
|
2164
|
+
if (isWord(q, "as")) {
|
|
2165
|
+
q = skipTrivia(q + 2);
|
|
2166
|
+
const n = src.charCodeAt(q);
|
|
2167
|
+
if (isQuote(n))
|
|
2168
|
+
q = stringEnd(q);
|
|
2169
|
+
else if (isIdentStart(n))
|
|
2170
|
+
q = identEnd(q + 1);
|
|
2171
|
+
else
|
|
2172
|
+
return;
|
|
2173
|
+
if (q === -1)
|
|
2174
|
+
return;
|
|
2175
|
+
}
|
|
2176
|
+
} else {
|
|
2177
|
+
return;
|
|
2178
|
+
}
|
|
2179
|
+
q = skipTrivia(q);
|
|
2180
|
+
if (!isWord(q, "from"))
|
|
2181
|
+
return;
|
|
2182
|
+
const r = skipTrivia(q + 4);
|
|
2183
|
+
if (!isQuote(src.charCodeAt(r)))
|
|
2184
|
+
return;
|
|
2185
|
+
const e = stringEnd(r);
|
|
2186
|
+
if (e === -1)
|
|
2187
|
+
return;
|
|
2188
|
+
record(src.slice(r + 1, e - 1), clauseNames(clauseStart, q), true, type);
|
|
2189
|
+
}
|
|
2190
|
+
function call(k, kind) {
|
|
2191
|
+
const a = skipTrivia(k + 1);
|
|
2192
|
+
const c = src.charCodeAt(a);
|
|
2193
|
+
if (c === 41)
|
|
2194
|
+
return;
|
|
2195
|
+
let e;
|
|
2196
|
+
if (isQuote(c)) {
|
|
2197
|
+
e = stringEnd(a);
|
|
2198
|
+
} else if (c === 96) {
|
|
2199
|
+
e = template(a + 1);
|
|
2200
|
+
if (templateOpen)
|
|
2201
|
+
e = -1;
|
|
2202
|
+
} else {
|
|
2203
|
+
e = -1;
|
|
2204
|
+
}
|
|
2205
|
+
const next = e === -1 ? -1 : src.charCodeAt(skipTrivia(e));
|
|
2206
|
+
if (next === 44 || next === 41) {
|
|
2207
|
+
record(src.slice(a + 1, e - 1), undefined, kind === CALL_IMPORT || kind === CALL_PROPERTY || kind === CALL_ACTUAL);
|
|
2208
|
+
} else if (kind !== CALL_PROPERTY && !isSignature(k, a)) {
|
|
2209
|
+
out.nonLiteralImport = true;
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
function isSignature(k, a) {
|
|
2213
|
+
if (isIdentStart(src.charCodeAt(a))) {
|
|
2214
|
+
const p = skipTrivia(identEnd(a + 1));
|
|
2215
|
+
const c = src.charCodeAt(p);
|
|
2216
|
+
if (c === 58 || c === 63 && src.charCodeAt(skipTrivia(p + 1)) === 58) {
|
|
2217
|
+
return true;
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
const close = parenEnd(k);
|
|
2221
|
+
return close !== -1 && src.charCodeAt(skipTrivia(close)) === 123;
|
|
2222
|
+
}
|
|
2223
|
+
function parenEnd(k) {
|
|
2224
|
+
let depth = 0;
|
|
2225
|
+
let p = k;
|
|
2226
|
+
while (p < end) {
|
|
2227
|
+
const c = src.charCodeAt(p);
|
|
2228
|
+
if (c === 40 || c === 91 || c === 123) {
|
|
2229
|
+
depth++;
|
|
2230
|
+
} else if (c === 41 || c === 93 || c === 125) {
|
|
2231
|
+
if (--depth === 0)
|
|
2232
|
+
return p + 1;
|
|
2233
|
+
} else if (isQuote(c) || c === 96) {
|
|
2234
|
+
p = c === 96 ? templateEnd(p) : stringEnd(p);
|
|
2235
|
+
if (p === -1)
|
|
2236
|
+
return -1;
|
|
2237
|
+
continue;
|
|
2238
|
+
} else if (c === 47 && (src.charCodeAt(p + 1) === 47 || src.charCodeAt(p + 1) === 42)) {
|
|
2239
|
+
p = skipTrivia(p);
|
|
2240
|
+
continue;
|
|
2241
|
+
}
|
|
2242
|
+
p++;
|
|
2243
|
+
}
|
|
2244
|
+
return -1;
|
|
2245
|
+
}
|
|
2246
|
+
function requireAt(j, property) {
|
|
2247
|
+
const k = skipTrivia(j);
|
|
2248
|
+
if (src.charCodeAt(k) === 40)
|
|
2249
|
+
call(k, property ? CALL_PROPERTY : CALL_IMPORT);
|
|
2250
|
+
}
|
|
2251
|
+
function vi(j) {
|
|
2252
|
+
const p = skipTrivia(j);
|
|
2253
|
+
if (src.charCodeAt(p) !== 46)
|
|
2254
|
+
return;
|
|
2255
|
+
const q = skipTrivia(p + 1);
|
|
2256
|
+
const r = identEnd(q);
|
|
2257
|
+
const name = src.slice(q, r);
|
|
2258
|
+
if (!VI_CALLS.has(name))
|
|
2259
|
+
return;
|
|
2260
|
+
let s = skipTrivia(r);
|
|
2261
|
+
if (src.charCodeAt(s) === 60) {
|
|
2262
|
+
s = genericEnd(s);
|
|
2263
|
+
if (s === -1) {
|
|
2264
|
+
out.nonLiteralImport = true;
|
|
2265
|
+
return;
|
|
2266
|
+
}
|
|
2267
|
+
s = skipTrivia(s);
|
|
2268
|
+
}
|
|
2269
|
+
if (src.charCodeAt(s) === 40) {
|
|
2270
|
+
call(s, name === "importActual" ? CALL_ACTUAL : CALL_MOCK);
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
function meta(k) {
|
|
2274
|
+
const m = skipTrivia(k + 1);
|
|
2275
|
+
if (!isWord(m, "meta"))
|
|
2276
|
+
return;
|
|
2277
|
+
const p = skipTrivia(m + 4);
|
|
2278
|
+
if (src.charCodeAt(p) !== 46)
|
|
2279
|
+
return;
|
|
2280
|
+
const q = skipTrivia(p + 1);
|
|
2281
|
+
const r = identEnd(q);
|
|
2282
|
+
if (!isWordAt(q, r, "glob") && !isWordAt(q, r, "globEager"))
|
|
2283
|
+
return;
|
|
2284
|
+
let s = skipTrivia(r);
|
|
2285
|
+
if (src.charCodeAt(s) === 60) {
|
|
2286
|
+
s = genericEnd(s);
|
|
2287
|
+
if (s === -1) {
|
|
2288
|
+
out.nonLiteralGlob = true;
|
|
2289
|
+
return;
|
|
2290
|
+
}
|
|
2291
|
+
s = skipTrivia(s);
|
|
2292
|
+
}
|
|
2293
|
+
if (src.charCodeAt(s) === 40)
|
|
2294
|
+
globArgument(s);
|
|
2295
|
+
}
|
|
2296
|
+
function genericEnd(start) {
|
|
2297
|
+
let p = start;
|
|
2298
|
+
let depth = 0;
|
|
2299
|
+
while (p < end && p - start < 4096) {
|
|
2300
|
+
const c = src.charCodeAt(p);
|
|
2301
|
+
if (c === 60) {
|
|
2302
|
+
depth++;
|
|
2303
|
+
} else if (c === 62 && src.charCodeAt(p - 1) !== 61) {
|
|
2304
|
+
if (--depth === 0)
|
|
2305
|
+
return p + 1;
|
|
2306
|
+
} else if (isQuote(c)) {
|
|
2307
|
+
p = stringEnd(p);
|
|
2308
|
+
if (p === -1)
|
|
2309
|
+
return -1;
|
|
2310
|
+
continue;
|
|
2311
|
+
} else if (c === 96) {
|
|
2312
|
+
p = template(p + 1);
|
|
2313
|
+
if (templateOpen)
|
|
2314
|
+
return -1;
|
|
2315
|
+
continue;
|
|
2316
|
+
} else if (c === 59 && depth === 0) {
|
|
2317
|
+
return -1;
|
|
2318
|
+
}
|
|
2319
|
+
p++;
|
|
2320
|
+
}
|
|
2321
|
+
return -1;
|
|
2322
|
+
}
|
|
2323
|
+
function literalEnd(a) {
|
|
2324
|
+
const c = src.charCodeAt(a);
|
|
2325
|
+
if (isQuote(c))
|
|
2326
|
+
return stringEnd(a);
|
|
2327
|
+
if (c === 96) {
|
|
2328
|
+
const e = template(a + 1);
|
|
2329
|
+
return templateOpen ? -1 : e;
|
|
2330
|
+
}
|
|
2331
|
+
return -1;
|
|
2332
|
+
}
|
|
2333
|
+
function globArgument(k) {
|
|
2334
|
+
let a = skipTrivia(k + 1);
|
|
2335
|
+
const patterns = [];
|
|
2336
|
+
let e;
|
|
2337
|
+
if (src.charCodeAt(a) === 91) {
|
|
2338
|
+
a = skipTrivia(a + 1);
|
|
2339
|
+
for (;; ) {
|
|
2340
|
+
if (src.charCodeAt(a) === 93) {
|
|
2341
|
+
e = a + 1;
|
|
2342
|
+
break;
|
|
2343
|
+
}
|
|
2344
|
+
const f = literalEnd(a);
|
|
2345
|
+
if (f === -1) {
|
|
2346
|
+
e = -1;
|
|
2347
|
+
break;
|
|
2348
|
+
}
|
|
2349
|
+
patterns.push(src.slice(a + 1, f - 1));
|
|
2350
|
+
a = skipTrivia(f);
|
|
2351
|
+
const c = src.charCodeAt(a);
|
|
2352
|
+
if (c === 44) {
|
|
2353
|
+
a = skipTrivia(a + 1);
|
|
2354
|
+
} else if (c !== 93) {
|
|
2355
|
+
e = -1;
|
|
2356
|
+
break;
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
} else {
|
|
2360
|
+
e = literalEnd(a);
|
|
2361
|
+
if (e !== -1)
|
|
2362
|
+
patterns.push(src.slice(a + 1, e - 1));
|
|
2363
|
+
}
|
|
2364
|
+
const next = e === -1 ? -1 : src.charCodeAt(skipTrivia(e));
|
|
2365
|
+
if (next === 44 || next === 41)
|
|
2366
|
+
out.globs.push(...patterns);
|
|
2367
|
+
else
|
|
2368
|
+
out.nonLiteralGlob = true;
|
|
2369
|
+
}
|
|
2370
|
+
function goto(j) {
|
|
2371
|
+
const k = skipTrivia(j);
|
|
2372
|
+
if (src.charCodeAt(k) !== 40)
|
|
2373
|
+
return;
|
|
2374
|
+
const a = skipTrivia(k + 1);
|
|
2375
|
+
let p = a;
|
|
2376
|
+
let depth = 0;
|
|
2377
|
+
while (p < end) {
|
|
2378
|
+
const c = src.charCodeAt(p);
|
|
2379
|
+
if ((c === 44 || c === 41) && depth === 0)
|
|
2380
|
+
break;
|
|
2381
|
+
if (c === 40 || c === 91 || c === 123) {
|
|
2382
|
+
depth++;
|
|
2383
|
+
} else if (c === 41 || c === 93 || c === 125) {
|
|
2384
|
+
depth--;
|
|
2385
|
+
} else if (isQuote(c) || c === 96) {
|
|
2386
|
+
const e = c === 96 ? templateEnd(p) : stringEnd(p);
|
|
2387
|
+
if (e === -1)
|
|
2388
|
+
return;
|
|
2389
|
+
p = e;
|
|
2390
|
+
continue;
|
|
2391
|
+
}
|
|
2392
|
+
p++;
|
|
2393
|
+
}
|
|
2394
|
+
out.gotos.push(src.slice(a, p).trim());
|
|
2395
|
+
}
|
|
2396
|
+
function templateEnd(p) {
|
|
2397
|
+
let q = template(p + 1);
|
|
2398
|
+
while (templateOpen) {
|
|
2399
|
+
let depth = 1;
|
|
2400
|
+
while (q < end && depth > 0) {
|
|
2401
|
+
const c = src.charCodeAt(q);
|
|
2402
|
+
if (c === 123)
|
|
2403
|
+
depth++;
|
|
2404
|
+
else if (c === 125)
|
|
2405
|
+
depth--;
|
|
2406
|
+
else if (isQuote(c)) {
|
|
2407
|
+
q = stringEnd(q);
|
|
2408
|
+
if (q === -1)
|
|
2409
|
+
return -1;
|
|
2410
|
+
continue;
|
|
2411
|
+
} else if (c === 96) {
|
|
2412
|
+
q = templateEnd(q);
|
|
2413
|
+
if (q === -1)
|
|
2414
|
+
return -1;
|
|
2415
|
+
continue;
|
|
2416
|
+
}
|
|
2417
|
+
q++;
|
|
2418
|
+
}
|
|
2419
|
+
if (depth > 0)
|
|
2420
|
+
return -1;
|
|
2421
|
+
q = template(q);
|
|
2422
|
+
}
|
|
2423
|
+
return q;
|
|
2424
|
+
}
|
|
2425
|
+
function bun(j) {
|
|
2426
|
+
if (src.charCodeAt(j) !== 46)
|
|
2427
|
+
return;
|
|
2428
|
+
if (src.startsWith("file", j + 1) || src.startsWith("Glob", j + 1) || src.startsWith("spawn", j + 1)) {
|
|
2429
|
+
out.readsDisk = true;
|
|
2430
|
+
}
|
|
2431
|
+
}
|
|
2432
|
+
function isJsxStart(i) {
|
|
2433
|
+
const c = src.charCodeAt(i + 1);
|
|
2434
|
+
if (c === 62)
|
|
2435
|
+
return true;
|
|
2436
|
+
if (!isIdentStart(c))
|
|
2437
|
+
return false;
|
|
2438
|
+
if (!dialect.tsx)
|
|
2439
|
+
return true;
|
|
2440
|
+
const p = skipTrivia(identEnd(i + 2));
|
|
2441
|
+
return src.charCodeAt(p) !== 44 && !isWord(p, "extends");
|
|
2442
|
+
}
|
|
2443
|
+
function jsxNameEnd(start) {
|
|
2444
|
+
let p = start;
|
|
2445
|
+
while (p < end) {
|
|
2446
|
+
const c = src.charCodeAt(p);
|
|
2447
|
+
if (isIdentPart(c) || c === 45 || c === 46 || c === 58)
|
|
2448
|
+
p++;
|
|
2449
|
+
else
|
|
2450
|
+
break;
|
|
2451
|
+
}
|
|
2452
|
+
return p;
|
|
2453
|
+
}
|
|
2454
|
+
function jsxElement(i) {
|
|
2455
|
+
let p = i + 1;
|
|
2456
|
+
if (src.charCodeAt(p) === 62)
|
|
2457
|
+
return jsxChildren(p + 1);
|
|
2458
|
+
p = jsxNameEnd(p);
|
|
2459
|
+
if (dialect.tsx) {
|
|
2460
|
+
const q = skipTrivia(p);
|
|
2461
|
+
if (src.charCodeAt(q) === 60) {
|
|
2462
|
+
p = genericEnd(q);
|
|
2463
|
+
if (p === -1)
|
|
2464
|
+
fail("unterminated JSX type arguments", q);
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
for (;; ) {
|
|
2468
|
+
p = skipTrivia(p);
|
|
2469
|
+
if (p >= end)
|
|
2470
|
+
fail("unterminated JSX", i);
|
|
2471
|
+
const c = src.charCodeAt(p);
|
|
2472
|
+
if (c === 47 && src.charCodeAt(p + 1) === 62)
|
|
2473
|
+
return p + 2;
|
|
2474
|
+
if (c === 62)
|
|
2475
|
+
return jsxChildren(p + 1);
|
|
2476
|
+
if (c === 123) {
|
|
2477
|
+
p = code(p + 1, true);
|
|
2478
|
+
continue;
|
|
2479
|
+
}
|
|
2480
|
+
if (!isIdentStart(c))
|
|
2481
|
+
fail(`bad JSX attribute in <> at ${i}:`, p);
|
|
2482
|
+
p = skipTrivia(jsxNameEnd(p));
|
|
2483
|
+
if (src.charCodeAt(p) !== 61)
|
|
2484
|
+
continue;
|
|
2485
|
+
p = skipTrivia(p + 1);
|
|
2486
|
+
const v = src.charCodeAt(p);
|
|
2487
|
+
if (isQuote(v)) {
|
|
2488
|
+
const e = src.indexOf(src[p], p + 1);
|
|
2489
|
+
if (e === -1 || e >= end)
|
|
2490
|
+
fail("unterminated JSX attribute", p);
|
|
2491
|
+
p = e + 1;
|
|
2492
|
+
} else if (v === 123) {
|
|
2493
|
+
p = code(p + 1, true);
|
|
2494
|
+
} else if (v === 60) {
|
|
2495
|
+
p = jsxElement(p);
|
|
2496
|
+
} else {
|
|
2497
|
+
fail("bad JSX attribute", p);
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
function jsxChildren(start) {
|
|
2502
|
+
let p = start;
|
|
2503
|
+
while (p < end) {
|
|
2504
|
+
const c = src.charCodeAt(p);
|
|
2505
|
+
if (c === 60) {
|
|
2506
|
+
if (src.charCodeAt(p + 1) === 47) {
|
|
2507
|
+
const e = src.indexOf(">", p);
|
|
2508
|
+
if (e === -1 || e >= end)
|
|
2509
|
+
break;
|
|
2510
|
+
return e + 1;
|
|
2511
|
+
}
|
|
2512
|
+
p = jsxElement(p);
|
|
2513
|
+
} else if (c === 123) {
|
|
2514
|
+
p = code(p + 1, true);
|
|
2515
|
+
} else {
|
|
2516
|
+
p++;
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
return fail("unterminated JSX", p);
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
// src/host.ts
|
|
2523
|
+
function hostOf(path) {
|
|
2524
|
+
const dot = path.lastIndexOf(".");
|
|
2525
|
+
const ext = path.slice(dot + 1);
|
|
2526
|
+
return ext === "svelte" || ext === "vue" || ext === "astro" || ext === "html" ? ext : undefined;
|
|
2527
|
+
}
|
|
2528
|
+
var TAG_REF = /^<(?:script|link|iframe)\b[^>]*\b(?:src|href)=(?:"([^"]+)"|'([^']+)'|([^\s"'=<>`]+))/;
|
|
2529
|
+
var GENERICS = /\sgenerics=(["'])/;
|
|
2530
|
+
var LANG_JSX = /\slang=["']?(?:jsx|tsx)\b/;
|
|
2531
|
+
var LOWERCASE = /[a-z]/;
|
|
2532
|
+
var FENCE_OPEN = /^\s*---[ \t]*\r?\n/;
|
|
2533
|
+
var FENCE_CLOSE = /^---[ \t]*$/m;
|
|
2534
|
+
function isTag(src, i, name) {
|
|
2535
|
+
for (let k = 0;k < name.length; k++) {
|
|
2536
|
+
if ((src.charCodeAt(i + 1 + k) | 32) !== name.charCodeAt(k))
|
|
2537
|
+
return false;
|
|
2538
|
+
}
|
|
2539
|
+
const c = src.charCodeAt(i + 1 + name.length);
|
|
2540
|
+
return c === 62 || c === 47 || c === 32 || c >= 9 && c <= 13;
|
|
2541
|
+
}
|
|
2542
|
+
function tagEnd(src, i) {
|
|
2543
|
+
let j = i + 1;
|
|
2544
|
+
while (j < src.length) {
|
|
2545
|
+
const c = src.charCodeAt(j);
|
|
2546
|
+
if (c === 62)
|
|
2547
|
+
return j + 1;
|
|
2548
|
+
if (c === 34 || c === 39) {
|
|
2549
|
+
const e = src.indexOf(src[j], j + 1);
|
|
2550
|
+
if (e === -1)
|
|
2551
|
+
break;
|
|
2552
|
+
j = e + 1;
|
|
2553
|
+
} else {
|
|
2554
|
+
j++;
|
|
2555
|
+
}
|
|
2556
|
+
}
|
|
2557
|
+
throw new LexError(`unterminated tag at ${i}`);
|
|
2558
|
+
}
|
|
2559
|
+
function closeTag(src, from, name) {
|
|
2560
|
+
const lower = src.indexOf(`</${name}`, from);
|
|
2561
|
+
if (lower !== -1)
|
|
2562
|
+
return lower;
|
|
2563
|
+
const any = src.slice(from).search(new RegExp(`</${name}`, "i"));
|
|
2564
|
+
if (any === -1)
|
|
2565
|
+
throw new LexError(`unterminated <${name}> at ${from}`);
|
|
2566
|
+
return from + any;
|
|
2567
|
+
}
|
|
2568
|
+
function indexOrEnd2(src, text, from) {
|
|
2569
|
+
const at = src.indexOf(text, from);
|
|
2570
|
+
return at === -1 ? src.length : at;
|
|
2571
|
+
}
|
|
2572
|
+
function lexHost(src, out, host) {
|
|
2573
|
+
const n = src.length;
|
|
2574
|
+
let i = 0;
|
|
2575
|
+
if (host === "astro")
|
|
2576
|
+
i = frontmatter(src);
|
|
2577
|
+
let nextTag = -1;
|
|
2578
|
+
let nextBrace = host === "svelte" ? -1 : n;
|
|
2579
|
+
while (i < n) {
|
|
2580
|
+
if (nextTag < i)
|
|
2581
|
+
nextTag = indexOrEnd2(src, "<", i);
|
|
2582
|
+
if (nextBrace < i)
|
|
2583
|
+
nextBrace = indexOrEnd2(src, "{", i);
|
|
2584
|
+
i = Math.min(nextTag, nextBrace);
|
|
2585
|
+
if (i >= n)
|
|
2586
|
+
break;
|
|
2587
|
+
const c = src.charCodeAt(i);
|
|
2588
|
+
if (c === 123) {
|
|
2589
|
+
let j = i + 1;
|
|
2590
|
+
const sigil = src.charCodeAt(j);
|
|
2591
|
+
if (sigil === 35 || sigil === 58 || sigil === 64 || sigil === 47 && LOWERCASE.test(src[j + 1] ?? "")) {
|
|
2592
|
+
j++;
|
|
2593
|
+
}
|
|
2594
|
+
i = lexExpression(j, n);
|
|
2595
|
+
continue;
|
|
2596
|
+
}
|
|
2597
|
+
if (src.startsWith("<!--", i)) {
|
|
2598
|
+
const e = src.indexOf("-->", i + 4);
|
|
2599
|
+
if (e === -1)
|
|
2600
|
+
throw new LexError(`unterminated comment at ${i}`);
|
|
2601
|
+
i = e + 3;
|
|
2602
|
+
} else if (isTag(src, i, "script")) {
|
|
2603
|
+
const open = tagEnd(src, i);
|
|
2604
|
+
const tag = src.slice(i, open);
|
|
2605
|
+
if (host === "html" || host === "vue")
|
|
2606
|
+
ref(tag, out);
|
|
2607
|
+
const generics = tag.match(GENERICS);
|
|
2608
|
+
if (generics?.index !== undefined) {
|
|
2609
|
+
const start = i + generics.index + generics[0].length;
|
|
2610
|
+
const stop = src.indexOf(generics[1], start);
|
|
2611
|
+
if (stop !== -1 && stop < open)
|
|
2612
|
+
lexRange(start, stop, TS);
|
|
2613
|
+
}
|
|
2614
|
+
const close = closeTag(src, open, "script");
|
|
2615
|
+
const dialect = LANG_JSX.test(tag) ? TSX : TS;
|
|
2616
|
+
lexRange(open, close, dialect);
|
|
2617
|
+
i = close + 8;
|
|
2618
|
+
} else if (isTag(src, i, "style")) {
|
|
2619
|
+
i = closeTag(src, tagEnd(src, i), "style") + 7;
|
|
2620
|
+
} else if (host === "html" && (isTag(src, i, "link") || isTag(src, i, "iframe"))) {
|
|
2621
|
+
const open = tagEnd(src, i);
|
|
2622
|
+
ref(src.slice(i, open), out);
|
|
2623
|
+
i = open;
|
|
2624
|
+
} else {
|
|
2625
|
+
i++;
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
function ref(tag, out) {
|
|
2630
|
+
const match = TAG_REF.exec(tag);
|
|
2631
|
+
const value = match && (match[1] ?? match[2] ?? match[3]);
|
|
2632
|
+
if (value && !URL_REF.test(value)) {
|
|
2633
|
+
out.imports.push({
|
|
2634
|
+
specifier: RELATIVE.test(value) ? value : `./${value}`
|
|
2635
|
+
});
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
function frontmatter(src) {
|
|
2639
|
+
const start = FENCE_OPEN.exec(src);
|
|
2640
|
+
if (!start)
|
|
2641
|
+
return 0;
|
|
2642
|
+
const from = start[0].length;
|
|
2643
|
+
const close = src.slice(from).search(FENCE_CLOSE);
|
|
2644
|
+
if (close === -1)
|
|
2645
|
+
throw new LexError("unterminated frontmatter");
|
|
2646
|
+
lexRange(from, from + close, TS);
|
|
2647
|
+
return from + close + 3;
|
|
2648
|
+
}
|
|
2649
|
+
|
|
2650
|
+
// src/scan-regex.ts
|
|
2651
|
+
var COMMENTS3 = /\/\/.*$|\/\*[\s\S]*?\*\//gm;
|
|
2652
|
+
var QUOTED = /["']([^"']+)["']/g;
|
|
2653
|
+
var STATIC_IMPORT = /(?:^|[\n;>])\s*(?:import|export)\s+(?:type\s+)?([^;"'`]*?)\s*from\s*["']([^"'\n]+)["']/g;
|
|
2654
|
+
var SIDE_EFFECT_IMPORT = /(?:^|[\n;>])\s*import\s*["']([^"'\n]+)["']/g;
|
|
2655
|
+
var CALL_WITH_LITERAL = /\b(?:import|require|vi\.(?:mock|doMock|unmock|importActual|importMock))\(\s*(["'`])([^"'`$\n]+)\1\s*[,)]/g;
|
|
2656
|
+
var NON_LITERAL_CALL = /(?<![.\w])(?:import|require)\(\s*(?!["'`])[^)\s]/;
|
|
2657
|
+
var TEMPLATE_CALL = /(?<![.\w])(?:import|require|vi\.(?:mock|doMock|unmock|importActual|importMock))\(\s*`[^`]*\$\{/;
|
|
2658
|
+
var GLOB_CALL = /\bimport\.meta\.glob(?:<(?:[^<>]|<[^<>]*>)*>)?\(\s*(\[[^\]]*\]|["'][^"']*["'])/g;
|
|
2659
|
+
var GLOB_CALL_ANY = /\bimport\.meta\.glob[<(]/g;
|
|
2660
|
+
var GOTO = /\.goto\(\s*([^,)]*)/g;
|
|
2661
|
+
var HTML_REF = /<(?:script|link|iframe)\b[^>]*\b(?:src|href)=["']([^"']+)["']/g;
|
|
2662
|
+
var READS_DISK = /\b(?:from|import|require|importActual)\s*\(?\s*["'](?:node:)?(?:fs|fs\/promises|child_process)["']|\bBun\.(?:file|Glob|spawn)/;
|
|
2663
|
+
function extractRegex(source, html) {
|
|
2664
|
+
const result = {
|
|
2665
|
+
imports: [],
|
|
2666
|
+
globs: [],
|
|
2667
|
+
gotos: [],
|
|
2668
|
+
dependsOn: [],
|
|
2669
|
+
reads: [],
|
|
2670
|
+
lists: [],
|
|
2671
|
+
unknown: []
|
|
2672
|
+
};
|
|
2673
|
+
if (html) {
|
|
2674
|
+
for (const [, ref] of source.matchAll(HTML_REF)) {
|
|
2675
|
+
if (!URL_REF.test(ref)) {
|
|
2676
|
+
result.imports.push({
|
|
2677
|
+
specifier: RELATIVE.test(ref) ? ref : `./${ref}`
|
|
2678
|
+
});
|
|
2679
|
+
}
|
|
2680
|
+
}
|
|
2681
|
+
}
|
|
2682
|
+
for (const [, clause, specifier] of source.matchAll(STATIC_IMPORT)) {
|
|
2683
|
+
const names = namedBindings(clause.replace(COMMENTS3, ""));
|
|
2684
|
+
result.imports.push(names ? { specifier, names } : { specifier });
|
|
2685
|
+
}
|
|
2686
|
+
for (const [, specifier] of source.matchAll(SIDE_EFFECT_IMPORT)) {
|
|
2687
|
+
result.imports.push({ specifier });
|
|
2688
|
+
}
|
|
2689
|
+
const calls = source.includes("import(") || source.includes("require(") || source.includes("vi.");
|
|
2690
|
+
for (const [, , specifier] of calls ? source.matchAll(CALL_WITH_LITERAL) : []) {
|
|
2691
|
+
result.imports.push({ specifier });
|
|
2692
|
+
}
|
|
2693
|
+
if (calls && (NON_LITERAL_CALL.test(source) || TEMPLATE_CALL.test(source))) {
|
|
2694
|
+
result.unknown.push("non-literal dynamic import");
|
|
2695
|
+
}
|
|
2696
|
+
const globbing = source.includes("import.meta.glob");
|
|
2697
|
+
const globs = globbing ? [...source.matchAll(GLOB_CALL)] : [];
|
|
2698
|
+
if (globbing && globs.length !== [...source.matchAll(GLOB_CALL_ANY)].length) {
|
|
2699
|
+
result.unknown.push("non-literal glob import");
|
|
2700
|
+
}
|
|
2701
|
+
for (const [, argument] of GLOB_REGISTRY.test(source) ? [] : globs) {
|
|
2702
|
+
for (const [, glob] of argument.matchAll(QUOTED)) {
|
|
2703
|
+
if (glob.startsWith("!"))
|
|
2704
|
+
continue;
|
|
2705
|
+
result.globs.push(glob.replace(QUERY, ""));
|
|
2706
|
+
}
|
|
2707
|
+
}
|
|
2708
|
+
if (source.includes(".goto(")) {
|
|
2709
|
+
for (const [, argument] of source.matchAll(GOTO)) {
|
|
2710
|
+
result.gotos.push(argument.trim());
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
result.dependsOn = [
|
|
2714
|
+
...source.includes("@depends-on") ? source.matchAll(DEPENDS_ON) : []
|
|
2715
|
+
].flatMap(([, list]) => list.split(LIST_SEPARATOR).filter(Boolean));
|
|
2716
|
+
result.reads = [
|
|
2717
|
+
...source.includes("@reads") ? source.matchAll(READS) : []
|
|
2718
|
+
].flatMap(([, list]) => list.split(LIST_SEPARATOR).filter(Boolean));
|
|
2719
|
+
result.lists = [
|
|
2720
|
+
...source.includes("@lists") ? source.matchAll(LISTS) : []
|
|
2721
|
+
].flatMap(([, list]) => list.split(LIST_SEPARATOR).filter(Boolean));
|
|
2722
|
+
const disk = source.includes("fs") || source.includes("child_process") || source.includes("Bun.");
|
|
2723
|
+
if (disk && READS_DISK.test(source) && result.dependsOn.length + result.reads.length + result.lists.length === 0) {
|
|
2724
|
+
result.unknown.push(UNDECLARED_READS);
|
|
2725
|
+
}
|
|
2726
|
+
return result;
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
// src/scan.ts
|
|
2730
|
+
var SCANNED = /\.(?:svelte|vue|astro|html|[cm]?[jt]sx?)$/;
|
|
2731
|
+
function formatOf(path) {
|
|
2732
|
+
const host = hostOf(path);
|
|
2733
|
+
if (host)
|
|
2734
|
+
return host;
|
|
2735
|
+
const dialect = dialectOf(path);
|
|
2736
|
+
if (dialect === TSX)
|
|
2737
|
+
return "tsx";
|
|
2738
|
+
return dialect === JS ? "js" : "ts";
|
|
2739
|
+
}
|
|
2740
|
+
function extract(format, source) {
|
|
2741
|
+
const lexed = createLexed();
|
|
2742
|
+
let extraction;
|
|
2743
|
+
try {
|
|
2744
|
+
openFile(source, lexed);
|
|
2745
|
+
if (isHost(format))
|
|
2746
|
+
lexHost(source, lexed, format);
|
|
2747
|
+
else
|
|
2748
|
+
lexRange(0, source.length, DIALECTS[format]);
|
|
2749
|
+
extraction = fromLexed(lexed);
|
|
2750
|
+
} catch (error) {
|
|
2751
|
+
const fallback = error instanceof LexError ? error.message : `lexer error: ${error}`;
|
|
2752
|
+
extraction = { ...extractRegex(source, format === "html"), fallback };
|
|
2753
|
+
}
|
|
2754
|
+
if (!isHost(format)) {
|
|
2755
|
+
const barrel = parseReExports(source);
|
|
2756
|
+
if (barrel)
|
|
2757
|
+
extraction.barrel = barrel;
|
|
2758
|
+
}
|
|
2759
|
+
return detach(extraction);
|
|
2760
|
+
}
|
|
2761
|
+
function stringsOf(format, source) {
|
|
2762
|
+
const lexed = createLexed();
|
|
2763
|
+
const strings = [];
|
|
2764
|
+
lexed.strings = strings;
|
|
2765
|
+
try {
|
|
2766
|
+
openFile(source, lexed);
|
|
2767
|
+
if (isHost(format))
|
|
2768
|
+
lexHost(source, lexed, format);
|
|
2769
|
+
else
|
|
2770
|
+
lexRange(0, source.length, DIALECTS[format]);
|
|
2771
|
+
} catch {
|
|
2772
|
+
return;
|
|
2773
|
+
}
|
|
2774
|
+
return strings;
|
|
2775
|
+
}
|
|
2776
|
+
function own(text) {
|
|
2777
|
+
return JSON.parse(JSON.stringify(text));
|
|
2778
|
+
}
|
|
2779
|
+
function detach(extraction) {
|
|
2780
|
+
const list = (texts) => texts.map(own);
|
|
2781
|
+
const { imports, barrel, fallback } = extraction;
|
|
2782
|
+
const detached = {
|
|
2783
|
+
imports: imports.map(({ specifier, names, type }) => {
|
|
2784
|
+
const ref = names ? { specifier: own(specifier), names: list(names) } : { specifier: own(specifier) };
|
|
2785
|
+
if (type)
|
|
2786
|
+
ref.type = true;
|
|
2787
|
+
return ref;
|
|
2788
|
+
}),
|
|
2789
|
+
globs: list(extraction.globs),
|
|
2790
|
+
gotos: list(extraction.gotos),
|
|
2791
|
+
dependsOn: list(extraction.dependsOn),
|
|
2792
|
+
reads: list(extraction.reads),
|
|
2793
|
+
lists: list(extraction.lists),
|
|
2794
|
+
unknown: extraction.unknown
|
|
2795
|
+
};
|
|
2796
|
+
if (fallback !== undefined)
|
|
2797
|
+
detached.fallback = fallback;
|
|
2798
|
+
if (barrel) {
|
|
2799
|
+
detached.barrel = {
|
|
2800
|
+
names: new Map([...barrel.names].map(([name, from]) => [own(name), own(from)])),
|
|
2801
|
+
stars: list(barrel.stars)
|
|
2802
|
+
};
|
|
2803
|
+
}
|
|
2804
|
+
return detached;
|
|
2805
|
+
}
|
|
2806
|
+
function finish(path, extraction, { gotos = false } = {}) {
|
|
2807
|
+
const { globs } = extraction;
|
|
2808
|
+
const dir = globs.length > 0 ? posix2.dirname(path) : "";
|
|
2809
|
+
const result = {
|
|
2810
|
+
imports: extraction.imports,
|
|
2811
|
+
globs: globs.map((glob) => posix2.join(dir, glob)),
|
|
2812
|
+
gotos: gotos ? extraction.gotos : [],
|
|
2813
|
+
dependsOn: extraction.dependsOn,
|
|
2814
|
+
reads: extraction.reads,
|
|
2815
|
+
lists: extraction.lists,
|
|
2816
|
+
unknown: extraction.unknown
|
|
2817
|
+
};
|
|
2818
|
+
if (extraction.fallback !== undefined)
|
|
2819
|
+
result.fallback = extraction.fallback;
|
|
2820
|
+
return result;
|
|
2821
|
+
}
|
|
2822
|
+
var DIALECTS = { ts: TS, tsx: TSX, js: JS };
|
|
2823
|
+
var isHost = (format) => format !== "ts" && format !== "tsx" && format !== "js";
|
|
2824
|
+
function fromLexed(lexed) {
|
|
2825
|
+
const unknown = [];
|
|
2826
|
+
if (lexed.nonLiteralImport)
|
|
2827
|
+
unknown.push("non-literal dynamic import");
|
|
2828
|
+
if (lexed.nonLiteralGlob)
|
|
2829
|
+
unknown.push("non-literal glob import");
|
|
2830
|
+
const globs = [];
|
|
2831
|
+
for (const glob of lexed.registry ? [] : lexed.globs) {
|
|
2832
|
+
if (glob.startsWith("!"))
|
|
2833
|
+
continue;
|
|
2834
|
+
globs.push(glob.replace(QUERY, ""));
|
|
2835
|
+
}
|
|
2836
|
+
const declared = lexed.dependsOn.length + lexed.reads.length + lexed.lists.length;
|
|
2837
|
+
if (lexed.readsDisk && declared === 0) {
|
|
2838
|
+
unknown.push(UNDECLARED_READS);
|
|
2839
|
+
}
|
|
2840
|
+
return {
|
|
2841
|
+
imports: lexed.imports,
|
|
2842
|
+
globs,
|
|
2843
|
+
gotos: lexed.gotos,
|
|
2844
|
+
dependsOn: lexed.dependsOn,
|
|
2845
|
+
reads: lexed.reads,
|
|
2846
|
+
lists: lexed.lists,
|
|
2847
|
+
unknown
|
|
2848
|
+
};
|
|
2849
|
+
}
|
|
2850
|
+
var STRING_LITERAL = /^["'`]([^"'`$]*)["'`]$/;
|
|
2851
|
+
var STATIC_PATH_TEMPLATE = /^`([^`$?#]*)[?#][^`]*`$/;
|
|
2852
|
+
function gotoUrl(argument) {
|
|
2853
|
+
return STRING_LITERAL.exec(argument)?.[1] ?? STATIC_PATH_TEMPLATE.exec(argument)?.[1];
|
|
2854
|
+
}
|
|
2855
|
+
|
|
2856
|
+
// src/graph.ts
|
|
2857
|
+
var leaf = () => ({
|
|
2858
|
+
files: new Set,
|
|
2859
|
+
patterns: [],
|
|
2860
|
+
reads: [],
|
|
2861
|
+
unknown: []
|
|
2862
|
+
});
|
|
2863
|
+
var LEADING_DOT_SLASH2 = /^\.\//;
|
|
2864
|
+
var JS_SPECIFIER = /\.[cm]?jsx?$/;
|
|
2865
|
+
var DECLARATION = /\.d(?:\.[^/.]+)?\.[cm]?ts$/;
|
|
2866
|
+
function addPattern(deps, pattern, kind) {
|
|
2867
|
+
deps.patterns.push(pattern);
|
|
2868
|
+
if (kind === "glob")
|
|
2869
|
+
return;
|
|
2870
|
+
deps.patternKinds ??= new Map;
|
|
2871
|
+
deps.patternKinds.set(pattern, kind);
|
|
2872
|
+
}
|
|
2873
|
+
var NON_LITERAL_GOTO = "non-literal goto(";
|
|
2874
|
+
var HERMETIC_CAUSES = new Set([
|
|
2875
|
+
UNDECLARED_READS,
|
|
2876
|
+
"non-literal dynamic import"
|
|
2877
|
+
]);
|
|
2878
|
+
function sideEffectful(owning) {
|
|
2879
|
+
const sideEffects = owning?.[1].sideEffects;
|
|
2880
|
+
if (sideEffects === false)
|
|
2881
|
+
return () => false;
|
|
2882
|
+
if (!Array.isArray(sideEffects))
|
|
2883
|
+
return () => true;
|
|
2884
|
+
const dir = owning?.[0] ?? ".";
|
|
2885
|
+
return matcher(sideEffects.map((glob) => {
|
|
2886
|
+
if (!glob.includes("/"))
|
|
2887
|
+
return `**/${glob}`;
|
|
2888
|
+
const path = glob.replace(LEADING_DOT_SLASH2, "");
|
|
2889
|
+
return dir === "." ? path : `${dir}/${path}`;
|
|
2890
|
+
}));
|
|
2891
|
+
}
|
|
2892
|
+
function gotoSuite(config, path) {
|
|
2893
|
+
return config.suites.find((suite) => suite.urls.length > 0 && suite.roots.some((root) => inRoot(path, root)));
|
|
2894
|
+
}
|
|
2895
|
+
function mapUrl(suite, url) {
|
|
2896
|
+
for (const { key, targets } of suite.urls) {
|
|
2897
|
+
const star = key.indexOf("*");
|
|
2898
|
+
if (star === -1) {
|
|
2899
|
+
if (url === key)
|
|
2900
|
+
return targets;
|
|
2901
|
+
continue;
|
|
2902
|
+
}
|
|
2903
|
+
const prefix = key.slice(0, star);
|
|
2904
|
+
const suffix = key.slice(star + 1);
|
|
2905
|
+
if (url.startsWith(prefix) && url.endsWith(suffix)) {
|
|
2906
|
+
const match = url.slice(prefix.length, url.length - suffix.length);
|
|
2907
|
+
return targets.map((target) => target.replaceAll("*", match));
|
|
2908
|
+
}
|
|
2909
|
+
}
|
|
2910
|
+
return;
|
|
2911
|
+
}
|
|
2912
|
+
function gotoPage(config, path, argument) {
|
|
2913
|
+
const suite = gotoSuite(config, path);
|
|
2914
|
+
const url = gotoUrl(argument);
|
|
2915
|
+
return suite === undefined || url === undefined ? undefined : mapUrl(suite, url.replace(QUERY, ""));
|
|
2916
|
+
}
|
|
2917
|
+
function graphFrom(files, extractions, { config, packages, tsconfigs }) {
|
|
2918
|
+
const graph = new Map;
|
|
2919
|
+
const paths = [...files];
|
|
2920
|
+
const exists = new Set(paths);
|
|
2921
|
+
const resolve = createResolver({
|
|
2922
|
+
files: exists,
|
|
2923
|
+
config,
|
|
2924
|
+
packages,
|
|
2925
|
+
tsconfigs
|
|
2926
|
+
});
|
|
2927
|
+
const scans = new Map;
|
|
2928
|
+
const barrels = new Map;
|
|
2929
|
+
for (const path of paths) {
|
|
2930
|
+
const extraction = extractions.get(path);
|
|
2931
|
+
if (!extraction)
|
|
2932
|
+
continue;
|
|
2933
|
+
const gotos = gotoSuite(config, path) !== undefined;
|
|
2934
|
+
scans.set(path, finish(path, extraction, { gotos }));
|
|
2935
|
+
if (extraction.barrel && config.isBarrel(path)) {
|
|
2936
|
+
barrels.set(path, extraction.barrel);
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
const named = new Map;
|
|
2940
|
+
const effects = new Map;
|
|
2941
|
+
const nameUnknown = new Map;
|
|
2942
|
+
for (const [path, barrel] of barrels) {
|
|
2943
|
+
const effectful = sideEffectful(owner(packages, path));
|
|
2944
|
+
if (effectful(path))
|
|
2945
|
+
continue;
|
|
2946
|
+
const resolveTarget = (specifier) => {
|
|
2947
|
+
const resolved = resolve(path, specifier);
|
|
2948
|
+
return resolved && "files" in resolved ? resolved.files : undefined;
|
|
2949
|
+
};
|
|
2950
|
+
const always = [];
|
|
2951
|
+
for (const specifier of [...barrel.names.values(), ...barrel.stars]) {
|
|
2952
|
+
for (const file of resolveTarget(specifier) ?? []) {
|
|
2953
|
+
if (effectful(file) && !always.includes(file))
|
|
2954
|
+
always.push(file);
|
|
2955
|
+
}
|
|
2956
|
+
}
|
|
2957
|
+
const names = new Map;
|
|
2958
|
+
for (const [name, specifier] of barrel.names) {
|
|
2959
|
+
const targets = resolveTarget(specifier);
|
|
2960
|
+
if (!targets) {
|
|
2961
|
+
nameUnknown.set(`${path}#${name}`, [`unresolved "${specifier}"`]);
|
|
2962
|
+
}
|
|
2963
|
+
names.set(name, targets ?? []);
|
|
2964
|
+
}
|
|
2965
|
+
named.set(path, names);
|
|
2966
|
+
effects.set(path, always);
|
|
2967
|
+
}
|
|
2968
|
+
const external = new Set;
|
|
2969
|
+
for (const path of paths) {
|
|
2970
|
+
const found = scans.get(path);
|
|
2971
|
+
if (!found) {
|
|
2972
|
+
graph.set(path, leaf());
|
|
2973
|
+
continue;
|
|
2974
|
+
}
|
|
2975
|
+
const deps = leaf();
|
|
2976
|
+
const valued = new Set;
|
|
2977
|
+
const typed = new Set;
|
|
2978
|
+
const declaration = DECLARATION.test(path);
|
|
2979
|
+
for (const { specifier, names, type } of found.imports) {
|
|
2980
|
+
const resolved = resolve(path, specifier);
|
|
2981
|
+
const add = (node) => {
|
|
2982
|
+
deps.files.add(node);
|
|
2983
|
+
const erased = type === true || declaration || DECLARATION.test(node);
|
|
2984
|
+
(erased ? typed : valued).add(node);
|
|
2985
|
+
};
|
|
2986
|
+
if (resolved === undefined) {
|
|
2987
|
+
const cause = `unresolved "${specifier}"`;
|
|
2988
|
+
if (!deps.unknown.includes(cause))
|
|
2989
|
+
deps.unknown.push(cause);
|
|
2990
|
+
} else if ("pkg" in resolved) {
|
|
2991
|
+
add(`pkg:${resolved.pkg}`);
|
|
2992
|
+
external.add(`pkg:${resolved.pkg}`);
|
|
2993
|
+
} else {
|
|
2994
|
+
if (type !== true && !declaration && JS_SPECIFIER.test(specifier.replace(QUERY, "")) && resolved.files.every((file) => DECLARATION.test(file))) {
|
|
2995
|
+
const cause = `only declarations for "${specifier}"`;
|
|
2996
|
+
if (!deps.unknown.includes(cause))
|
|
2997
|
+
deps.unknown.push(cause);
|
|
2998
|
+
}
|
|
2999
|
+
for (const file of resolved.files) {
|
|
3000
|
+
const barrel = names && named.get(file);
|
|
3001
|
+
if (!barrel || !names) {
|
|
3002
|
+
add(file);
|
|
3003
|
+
continue;
|
|
3004
|
+
}
|
|
3005
|
+
for (const name of names) {
|
|
3006
|
+
add(barrel.has(name) ? `${file}#${name}` : file);
|
|
3007
|
+
}
|
|
3008
|
+
}
|
|
3009
|
+
}
|
|
3010
|
+
}
|
|
3011
|
+
for (const node of typed) {
|
|
3012
|
+
if (valued.has(node))
|
|
3013
|
+
continue;
|
|
3014
|
+
deps.types ??= new Set;
|
|
3015
|
+
deps.types.add(node);
|
|
3016
|
+
}
|
|
3017
|
+
deps.unknown.push(...config.isHermetic(path) ? found.unknown.filter((cause) => !HERMETIC_CAUSES.has(cause)) : found.unknown);
|
|
3018
|
+
for (const glob of found.globs) {
|
|
3019
|
+
addPattern(deps, globToRegExp(glob), "glob");
|
|
3020
|
+
}
|
|
3021
|
+
const suite = found.gotos.length > 0 ? gotoSuite(config, path) : undefined;
|
|
3022
|
+
if (suite) {
|
|
3023
|
+
for (const argument of found.gotos) {
|
|
3024
|
+
const url = gotoUrl(argument);
|
|
3025
|
+
if (url === undefined) {
|
|
3026
|
+
if (found.dependsOn.length === 0) {
|
|
3027
|
+
deps.unknown.push(`${NON_LITERAL_GOTO}${argument})`);
|
|
3028
|
+
}
|
|
3029
|
+
} else if (url !== "about:blank") {
|
|
3030
|
+
const mapped = mapUrl(suite, url.replace(QUERY, ""));
|
|
3031
|
+
if (mapped === undefined)
|
|
3032
|
+
deps.unknown.push(`goto("${url}")`);
|
|
3033
|
+
else if (exists.has(mapped[0] ?? "")) {
|
|
3034
|
+
for (const page of mapped.filter((file) => exists.has(file))) {
|
|
3035
|
+
if (!deps.files.has(page)) {
|
|
3036
|
+
deps.pages ??= new Set;
|
|
3037
|
+
deps.pages.add(page);
|
|
3038
|
+
}
|
|
3039
|
+
deps.files.add(page);
|
|
3040
|
+
}
|
|
3041
|
+
} else
|
|
3042
|
+
deps.unknown.push(`missing fixture "${url}"`);
|
|
3043
|
+
}
|
|
3044
|
+
}
|
|
3045
|
+
}
|
|
3046
|
+
for (const glob of found.dependsOn) {
|
|
3047
|
+
addPattern(deps, globToRegExp(glob), "depends-on");
|
|
3048
|
+
}
|
|
3049
|
+
for (const glob of found.reads)
|
|
3050
|
+
deps.reads.push(globToRegExp(glob));
|
|
3051
|
+
if (found.lists.length > 0)
|
|
3052
|
+
deps.lists = found.lists.map(globToRegExp);
|
|
3053
|
+
const names = named.get(path);
|
|
3054
|
+
if (names)
|
|
3055
|
+
deps.barrel = names;
|
|
3056
|
+
graph.set(path, deps);
|
|
3057
|
+
}
|
|
3058
|
+
for (const [path, names] of named) {
|
|
3059
|
+
for (const [name, targets] of names) {
|
|
3060
|
+
const id = `${path}#${name}`;
|
|
3061
|
+
graph.set(id, {
|
|
3062
|
+
...leaf(),
|
|
3063
|
+
files: new Set([...targets, ...effects.get(path) ?? []]),
|
|
3064
|
+
unknown: nameUnknown.get(id) ?? []
|
|
3065
|
+
});
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
for (const id of external)
|
|
3069
|
+
graph.set(id, leaf());
|
|
3070
|
+
for (const suite of config.suites) {
|
|
3071
|
+
const setup = paths.filter(suite.isSetup);
|
|
3072
|
+
if (setup.length === 0)
|
|
3073
|
+
continue;
|
|
3074
|
+
for (const path of paths) {
|
|
3075
|
+
const deps = suite.isTest(path) ? graph.get(path) : undefined;
|
|
3076
|
+
if (!deps)
|
|
3077
|
+
continue;
|
|
3078
|
+
for (const file of setup) {
|
|
3079
|
+
if (file === path)
|
|
3080
|
+
continue;
|
|
3081
|
+
deps.types?.delete(file);
|
|
3082
|
+
deps.files.add(file);
|
|
3083
|
+
deps.setup ??= new Set;
|
|
3084
|
+
deps.setup.add(file);
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
}
|
|
3088
|
+
for (const deps of [...graph.values()]) {
|
|
3089
|
+
for (const file of deps.files) {
|
|
3090
|
+
if (!graph.has(file) && isGenerated(config, file))
|
|
3091
|
+
graph.set(file, leaf());
|
|
3092
|
+
}
|
|
3093
|
+
}
|
|
3094
|
+
for (const [path, deps] of graph) {
|
|
3095
|
+
let depends = false;
|
|
3096
|
+
let declared = false;
|
|
3097
|
+
for (const edge of config.generated) {
|
|
3098
|
+
const inputs = edge.targets(path);
|
|
3099
|
+
if (!inputs)
|
|
3100
|
+
continue;
|
|
3101
|
+
depends ||= edge.exact;
|
|
3102
|
+
for (const input of inputs)
|
|
3103
|
+
addPattern(deps, input, "edges");
|
|
3104
|
+
}
|
|
3105
|
+
for (const read of config.reads) {
|
|
3106
|
+
const globs = read.targets(path);
|
|
3107
|
+
if (!globs)
|
|
3108
|
+
continue;
|
|
3109
|
+
declared ||= read.exact;
|
|
3110
|
+
deps.reads.push(...globs);
|
|
3111
|
+
}
|
|
3112
|
+
if (depends || declared) {
|
|
3113
|
+
deps.unknown = deps.unknown.filter((cause) => cause !== UNDECLARED_READS && !(depends && cause.startsWith(NON_LITERAL_GOTO)));
|
|
3114
|
+
}
|
|
3115
|
+
}
|
|
3116
|
+
return graph;
|
|
3117
|
+
}
|
|
3118
|
+
function isGenerated(config, path) {
|
|
3119
|
+
return config.generated.some(({ pattern }) => pattern.test(path));
|
|
3120
|
+
}
|
|
3121
|
+
function changedBarrelNames(before, after) {
|
|
3122
|
+
const names = new Set([...before.keys(), ...after.keys()]);
|
|
3123
|
+
const key = (files) => files?.join("\x00");
|
|
3124
|
+
return [...names].filter((name) => key(before.get(name)) !== key(after.get(name)));
|
|
3125
|
+
}
|
|
3126
|
+
function baseBarrelNames(path, source, { config, packages, tsconfigs }, graph) {
|
|
3127
|
+
const barrel = source === undefined ? undefined : parseBarrel(path, source);
|
|
3128
|
+
if (!barrel)
|
|
3129
|
+
return;
|
|
3130
|
+
const resolve = createResolver({
|
|
3131
|
+
files: new Set(graph.keys()),
|
|
3132
|
+
config,
|
|
3133
|
+
packages,
|
|
3134
|
+
tsconfigs
|
|
3135
|
+
});
|
|
3136
|
+
const names = new Map;
|
|
3137
|
+
for (const [name, specifier] of barrel.names) {
|
|
3138
|
+
const resolved = resolve(path, specifier);
|
|
3139
|
+
names.set(name, resolved && "files" in resolved ? resolved.files : []);
|
|
3140
|
+
}
|
|
3141
|
+
return names;
|
|
3142
|
+
}
|
|
3143
|
+
var indexes = new WeakMap;
|
|
3144
|
+
var REGEX_SYNTAX = "[](){}|*+?.^$";
|
|
3145
|
+
function literalOf(re) {
|
|
3146
|
+
const { source } = re;
|
|
3147
|
+
if (!source.startsWith("^") || !source.endsWith("$"))
|
|
3148
|
+
return;
|
|
3149
|
+
let path = "";
|
|
3150
|
+
for (let i = 1;i < source.length - 1; i++) {
|
|
3151
|
+
const char = source[i];
|
|
3152
|
+
if (char === "\\")
|
|
3153
|
+
path += source[++i];
|
|
3154
|
+
else if (REGEX_SYNTAX.includes(char))
|
|
3155
|
+
return;
|
|
3156
|
+
else
|
|
3157
|
+
path += char;
|
|
3158
|
+
}
|
|
3159
|
+
return path;
|
|
3160
|
+
}
|
|
3161
|
+
function patterns() {
|
|
3162
|
+
const literal = new Map;
|
|
3163
|
+
const wild = [];
|
|
3164
|
+
const bySource = new Map;
|
|
3165
|
+
return {
|
|
3166
|
+
literal,
|
|
3167
|
+
wild,
|
|
3168
|
+
add: (re, node, kind) => {
|
|
3169
|
+
let group = bySource.get(re.source);
|
|
3170
|
+
if (!group) {
|
|
3171
|
+
group = { re, nodes: [], kinds: [] };
|
|
3172
|
+
bySource.set(re.source, group);
|
|
3173
|
+
const path = literalOf(re);
|
|
3174
|
+
if (path === undefined)
|
|
3175
|
+
wild.push(group);
|
|
3176
|
+
else {
|
|
3177
|
+
const list = literal.get(path);
|
|
3178
|
+
list ? list.push(group) : literal.set(path, [group]);
|
|
3179
|
+
}
|
|
3180
|
+
}
|
|
3181
|
+
if (group.nodes.at(-1) === node)
|
|
3182
|
+
return;
|
|
3183
|
+
group.nodes.push(node);
|
|
3184
|
+
group.kinds.push(kind);
|
|
3185
|
+
}
|
|
3186
|
+
};
|
|
3187
|
+
}
|
|
3188
|
+
function reverseIndex(graph) {
|
|
3189
|
+
const cached = indexes.get(graph);
|
|
3190
|
+
if (cached)
|
|
3191
|
+
return cached;
|
|
3192
|
+
const dependents = new Map;
|
|
3193
|
+
const globbers = patterns();
|
|
3194
|
+
const readers = patterns();
|
|
3195
|
+
const listers = patterns();
|
|
3196
|
+
const unknown = [];
|
|
3197
|
+
for (const [path, deps] of graph) {
|
|
3198
|
+
for (const file of deps.files) {
|
|
3199
|
+
const list = dependents.get(file);
|
|
3200
|
+
list ? list.push(path) : dependents.set(file, [path]);
|
|
3201
|
+
}
|
|
3202
|
+
for (const re of deps.patterns) {
|
|
3203
|
+
globbers.add(re, path, deps.patternKinds?.get(re) ?? "glob");
|
|
3204
|
+
}
|
|
3205
|
+
for (const re of deps.reads)
|
|
3206
|
+
readers.add(re, path, "reads");
|
|
3207
|
+
for (const re of deps.lists ?? [])
|
|
3208
|
+
listers.add(re, path, "lists");
|
|
3209
|
+
if (deps.unknown.length > 0)
|
|
3210
|
+
unknown.push(path);
|
|
3211
|
+
}
|
|
3212
|
+
const index = {
|
|
3213
|
+
dependents,
|
|
3214
|
+
globbers,
|
|
3215
|
+
readers,
|
|
3216
|
+
listers,
|
|
3217
|
+
unknown
|
|
3218
|
+
};
|
|
3219
|
+
indexes.set(graph, index);
|
|
3220
|
+
return index;
|
|
3221
|
+
}
|
|
3222
|
+
function runtimeDependents(graph, index) {
|
|
3223
|
+
if (index.runtime)
|
|
3224
|
+
return index.runtime;
|
|
3225
|
+
const runtime = new Map;
|
|
3226
|
+
for (const [path, deps] of graph) {
|
|
3227
|
+
for (const file of deps.files) {
|
|
3228
|
+
if (deps.types?.has(file))
|
|
3229
|
+
continue;
|
|
3230
|
+
const list = runtime.get(file);
|
|
3231
|
+
list ? list.push(path) : runtime.set(file, [path]);
|
|
3232
|
+
}
|
|
3233
|
+
}
|
|
3234
|
+
index.runtime = runtime;
|
|
3235
|
+
return runtime;
|
|
3236
|
+
}
|
|
3237
|
+
function reaches(graph, changed, match, options = {}) {
|
|
3238
|
+
const reached = affectedBy(graph, changed, { ...options, until: match });
|
|
3239
|
+
for (const path of reached)
|
|
3240
|
+
if (match(path))
|
|
3241
|
+
return true;
|
|
3242
|
+
return false;
|
|
3243
|
+
}
|
|
3244
|
+
function walksByTypes(graph, changed, options = {}) {
|
|
3245
|
+
const seeds = [...changed];
|
|
3246
|
+
const walks = new Map;
|
|
3247
|
+
return (types) => {
|
|
3248
|
+
let walk = walks.get(types);
|
|
3249
|
+
if (!walk) {
|
|
3250
|
+
walk = affectedBy(graph, seeds, { ...options, types });
|
|
3251
|
+
walks.set(types, walk);
|
|
3252
|
+
}
|
|
3253
|
+
return walk;
|
|
3254
|
+
};
|
|
3255
|
+
}
|
|
3256
|
+
function importersOf(graph, node) {
|
|
3257
|
+
return reverseIndex(graph).dependents.get(node) ?? [];
|
|
3258
|
+
}
|
|
3259
|
+
function fileKind(graph, dependent, node) {
|
|
3260
|
+
if (dependent.includes("#"))
|
|
3261
|
+
return "export";
|
|
3262
|
+
const deps = graph.get(dependent);
|
|
3263
|
+
if (deps?.setup?.has(node))
|
|
3264
|
+
return "setup";
|
|
3265
|
+
if (deps?.types?.has(node))
|
|
3266
|
+
return "type";
|
|
3267
|
+
if (deps?.pages?.has(node))
|
|
3268
|
+
return "goto";
|
|
3269
|
+
if (node.includes("#"))
|
|
3270
|
+
return "name";
|
|
3271
|
+
return dependent.endsWith(".html") ? "html" : "import";
|
|
3272
|
+
}
|
|
3273
|
+
function affectedBy(graph, changed, {
|
|
3274
|
+
includeUnknown = true,
|
|
3275
|
+
quiet = [],
|
|
3276
|
+
types = true,
|
|
3277
|
+
layout,
|
|
3278
|
+
readers: seedReaders = true,
|
|
3279
|
+
opaque,
|
|
3280
|
+
until,
|
|
3281
|
+
parents
|
|
3282
|
+
} = {}) {
|
|
3283
|
+
const index = reverseIndex(graph);
|
|
3284
|
+
const { globbers, readers, listers, unknown } = index;
|
|
3285
|
+
const dependents = types ? index.dependents : runtimeDependents(graph, index);
|
|
3286
|
+
const affected = new Set;
|
|
3287
|
+
const queue = [];
|
|
3288
|
+
let done = false;
|
|
3289
|
+
const visit = (path, via) => {
|
|
3290
|
+
if (affected.has(path) || opaque?.(path))
|
|
3291
|
+
return;
|
|
3292
|
+
affected.add(path);
|
|
3293
|
+
queue.push(path);
|
|
3294
|
+
if (via && parents)
|
|
3295
|
+
parents.set(path, via);
|
|
3296
|
+
if (until?.(path))
|
|
3297
|
+
done = true;
|
|
3298
|
+
};
|
|
3299
|
+
const visitGroup = (group, from) => {
|
|
3300
|
+
const { nodes, kinds } = group;
|
|
3301
|
+
for (let i = 0;i < nodes.length; i++) {
|
|
3302
|
+
visit(nodes[i], parents && { from, kind: kinds[i] });
|
|
3303
|
+
}
|
|
3304
|
+
};
|
|
3305
|
+
const visitMatches = (groups, path) => {
|
|
3306
|
+
for (const group of groups.literal.get(path) ?? []) {
|
|
3307
|
+
visitGroup(group, path);
|
|
3308
|
+
}
|
|
3309
|
+
for (const group of groups.wild) {
|
|
3310
|
+
if (group.re.test(path))
|
|
3311
|
+
visitGroup(group, path);
|
|
3312
|
+
}
|
|
3313
|
+
};
|
|
3314
|
+
const changedPaths = [...changed];
|
|
3315
|
+
for (const path of changedPaths)
|
|
3316
|
+
visit(path);
|
|
3317
|
+
const first = queue[0];
|
|
3318
|
+
for (const path of quiet) {
|
|
3319
|
+
visit(path);
|
|
3320
|
+
changedPaths.push(path);
|
|
3321
|
+
}
|
|
3322
|
+
if (seedReaders) {
|
|
3323
|
+
for (const path of changedPaths)
|
|
3324
|
+
visitMatches(readers, path);
|
|
3325
|
+
for (const path of changedPaths) {
|
|
3326
|
+
if (!layout || layout.has(path))
|
|
3327
|
+
visitMatches(listers, path);
|
|
3328
|
+
}
|
|
3329
|
+
}
|
|
3330
|
+
if (includeUnknown && first !== undefined) {
|
|
3331
|
+
for (const path of unknown)
|
|
3332
|
+
visit(path, { from: first, kind: "unknown" });
|
|
3333
|
+
}
|
|
3334
|
+
const { literal } = globbers;
|
|
3335
|
+
let active = globbers.wild;
|
|
3336
|
+
let spent;
|
|
3337
|
+
const spend = (group, from) => {
|
|
3338
|
+
visitGroup(group, from);
|
|
3339
|
+
spent ??= new Set;
|
|
3340
|
+
spent.add(group);
|
|
3341
|
+
};
|
|
3342
|
+
for (let at = 0;at < queue.length && !done; at++) {
|
|
3343
|
+
const path = queue[at];
|
|
3344
|
+
for (const dependent of dependents.get(path) ?? []) {
|
|
3345
|
+
visit(dependent, parents && { from: path, kind: fileKind(graph, dependent, path) });
|
|
3346
|
+
}
|
|
3347
|
+
const exact = literal.size > 0 ? literal.get(path) : undefined;
|
|
3348
|
+
if (exact) {
|
|
3349
|
+
for (const group of exact) {
|
|
3350
|
+
if (!spent?.has(group))
|
|
3351
|
+
spend(group, path);
|
|
3352
|
+
}
|
|
3353
|
+
}
|
|
3354
|
+
let matched = false;
|
|
3355
|
+
for (const group of active) {
|
|
3356
|
+
if (!group.re.test(path))
|
|
3357
|
+
continue;
|
|
3358
|
+
spend(group, path);
|
|
3359
|
+
matched = true;
|
|
3360
|
+
}
|
|
3361
|
+
if (matched)
|
|
3362
|
+
active = active.filter((group) => !spent?.has(group));
|
|
3363
|
+
}
|
|
3364
|
+
return affected;
|
|
3365
|
+
}
|
|
3366
|
+
|
|
3367
|
+
// src/lockfile.ts
|
|
3368
|
+
import { posix as posix3 } from "node:path";
|
|
3369
|
+
|
|
3370
|
+
// src/yaml.ts
|
|
3371
|
+
var COMMENT = /\s+#.*$/;
|
|
3372
|
+
var KEY_END = /:(?: |$)/;
|
|
3373
|
+
var EMPTY_MAP = /^\{\s*\}(?:\s+#.*)?$/;
|
|
3374
|
+
var EMPTY_LIST = /^\[\s*\](?:\s+#.*)?$/;
|
|
3375
|
+
var UNSUPPORTED = /^[&*!|>%@`]/;
|
|
3376
|
+
|
|
3377
|
+
class YamlError extends Error {
|
|
3378
|
+
}
|
|
3379
|
+
function fail2(line, message) {
|
|
3380
|
+
throw new YamlError(`${message} at line ${line?.number ?? "end"}`);
|
|
3381
|
+
}
|
|
3382
|
+
function linesOf(text) {
|
|
3383
|
+
const lines = [];
|
|
3384
|
+
for (const [i, raw] of text.split(`
|
|
3385
|
+
`).entries()) {
|
|
3386
|
+
const line = raw.endsWith("\r") ? raw.slice(0, -1) : raw;
|
|
3387
|
+
const content = line.trimStart();
|
|
3388
|
+
if (content === "" || content.startsWith("#"))
|
|
3389
|
+
continue;
|
|
3390
|
+
const indent = line.length - content.length;
|
|
3391
|
+
if (line.slice(0, indent).includes("\t")) {
|
|
3392
|
+
fail2({ indent, text: content, number: i + 1 }, "a tab in indentation");
|
|
3393
|
+
}
|
|
3394
|
+
lines.push({ indent, text: content.trimEnd(), number: i + 1 });
|
|
3395
|
+
}
|
|
3396
|
+
return lines;
|
|
3397
|
+
}
|
|
3398
|
+
function quoteEnd(text, start) {
|
|
3399
|
+
const quote = text[start];
|
|
3400
|
+
for (let i = start + 1;i < text.length; i++) {
|
|
3401
|
+
if (quote === "'" && text[i] === "'") {
|
|
3402
|
+
if (text[i + 1] === "'")
|
|
3403
|
+
i++;
|
|
3404
|
+
else
|
|
3405
|
+
return i + 1;
|
|
3406
|
+
} else if (quote === '"' && text[i] === "\\")
|
|
3407
|
+
i++;
|
|
3408
|
+
else if (quote === '"' && text[i] === '"')
|
|
3409
|
+
return i + 1;
|
|
3410
|
+
}
|
|
3411
|
+
return -1;
|
|
3412
|
+
}
|
|
3413
|
+
function unquote(quoted) {
|
|
3414
|
+
if (quoted.startsWith("'"))
|
|
3415
|
+
return quoted.slice(1, -1).replaceAll("''", "'");
|
|
3416
|
+
const value = JSON.parse(quoted);
|
|
3417
|
+
if (typeof value !== "string")
|
|
3418
|
+
throw new YamlError("bad quoted scalar");
|
|
3419
|
+
return value;
|
|
3420
|
+
}
|
|
3421
|
+
function flowEnd(text, line) {
|
|
3422
|
+
let depth = 0;
|
|
3423
|
+
for (let i = 0;i < text.length; i++) {
|
|
3424
|
+
const c = text[i];
|
|
3425
|
+
if (c === "'" || c === '"') {
|
|
3426
|
+
const end = quoteEnd(text, i);
|
|
3427
|
+
if (end === -1)
|
|
3428
|
+
fail2(line, "an unterminated string");
|
|
3429
|
+
i = end - 1;
|
|
3430
|
+
} else if (c === "{" || c === "[")
|
|
3431
|
+
depth++;
|
|
3432
|
+
else if (c === "}" || c === "]") {
|
|
3433
|
+
if (--depth === 0)
|
|
3434
|
+
return i + 1;
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
return fail2(line, "a flow collection over several lines");
|
|
3438
|
+
}
|
|
3439
|
+
function scalar(text, line) {
|
|
3440
|
+
if (text.startsWith("'") || text.startsWith('"')) {
|
|
3441
|
+
const end = quoteEnd(text, 0);
|
|
3442
|
+
if (end === -1)
|
|
3443
|
+
fail2(line, "an unterminated string");
|
|
3444
|
+
if (text.slice(end).replace(COMMENT, "").trim() !== "") {
|
|
3445
|
+
fail2(line, "text after a quoted scalar");
|
|
3446
|
+
}
|
|
3447
|
+
return unquote(text.slice(0, end));
|
|
3448
|
+
}
|
|
3449
|
+
if (text.startsWith("{") || text.startsWith("[")) {
|
|
3450
|
+
const end = flowEnd(text, line);
|
|
3451
|
+
if (text.slice(end).replace(COMMENT, "").trim() !== "") {
|
|
3452
|
+
fail2(line, "text after a flow collection");
|
|
3453
|
+
}
|
|
3454
|
+
return text.slice(0, end);
|
|
3455
|
+
}
|
|
3456
|
+
if (UNSUPPORTED.test(text))
|
|
3457
|
+
fail2(line, `unsupported syntax "${text[0]}"`);
|
|
3458
|
+
return text.replace(COMMENT, "");
|
|
3459
|
+
}
|
|
3460
|
+
function keyOf(text, line) {
|
|
3461
|
+
if (text.startsWith("'") || text.startsWith('"')) {
|
|
3462
|
+
const end = quoteEnd(text, 0);
|
|
3463
|
+
if (end === -1)
|
|
3464
|
+
fail2(line, "an unterminated key");
|
|
3465
|
+
if (text[end] !== ":")
|
|
3466
|
+
return;
|
|
3467
|
+
return { key: unquote(text.slice(0, end)), rest: text.slice(end + 1) };
|
|
3468
|
+
}
|
|
3469
|
+
const colon = text.search(KEY_END);
|
|
3470
|
+
if (colon === -1)
|
|
3471
|
+
return;
|
|
3472
|
+
return { key: text.slice(0, colon), rest: text.slice(colon + 1) };
|
|
3473
|
+
}
|
|
3474
|
+
|
|
3475
|
+
class Parser {
|
|
3476
|
+
at = 0;
|
|
3477
|
+
lines;
|
|
3478
|
+
constructor(lines) {
|
|
3479
|
+
this.lines = lines;
|
|
3480
|
+
}
|
|
3481
|
+
block(indent) {
|
|
3482
|
+
const first = this.lines[this.at];
|
|
3483
|
+
if (!first || first.indent !== indent)
|
|
3484
|
+
return "";
|
|
3485
|
+
return first.text === "-" || first.text.startsWith("- ") ? this.sequence(indent) : this.mapping(indent, {});
|
|
3486
|
+
}
|
|
3487
|
+
value(rest, indent, line) {
|
|
3488
|
+
const text = rest.trim();
|
|
3489
|
+
if (EMPTY_MAP.test(text))
|
|
3490
|
+
return {};
|
|
3491
|
+
if (EMPTY_LIST.test(text))
|
|
3492
|
+
return [];
|
|
3493
|
+
if (text !== "" && !text.startsWith("#"))
|
|
3494
|
+
return scalar(text, line);
|
|
3495
|
+
const next = this.lines[this.at];
|
|
3496
|
+
return next && next.indent > indent ? this.block(next.indent) : "";
|
|
3497
|
+
}
|
|
3498
|
+
mapping(indent, into) {
|
|
3499
|
+
for (let line = this.lines[this.at];line; line = this.lines[this.at]) {
|
|
3500
|
+
if (line.indent < indent)
|
|
3501
|
+
break;
|
|
3502
|
+
if (line.indent > indent)
|
|
3503
|
+
fail2(line, "unexpected indentation");
|
|
3504
|
+
const entry = keyOf(line.text, line);
|
|
3505
|
+
if (!entry)
|
|
3506
|
+
fail2(line, "a line that isn't a key");
|
|
3507
|
+
if (Object.hasOwn(into, entry.key))
|
|
3508
|
+
fail2(line, "a duplicate key");
|
|
3509
|
+
this.at++;
|
|
3510
|
+
into[entry.key] = this.value(entry.rest, indent, line);
|
|
3511
|
+
}
|
|
3512
|
+
return into;
|
|
3513
|
+
}
|
|
3514
|
+
sequence(indent) {
|
|
3515
|
+
const items = [];
|
|
3516
|
+
for (let line = this.lines[this.at];line; line = this.lines[this.at]) {
|
|
3517
|
+
if (line.indent < indent)
|
|
3518
|
+
break;
|
|
3519
|
+
if (line.indent > indent)
|
|
3520
|
+
fail2(line, "unexpected indentation");
|
|
3521
|
+
if (line.text !== "-" && !line.text.startsWith("- "))
|
|
3522
|
+
break;
|
|
3523
|
+
this.at++;
|
|
3524
|
+
const text = line.text.slice(1).trimStart();
|
|
3525
|
+
const inner = { ...line, indent: line.indent + 2, text };
|
|
3526
|
+
if (text !== "" && keyOf(text, line)) {
|
|
3527
|
+
this.lines.splice(this.at, 0, inner);
|
|
3528
|
+
items.push(this.mapping(inner.indent, {}));
|
|
3529
|
+
} else {
|
|
3530
|
+
items.push(this.value(text, indent, line));
|
|
3531
|
+
}
|
|
3532
|
+
}
|
|
3533
|
+
return items;
|
|
3534
|
+
}
|
|
3535
|
+
}
|
|
3536
|
+
function parseYaml(text) {
|
|
3537
|
+
const parser = new Parser(linesOf(text));
|
|
3538
|
+
const value = parser.block(0);
|
|
3539
|
+
const left = parser.lines[parser.at];
|
|
3540
|
+
if (left)
|
|
3541
|
+
fail2(left, "unexpected indentation");
|
|
3542
|
+
return value;
|
|
3543
|
+
}
|
|
3544
|
+
|
|
3545
|
+
// src/lockfile.ts
|
|
3546
|
+
var DEP_FIELDS2 = [
|
|
3547
|
+
"dependencies",
|
|
3548
|
+
"devDependencies",
|
|
3549
|
+
"peerDependencies",
|
|
3550
|
+
"optionalDependencies"
|
|
3551
|
+
];
|
|
3552
|
+
function isRecord3(value) {
|
|
3553
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3554
|
+
}
|
|
3555
|
+
function isStringRecord3(value) {
|
|
3556
|
+
return isRecord3(value) && Object.values(value).every((item) => typeof item === "string");
|
|
3557
|
+
}
|
|
3558
|
+
var STRING_OR_TRAILING_COMMA = /"(?:[^"\\]|\\.)*"|,(\s*[}\]])/g;
|
|
3559
|
+
function parseLenient(text) {
|
|
3560
|
+
return JSON.parse(text.replace(STRING_OR_TRAILING_COMMA, (match, close) => close ?? match));
|
|
3561
|
+
}
|
|
3562
|
+
function specsOf(value) {
|
|
3563
|
+
const specs = new Map;
|
|
3564
|
+
for (const field of DEP_FIELDS2) {
|
|
3565
|
+
const deps = value[field];
|
|
3566
|
+
if (deps === undefined)
|
|
3567
|
+
continue;
|
|
3568
|
+
if (!isStringRecord3(deps))
|
|
3569
|
+
return;
|
|
3570
|
+
specs.set(field, new Map(Object.entries(deps)));
|
|
3571
|
+
}
|
|
3572
|
+
return specs;
|
|
3573
|
+
}
|
|
3574
|
+
function restOf(value, skip) {
|
|
3575
|
+
return JSON.stringify(Object.entries(value).filter(([key]) => !skip.includes(key)));
|
|
3576
|
+
}
|
|
3577
|
+
function install(packages, name, entry, version, info) {
|
|
3578
|
+
const installed = packages.get(name) ?? {
|
|
3579
|
+
entries: [],
|
|
3580
|
+
versions: [],
|
|
3581
|
+
dependencies: new Set,
|
|
3582
|
+
bin: false
|
|
3583
|
+
};
|
|
3584
|
+
if (info?.bin !== undefined || info?.hasBin === true || info?.hasBin === "true") {
|
|
3585
|
+
installed.bin = true;
|
|
3586
|
+
}
|
|
3587
|
+
installed.entries.push(JSON.stringify(entry));
|
|
3588
|
+
if (!installed.versions.includes(version))
|
|
3589
|
+
installed.versions.push(version);
|
|
3590
|
+
for (const field of [
|
|
3591
|
+
"dependencies",
|
|
3592
|
+
"optionalDependencies",
|
|
3593
|
+
"peerDependencies"
|
|
3594
|
+
]) {
|
|
3595
|
+
const deps = info?.[field];
|
|
3596
|
+
if (isRecord3(deps)) {
|
|
3597
|
+
for (const dep of Object.keys(deps))
|
|
3598
|
+
installed.dependencies.add(dep);
|
|
3599
|
+
}
|
|
3600
|
+
}
|
|
3601
|
+
packages.set(name, installed);
|
|
3602
|
+
}
|
|
3603
|
+
function installedName(key) {
|
|
3604
|
+
const parts = key.split("/");
|
|
3605
|
+
const last = parts.length - 1;
|
|
3606
|
+
return last > 0 && parts[last - 1].startsWith("@") ? `${parts[last - 1]}/${parts[last]}` : parts[last];
|
|
3607
|
+
}
|
|
3608
|
+
function parseBunLock(text) {
|
|
3609
|
+
const value = parseLenient(text);
|
|
3610
|
+
if (!isRecord3(value))
|
|
3611
|
+
return;
|
|
3612
|
+
const { packages, workspaces } = value;
|
|
3613
|
+
if (!isRecord3(packages) || !isRecord3(workspaces))
|
|
3614
|
+
return;
|
|
3615
|
+
const lockfile = {
|
|
3616
|
+
packages: new Map,
|
|
3617
|
+
workspaces: new Map,
|
|
3618
|
+
rest: restOf(value, ["packages", "workspaces"])
|
|
3619
|
+
};
|
|
3620
|
+
for (const [key, entry] of Object.entries(packages)) {
|
|
3621
|
+
if (!Array.isArray(entry) || typeof entry[0] !== "string")
|
|
3622
|
+
return;
|
|
3623
|
+
const ident = entry[0];
|
|
3624
|
+
const at = ident.indexOf("@", 1);
|
|
3625
|
+
const version = at === -1 ? "" : ident.slice(at + 1);
|
|
3626
|
+
install(lockfile.packages, installedName(key), entry, version, entry.find(isRecord3));
|
|
3627
|
+
}
|
|
3628
|
+
for (const [dir, workspace] of Object.entries(workspaces)) {
|
|
3629
|
+
const specs = isRecord3(workspace) ? specsOf(workspace) : undefined;
|
|
3630
|
+
if (!specs)
|
|
3631
|
+
return;
|
|
3632
|
+
lockfile.workspaces.set(dir === "" ? "." : dir, specs);
|
|
3633
|
+
}
|
|
3634
|
+
return lockfile;
|
|
3635
|
+
}
|
|
3636
|
+
var NODE_MODULES = "node_modules/";
|
|
3637
|
+
function parsePackageLock(text) {
|
|
3638
|
+
const value = JSON.parse(text);
|
|
3639
|
+
if (!isRecord3(value) || !isRecord3(value.packages))
|
|
3640
|
+
return;
|
|
3641
|
+
const { lockfileVersion } = value;
|
|
3642
|
+
if (lockfileVersion !== 2 && lockfileVersion !== 3)
|
|
3643
|
+
return;
|
|
3644
|
+
const lockfile = {
|
|
3645
|
+
packages: new Map,
|
|
3646
|
+
workspaces: new Map,
|
|
3647
|
+
rest: restOf(value, ["packages", "dependencies", "name", "version"])
|
|
3648
|
+
};
|
|
3649
|
+
for (const [key, entry] of Object.entries(value.packages)) {
|
|
3650
|
+
if (!isRecord3(entry))
|
|
3651
|
+
return;
|
|
3652
|
+
const at = key.lastIndexOf(NODE_MODULES);
|
|
3653
|
+
if (at !== -1 && (at === 0 || key[at - 1] === "/")) {
|
|
3654
|
+
const version = typeof entry.version === "string" ? entry.version : "";
|
|
3655
|
+
const name = key.slice(at + NODE_MODULES.length);
|
|
3656
|
+
install(lockfile.packages, name, entry, version, entry);
|
|
3657
|
+
continue;
|
|
3658
|
+
}
|
|
3659
|
+
const specs = specsOf(entry);
|
|
3660
|
+
if (!specs)
|
|
3661
|
+
return;
|
|
3662
|
+
lockfile.workspaces.set(key === "" ? "." : key, specs);
|
|
3663
|
+
}
|
|
3664
|
+
return lockfile;
|
|
3665
|
+
}
|
|
3666
|
+
function pnpmKey(key) {
|
|
3667
|
+
const bare = key.startsWith("/") ? key.slice(1) : key;
|
|
3668
|
+
const at = bare.indexOf("@", 1);
|
|
3669
|
+
if (at === -1)
|
|
3670
|
+
return;
|
|
3671
|
+
return { name: bare.slice(0, at), version: withoutPeers(bare.slice(at + 1)) };
|
|
3672
|
+
}
|
|
3673
|
+
function withoutPeers(version) {
|
|
3674
|
+
const paren = version.indexOf("(");
|
|
3675
|
+
return paren === -1 ? version : version.slice(0, paren);
|
|
3676
|
+
}
|
|
3677
|
+
var YAML_DOCUMENT = /^---[ \t]*$/m;
|
|
3678
|
+
var PNPM_VERSION = /^[69]\./;
|
|
3679
|
+
var PNPM_DEP_FIELDS = [
|
|
3680
|
+
"dependencies",
|
|
3681
|
+
"devDependencies",
|
|
3682
|
+
"optionalDependencies"
|
|
3683
|
+
];
|
|
3684
|
+
function parsePnpmLock(text) {
|
|
3685
|
+
const docs = text.split(YAML_DOCUMENT).filter((doc) => doc.trim() !== "").map(parseYaml);
|
|
3686
|
+
const main = docs.findLast((doc) => isRecord3(doc) && typeof doc.lockfileVersion === "string");
|
|
3687
|
+
if (!isRecord3(main))
|
|
3688
|
+
return;
|
|
3689
|
+
const { lockfileVersion, packages = {}, snapshots = {} } = main;
|
|
3690
|
+
if (typeof lockfileVersion !== "string")
|
|
3691
|
+
return;
|
|
3692
|
+
if (!PNPM_VERSION.test(lockfileVersion))
|
|
3693
|
+
return;
|
|
3694
|
+
if (!isRecord3(packages) || !isRecord3(snapshots))
|
|
3695
|
+
return;
|
|
3696
|
+
const workspaces = isRecord3(main.importers);
|
|
3697
|
+
const importers = isRecord3(main.importers) ? main.importers : { ".": main };
|
|
3698
|
+
const lockfile = {
|
|
3699
|
+
packages: new Map,
|
|
3700
|
+
workspaces: new Map,
|
|
3701
|
+
rest: JSON.stringify([
|
|
3702
|
+
restOf(main, ["importers", "packages", "snapshots", ...PNPM_DEP_FIELDS]),
|
|
3703
|
+
...docs.filter((doc) => doc !== main)
|
|
3704
|
+
])
|
|
3705
|
+
};
|
|
3706
|
+
const restOfImporters = [];
|
|
3707
|
+
for (const [dir, importer] of Object.entries(importers)) {
|
|
3708
|
+
if (!isRecord3(importer))
|
|
3709
|
+
return;
|
|
3710
|
+
const specs = new Map;
|
|
3711
|
+
for (const field of PNPM_DEP_FIELDS) {
|
|
3712
|
+
const deps = importer[field];
|
|
3713
|
+
if (deps === undefined)
|
|
3714
|
+
continue;
|
|
3715
|
+
if (!isRecord3(deps))
|
|
3716
|
+
return;
|
|
3717
|
+
const declared = new Map;
|
|
3718
|
+
for (const [name, dep] of Object.entries(deps)) {
|
|
3719
|
+
if (!isRecord3(dep) || typeof dep.specifier !== "string") {
|
|
3720
|
+
return;
|
|
3721
|
+
}
|
|
3722
|
+
declared.set(name, dep.specifier);
|
|
3723
|
+
const version = typeof dep.version === "string" ? dep.version : "";
|
|
3724
|
+
install(lockfile.packages, name, [dir, field, version], withoutPeers(version), {});
|
|
3725
|
+
}
|
|
3726
|
+
specs.set(field, declared);
|
|
3727
|
+
}
|
|
3728
|
+
lockfile.workspaces.set(dir, specs);
|
|
3729
|
+
if (workspaces) {
|
|
3730
|
+
const settings = restOf(importer, PNPM_DEP_FIELDS);
|
|
3731
|
+
if (settings !== "[]")
|
|
3732
|
+
restOfImporters.push([dir, settings]);
|
|
3733
|
+
}
|
|
3734
|
+
}
|
|
3735
|
+
lockfile.rest += JSON.stringify(restOfImporters);
|
|
3736
|
+
for (const section of [packages, snapshots]) {
|
|
3737
|
+
for (const [key, entry] of Object.entries(section)) {
|
|
3738
|
+
const parsed = pnpmKey(key);
|
|
3739
|
+
if (!parsed)
|
|
3740
|
+
return;
|
|
3741
|
+
const info = isRecord3(entry) ? entry : undefined;
|
|
3742
|
+
install(lockfile.packages, parsed.name, [key, entry], parsed.version, info);
|
|
3743
|
+
}
|
|
3744
|
+
}
|
|
3745
|
+
return lockfile;
|
|
3746
|
+
}
|
|
3747
|
+
var FORMATS = new Map([
|
|
3748
|
+
["bun.lock", parseBunLock],
|
|
3749
|
+
["package-lock.json", parsePackageLock],
|
|
3750
|
+
["pnpm-lock.yaml", parsePnpmLock]
|
|
3751
|
+
]);
|
|
3752
|
+
var UNREAD = ["bun.lockb", "npm-shrinkwrap.json", "yarn.lock"];
|
|
3753
|
+
function parse(file, text) {
|
|
3754
|
+
const format = FORMATS.get(file);
|
|
3755
|
+
if (text === undefined || !format)
|
|
3756
|
+
return;
|
|
3757
|
+
try {
|
|
3758
|
+
return format(text);
|
|
3759
|
+
} catch {
|
|
3760
|
+
return;
|
|
3761
|
+
}
|
|
3762
|
+
}
|
|
3763
|
+
function describe(name, before, after) {
|
|
3764
|
+
if (before === undefined)
|
|
3765
|
+
return `${name} ${after} added`;
|
|
3766
|
+
if (after === undefined)
|
|
3767
|
+
return `${name} ${before} removed`;
|
|
3768
|
+
return before === after ? `${name} ${after}, entry changed` : `${name} ${before} → ${after}`;
|
|
3769
|
+
}
|
|
3770
|
+
function versions(installed) {
|
|
3771
|
+
return installed && sorted(installed.versions).join(", ");
|
|
3772
|
+
}
|
|
3773
|
+
function specChanges(before, after) {
|
|
3774
|
+
const changed = new Map;
|
|
3775
|
+
for (const field of DEP_FIELDS2) {
|
|
3776
|
+
const a = before?.get(field);
|
|
3777
|
+
const b = after?.get(field);
|
|
3778
|
+
for (const name of new Set([...a?.keys() ?? [], ...b?.keys() ?? []])) {
|
|
3779
|
+
if (a?.get(name) !== b?.get(name) && !changed.has(name)) {
|
|
3780
|
+
changed.set(name, [a?.get(name), b?.get(name)]);
|
|
3781
|
+
}
|
|
3782
|
+
}
|
|
3783
|
+
}
|
|
3784
|
+
return changed;
|
|
3785
|
+
}
|
|
3786
|
+
function lockfileChanges(file, base, head) {
|
|
3787
|
+
const changes = [];
|
|
3788
|
+
const names = new Set([...base.packages.keys(), ...head.packages.keys()]);
|
|
3789
|
+
for (const name of names) {
|
|
3790
|
+
const before = base.packages.get(name);
|
|
3791
|
+
const after = head.packages.get(name);
|
|
3792
|
+
const same = before !== undefined && after !== undefined && sorted(before.entries).join(`
|
|
3793
|
+
`) === sorted(after.entries).join(`
|
|
3794
|
+
`);
|
|
3795
|
+
if (same)
|
|
3796
|
+
continue;
|
|
3797
|
+
changes.push({
|
|
3798
|
+
name,
|
|
3799
|
+
detail: `${file}: ${describe(name, versions(before), versions(after))}`,
|
|
3800
|
+
file
|
|
3801
|
+
});
|
|
3802
|
+
}
|
|
3803
|
+
const resolved = new Set(changes.map((change) => change.name));
|
|
3804
|
+
const dirs = new Set([...base.workspaces.keys(), ...head.workspaces.keys()]);
|
|
3805
|
+
for (const dir of dirs) {
|
|
3806
|
+
const declared = specChanges(base.workspaces.get(dir), head.workspaces.get(dir));
|
|
3807
|
+
for (const [name, [before, after]] of declared) {
|
|
3808
|
+
if (resolved.has(name))
|
|
3809
|
+
continue;
|
|
3810
|
+
resolved.add(name);
|
|
3811
|
+
changes.push({
|
|
3812
|
+
name,
|
|
3813
|
+
detail: `${file}: ${describe(`${name} range`, before, after)}`,
|
|
3814
|
+
file
|
|
3815
|
+
});
|
|
3816
|
+
}
|
|
3817
|
+
}
|
|
3818
|
+
return changes;
|
|
3819
|
+
}
|
|
3820
|
+
var MANIFEST = /(?:^|\/)package\.json$/;
|
|
3821
|
+
function parseManifest(text) {
|
|
3822
|
+
if (text === undefined)
|
|
3823
|
+
return;
|
|
3824
|
+
try {
|
|
3825
|
+
const value = JSON.parse(text);
|
|
3826
|
+
return isRecord3(value) ? value : undefined;
|
|
3827
|
+
} catch {
|
|
3828
|
+
return;
|
|
3829
|
+
}
|
|
3830
|
+
}
|
|
3831
|
+
async function readPackageDiff(changed, listed, base, head) {
|
|
3832
|
+
const lockfiles = changed.filter((file) => FORMATS.has(file));
|
|
3833
|
+
const manifests = changed.filter((file) => MANIFEST.test(file));
|
|
3834
|
+
if (lockfiles.length === 0 && manifests.length === 0)
|
|
3835
|
+
return;
|
|
3836
|
+
const read = (path) => Promise.all([base(path), head(path)]);
|
|
3837
|
+
const known = [...FORMATS.keys()];
|
|
3838
|
+
const [lockTexts, manifestTexts] = await Promise.all([
|
|
3839
|
+
Promise.all(known.map((file) => lockfiles.includes(file) ? read(file) : head(file).then((text) => [text, text]))),
|
|
3840
|
+
Promise.all(manifests.map(read))
|
|
3841
|
+
]);
|
|
3842
|
+
const diff = {
|
|
3843
|
+
files: new Map,
|
|
3844
|
+
changes: [],
|
|
3845
|
+
dependents: new Map,
|
|
3846
|
+
declared: new Set,
|
|
3847
|
+
bins: new Set
|
|
3848
|
+
};
|
|
3849
|
+
const present = new Map;
|
|
3850
|
+
for (const [i, file] of known.entries()) {
|
|
3851
|
+
const [baseText, headText] = lockTexts[i];
|
|
3852
|
+
const after = parse(file, headText);
|
|
3853
|
+
if (headText !== undefined)
|
|
3854
|
+
present.set(file, after);
|
|
3855
|
+
const before = parse(file, baseText);
|
|
3856
|
+
for (const lockfile of [before, after]) {
|
|
3857
|
+
if (!lockfile)
|
|
3858
|
+
continue;
|
|
3859
|
+
for (const [name, installed] of lockfile.packages) {
|
|
3860
|
+
if (installed.bin)
|
|
3861
|
+
diff.bins.add(name);
|
|
3862
|
+
for (const dependency of installed.dependencies) {
|
|
3863
|
+
const set = diff.dependents.get(dependency) ?? new Set;
|
|
3864
|
+
set.add(name);
|
|
3865
|
+
diff.dependents.set(dependency, set);
|
|
3866
|
+
}
|
|
3867
|
+
}
|
|
3868
|
+
for (const specs of lockfile.workspaces.values()) {
|
|
3869
|
+
for (const deps of specs.values()) {
|
|
3870
|
+
for (const name of deps.keys())
|
|
3871
|
+
diff.declared.add(name);
|
|
3872
|
+
}
|
|
3873
|
+
}
|
|
3874
|
+
}
|
|
3875
|
+
if (!lockfiles.includes(file))
|
|
3876
|
+
continue;
|
|
3877
|
+
if (baseText === undefined || headText === undefined) {
|
|
3878
|
+
diff.files.set(file, baseText === undefined ? "added" : "deleted");
|
|
3879
|
+
} else if (!(before && after)) {
|
|
3880
|
+
diff.files.set(file, "can't parse it");
|
|
3881
|
+
} else if (before.rest === after.rest) {
|
|
3882
|
+
diff.files.set(file, undefined);
|
|
3883
|
+
diff.changes.push(...lockfileChanges(file, before, after));
|
|
3884
|
+
} else {
|
|
3885
|
+
diff.files.set(file, "a setting other than the packages changed");
|
|
3886
|
+
}
|
|
3887
|
+
}
|
|
3888
|
+
const unread = UNREAD.some((file) => listed.includes(file));
|
|
3889
|
+
for (const [i, file] of manifests.entries()) {
|
|
3890
|
+
const [before, after] = manifestTexts[i].map(parseManifest);
|
|
3891
|
+
if (!(before && after) || unread || present.size === 0)
|
|
3892
|
+
continue;
|
|
3893
|
+
const others = (record) => restOf(record, DEP_FIELDS2);
|
|
3894
|
+
const specsBefore = specsOf(before);
|
|
3895
|
+
const specsAfter = specsOf(after);
|
|
3896
|
+
if (others(before) !== others(after) || !specsBefore || !specsAfter) {
|
|
3897
|
+
continue;
|
|
3898
|
+
}
|
|
3899
|
+
const dir = posix3.dirname(file);
|
|
3900
|
+
const declared = specChanges(specsBefore, specsAfter);
|
|
3901
|
+
const recorded = [...present.values()].every((lockfile) => {
|
|
3902
|
+
const specs = lockfile?.workspaces.get(dir);
|
|
3903
|
+
if (!specs)
|
|
3904
|
+
return false;
|
|
3905
|
+
return [...declared.keys()].every((name) => DEP_FIELDS2.every((field) => specs.get(field)?.get(name) === specsAfter.get(field)?.get(name)));
|
|
3906
|
+
});
|
|
3907
|
+
if (!recorded)
|
|
3908
|
+
continue;
|
|
3909
|
+
diff.files.set(file, undefined);
|
|
3910
|
+
for (const [name, [from, to]] of declared) {
|
|
3911
|
+
diff.declared.add(name);
|
|
3912
|
+
diff.changes.push({
|
|
3913
|
+
name,
|
|
3914
|
+
detail: `${file}: ${describe(`${name} range`, from, to)}`,
|
|
3915
|
+
file
|
|
3916
|
+
});
|
|
3917
|
+
}
|
|
3918
|
+
}
|
|
3919
|
+
return diff;
|
|
3920
|
+
}
|
|
3921
|
+
function packageSeeds(graph, config, diff, files) {
|
|
3922
|
+
const result = { seeds: new Map, notes: [], reasons: [] };
|
|
3923
|
+
const { isLoadedByName } = config;
|
|
3924
|
+
const loadedOtherwise = (name) => isLoadedByName === undefined || diff.bins.has(name) || isLoadedByName(name);
|
|
3925
|
+
const tested = new Map;
|
|
3926
|
+
const reachesTest = (name) => {
|
|
3927
|
+
let found = tested.get(name);
|
|
3928
|
+
if (found === undefined) {
|
|
3929
|
+
const node = `pkg:${name}`;
|
|
3930
|
+
found = graph.has(node) && config.suites.some((suite) => reaches(graph, [node], suite.isTest, {
|
|
3931
|
+
includeUnknown: false,
|
|
3932
|
+
types: suite.types
|
|
3933
|
+
}));
|
|
3934
|
+
tested.set(name, found);
|
|
3935
|
+
}
|
|
3936
|
+
return found;
|
|
3937
|
+
};
|
|
3938
|
+
const changes = diff.changes.filter((change) => files.includes(change.file)).sort((a, b) => byCodeUnit(a.name, b.name));
|
|
3939
|
+
const unused = new Map;
|
|
3940
|
+
const seen = new Set;
|
|
3941
|
+
for (const change of changes) {
|
|
3942
|
+
const { name, detail } = change;
|
|
3943
|
+
if (seen.has(name))
|
|
3944
|
+
continue;
|
|
3945
|
+
seen.add(name);
|
|
3946
|
+
const closure = new Set([name]);
|
|
3947
|
+
const queue = [name];
|
|
3948
|
+
for (let at = 0;at < queue.length; at++) {
|
|
3949
|
+
for (const dependent of diff.dependents.get(queue[at]) ?? []) {
|
|
3950
|
+
if (closure.has(dependent))
|
|
3951
|
+
continue;
|
|
3952
|
+
closure.add(dependent);
|
|
3953
|
+
queue.push(dependent);
|
|
3954
|
+
}
|
|
3955
|
+
}
|
|
3956
|
+
const through = [];
|
|
3957
|
+
let covered = true;
|
|
3958
|
+
let importOnly = false;
|
|
3959
|
+
for (const user of closure) {
|
|
3960
|
+
const node = `pkg:${user}`;
|
|
3961
|
+
const imported = graph.has(node);
|
|
3962
|
+
if (imported) {
|
|
3963
|
+
through.push(user);
|
|
3964
|
+
if (!result.seeds.has(node)) {
|
|
3965
|
+
result.seeds.set(node, user === name ? detail : `${detail}, which ${user} depends on`);
|
|
3966
|
+
}
|
|
3967
|
+
}
|
|
3968
|
+
const entry = imported || diff.declared.has(user) || !diff.dependents.has(user);
|
|
3969
|
+
if (!entry || reachesTest(user) || config.isInert(user))
|
|
3970
|
+
continue;
|
|
3971
|
+
if (loadedOtherwise(user)) {
|
|
3972
|
+
covered = false;
|
|
3973
|
+
const list = unused.get(user) ?? [];
|
|
3974
|
+
if (user === name)
|
|
3975
|
+
list.unshift(change);
|
|
3976
|
+
else
|
|
3977
|
+
list.push(change);
|
|
3978
|
+
unused.set(user, list);
|
|
3979
|
+
} else {
|
|
3980
|
+
importOnly = true;
|
|
3981
|
+
}
|
|
3982
|
+
}
|
|
3983
|
+
if (!covered)
|
|
3984
|
+
continue;
|
|
3985
|
+
if (through.length === 0) {
|
|
3986
|
+
result.notes.push(`${detail}, ${importOnly ? "imported by no test" : "inert"}`);
|
|
3987
|
+
continue;
|
|
3988
|
+
}
|
|
3989
|
+
const others = through.filter((user) => user !== name);
|
|
3990
|
+
const shown = others.slice(0, 3).join(", ");
|
|
3991
|
+
const rest = others.length > 3 ? ` and ${others.length - 3} more` : "";
|
|
3992
|
+
result.notes.push(others.length === 0 ? detail : `${detail}, through ${shown}${rest}`);
|
|
3993
|
+
}
|
|
3994
|
+
for (const user of sorted(unused.keys())) {
|
|
3995
|
+
const [first, ...more] = unused.get(user) ?? [];
|
|
3996
|
+
if (!first)
|
|
3997
|
+
continue;
|
|
3998
|
+
const own = first.name === user;
|
|
3999
|
+
const also = more.length === 0 ? "" : ` (and ${more.length} more ${more.length === 1 ? "package" : "packages"} ${user} depends on)`;
|
|
4000
|
+
result.reasons.push({
|
|
4001
|
+
package: user,
|
|
4002
|
+
file: first.file,
|
|
4003
|
+
message: own ? `${first.detail}${also}, and no test imports it` : `${first.detail}, which ${user} depends on${also}, and no test imports ${user}`
|
|
4004
|
+
});
|
|
4005
|
+
}
|
|
4006
|
+
return result;
|
|
4007
|
+
}
|
|
4008
|
+
|
|
4009
|
+
// src/plan.ts
|
|
4010
|
+
function formatSelection(selection) {
|
|
4011
|
+
return selection.mode === "some" ? selection.files.join(" ") : selection.mode;
|
|
4012
|
+
}
|
|
4013
|
+
function select(affected, tests) {
|
|
4014
|
+
const files = tests.filter((file) => affected.has(file));
|
|
4015
|
+
if (files.length === 0)
|
|
4016
|
+
return { mode: "none" };
|
|
4017
|
+
if (files.length === tests.length)
|
|
4018
|
+
return { mode: "all" };
|
|
4019
|
+
return { mode: "some", files };
|
|
4020
|
+
}
|
|
4021
|
+
function classify(graph, changed, config, packages, settings) {
|
|
4022
|
+
const causes = [];
|
|
4023
|
+
const all = config.suites.map((suite) => suite.name);
|
|
4024
|
+
const runAll = (cause) => causes.push({ ...cause, suites: all });
|
|
4025
|
+
const relevant = [];
|
|
4026
|
+
const quiet = [];
|
|
4027
|
+
const packageFiles = [];
|
|
4028
|
+
const settingFiles = [];
|
|
4029
|
+
for (const file of changed) {
|
|
4030
|
+
const read = graph.has(file);
|
|
4031
|
+
const ignored = config.isIgnored(file);
|
|
4032
|
+
if (!read && ignored)
|
|
4033
|
+
continue;
|
|
4034
|
+
const wired = config.suites.filter((suite) => suite.isWiring(file));
|
|
4035
|
+
const diffed = packages?.files.has(file) ?? false;
|
|
4036
|
+
if (config.isSelf(file)) {
|
|
4037
|
+
runAll({
|
|
4038
|
+
kind: "self",
|
|
4039
|
+
file,
|
|
4040
|
+
message: `${file}: test selector changed`
|
|
4041
|
+
});
|
|
4042
|
+
continue;
|
|
4043
|
+
}
|
|
4044
|
+
for (const suite of wired) {
|
|
4045
|
+
causes.push({
|
|
4046
|
+
kind: "wiring",
|
|
4047
|
+
suites: [suite.name],
|
|
4048
|
+
file,
|
|
4049
|
+
message: `${file}: wires the ${suite.name} suite`
|
|
4050
|
+
});
|
|
4051
|
+
}
|
|
4052
|
+
if (wired.length > 0 && !diffed)
|
|
4053
|
+
continue;
|
|
4054
|
+
if (diffed) {
|
|
4055
|
+
const reason = packages?.files.get(file);
|
|
4056
|
+
if (reason === undefined) {
|
|
4057
|
+
packageFiles.push(file);
|
|
4058
|
+
} else {
|
|
4059
|
+
runAll({ kind: "lockfile", file, message: `${file}: ${reason}` });
|
|
4060
|
+
}
|
|
4061
|
+
} else if (read || config.roots.some((root) => inRoot(file, root))) {
|
|
4062
|
+
if (ignored)
|
|
4063
|
+
quiet.push(file);
|
|
4064
|
+
else
|
|
4065
|
+
((settings?.has(file)) ? settingFiles : relevant).push(file);
|
|
4066
|
+
} else {
|
|
4067
|
+
runAll({
|
|
4068
|
+
kind: "outside",
|
|
4069
|
+
file,
|
|
4070
|
+
message: `${file}: outside the dependency graph`
|
|
4071
|
+
});
|
|
4072
|
+
}
|
|
4073
|
+
}
|
|
4074
|
+
return { relevant, quiet, packageFiles, settingFiles, causes };
|
|
4075
|
+
}
|
|
4076
|
+
function narrowedWalk(graph, seeds, {
|
|
4077
|
+
quiet,
|
|
4078
|
+
layout,
|
|
4079
|
+
types
|
|
4080
|
+
}, coarse, narrowings) {
|
|
4081
|
+
const matched = new Set(narrowings.flatMap((narrowing) => narrowing.matched));
|
|
4082
|
+
const fine = affectedBy(graph, seeds.filter((seed) => !matched.has(seed)), { quiet, layout, types });
|
|
4083
|
+
for (const narrowing of narrowings) {
|
|
4084
|
+
const walk = affectedBy(graph, [...narrowing.matched, ...narrowing.seeds], {
|
|
4085
|
+
opaque: narrowing.narrower.opaque,
|
|
4086
|
+
layout,
|
|
4087
|
+
types
|
|
4088
|
+
});
|
|
4089
|
+
for (const node of walk)
|
|
4090
|
+
fine.add(node);
|
|
4091
|
+
}
|
|
4092
|
+
return new Set([...fine].filter((node) => coarse.has(node)));
|
|
4093
|
+
}
|
|
4094
|
+
function orphanOf(graph, config, suite, file) {
|
|
4095
|
+
if (!suite.roots.some((root) => inRoot(file, root)))
|
|
4096
|
+
return;
|
|
4097
|
+
const wired = (path) => suite.isTest(path) || suite.isPage(path);
|
|
4098
|
+
if (reaches(graph, [file], wired, {
|
|
4099
|
+
includeUnknown: false,
|
|
4100
|
+
types: suite.types || DECLARATION.test(file)
|
|
4101
|
+
})) {
|
|
4102
|
+
return;
|
|
4103
|
+
}
|
|
4104
|
+
const deleted = !graph.has(file);
|
|
4105
|
+
if (deleted && suite.isTest(file))
|
|
4106
|
+
return;
|
|
4107
|
+
if (deleted && config.isImportOnly(file))
|
|
4108
|
+
return;
|
|
4109
|
+
if (suite.isNonInput(file))
|
|
4110
|
+
return;
|
|
4111
|
+
const typed = !suite.types && reaches(graph, [file], suite.isTest, { includeUnknown: false });
|
|
4112
|
+
return { typed };
|
|
4113
|
+
}
|
|
4114
|
+
function planTests(graph, changed, config, {
|
|
4115
|
+
barrelNames = [],
|
|
4116
|
+
layout = new Set,
|
|
4117
|
+
narrowed = [],
|
|
4118
|
+
packages,
|
|
4119
|
+
settings
|
|
4120
|
+
} = {}) {
|
|
4121
|
+
const { relevant, quiet, packageFiles, settingFiles, causes } = classify(graph, changed, config, packages, settings);
|
|
4122
|
+
const settingCauses = new Map;
|
|
4123
|
+
const settingNotes = [];
|
|
4124
|
+
for (const file of settingFiles) {
|
|
4125
|
+
const change = settings?.get(file);
|
|
4126
|
+
if (!change)
|
|
4127
|
+
continue;
|
|
4128
|
+
settingNotes.push(change.note);
|
|
4129
|
+
for (const seed of change.seeds) {
|
|
4130
|
+
if (!settingCauses.has(seed)) {
|
|
4131
|
+
settingCauses.set(seed, `governed by ${file}, which changed`);
|
|
4132
|
+
}
|
|
4133
|
+
}
|
|
4134
|
+
}
|
|
4135
|
+
const bumped = packages && packageFiles.length > 0 ? packageSeeds(graph, config, packages, packageFiles) : { seeds: new Map, notes: [], reasons: [] };
|
|
4136
|
+
const all = config.suites.map((suite) => suite.name);
|
|
4137
|
+
for (const unused of bumped.reasons) {
|
|
4138
|
+
causes.push({ kind: "package", suites: all, ...unused });
|
|
4139
|
+
}
|
|
4140
|
+
const seeds = [
|
|
4141
|
+
...relevant,
|
|
4142
|
+
...settingFiles,
|
|
4143
|
+
...settingCauses.keys(),
|
|
4144
|
+
...barrelNames,
|
|
4145
|
+
...packageFiles,
|
|
4146
|
+
...bumped.seeds.keys()
|
|
4147
|
+
];
|
|
4148
|
+
const affected = walksByTypes(graph, seeds, { quiet, layout });
|
|
4149
|
+
const nodes = [...graph.keys()];
|
|
4150
|
+
const tests = new Map(config.suites.map((suite) => [suite.name, nodes.filter(suite.isTest)]));
|
|
4151
|
+
for (const suite of config.suites) {
|
|
4152
|
+
for (const file of relevant) {
|
|
4153
|
+
const orphan = orphanOf(graph, config, suite, file);
|
|
4154
|
+
if (!orphan)
|
|
4155
|
+
continue;
|
|
4156
|
+
const { typed } = orphan;
|
|
4157
|
+
causes.push({
|
|
4158
|
+
kind: "orphan",
|
|
4159
|
+
suites: [suite.name],
|
|
4160
|
+
file,
|
|
4161
|
+
message: `${file}: no ${suite.name} test imports it${typed ? " (only type imports reach it: list it in the suite's `nonInputs` if nothing loads it at runtime)" : ""}`
|
|
4162
|
+
});
|
|
4163
|
+
}
|
|
4164
|
+
}
|
|
4165
|
+
const narrowings = new Map;
|
|
4166
|
+
for (const { narrower, result } of narrowed) {
|
|
4167
|
+
const matched = relevant.filter(narrower.match);
|
|
4168
|
+
if (matched.length === 0)
|
|
4169
|
+
continue;
|
|
4170
|
+
if ("all" in result) {
|
|
4171
|
+
causes.push({
|
|
4172
|
+
kind: "narrower",
|
|
4173
|
+
suites: narrower.suites,
|
|
4174
|
+
narrower: narrower.name,
|
|
4175
|
+
message: `${narrower.name}: ${result.all}`
|
|
4176
|
+
});
|
|
4177
|
+
continue;
|
|
4178
|
+
}
|
|
4179
|
+
for (const name of narrower.suites) {
|
|
4180
|
+
const list = narrowings.get(name) ?? [];
|
|
4181
|
+
list.push({ matched, narrower, seeds: result.seeds });
|
|
4182
|
+
narrowings.set(name, list);
|
|
4183
|
+
}
|
|
4184
|
+
}
|
|
4185
|
+
const full = new Set(causes.flatMap((cause) => cause.suites));
|
|
4186
|
+
const suites = {};
|
|
4187
|
+
for (const suite of config.suites) {
|
|
4188
|
+
if (full.has(suite.name)) {
|
|
4189
|
+
suites[suite.name] = { mode: "all" };
|
|
4190
|
+
continue;
|
|
4191
|
+
}
|
|
4192
|
+
const coarse = affected(suite.types);
|
|
4193
|
+
const narrowing = narrowings.get(suite.name);
|
|
4194
|
+
suites[suite.name] = select(narrowing ? narrowedWalk(graph, seeds, { quiet, layout, types: suite.types }, coarse, narrowing) : coarse, tests.get(suite.name) ?? []);
|
|
4195
|
+
}
|
|
4196
|
+
return {
|
|
4197
|
+
suites,
|
|
4198
|
+
reasons: causes.map((cause) => cause.message),
|
|
4199
|
+
causes,
|
|
4200
|
+
totals: Object.fromEntries(config.suites.map((suite) => [
|
|
4201
|
+
suite.name,
|
|
4202
|
+
tests.get(suite.name)?.length ?? 0
|
|
4203
|
+
])),
|
|
4204
|
+
full: [...full],
|
|
4205
|
+
packages: bumped.notes,
|
|
4206
|
+
settings: settingNotes,
|
|
4207
|
+
settingCauses,
|
|
4208
|
+
seeds,
|
|
4209
|
+
quiet,
|
|
4210
|
+
layout,
|
|
4211
|
+
packageCauses: bumped.seeds,
|
|
4212
|
+
narrowings
|
|
4213
|
+
};
|
|
4214
|
+
}
|
|
4215
|
+
|
|
4216
|
+
// src/settings.ts
|
|
4217
|
+
import { posix as posix5 } from "node:path";
|
|
4218
|
+
|
|
4219
|
+
// src/text.ts
|
|
4220
|
+
function plural(count, one, many = `${one}s`) {
|
|
4221
|
+
return `${count} ${count === 1 ? one : many}`;
|
|
4222
|
+
}
|
|
4223
|
+
|
|
4224
|
+
// src/tsconfig.ts
|
|
4225
|
+
import { posix as posix4 } from "node:path";
|
|
4226
|
+
var COMMENT2 = /("(?:[^"\\]|\\.)*")|\/\/[^\n]*|\/\*[\s\S]*?\*\//g;
|
|
4227
|
+
var TRAILING_COMMA = /("(?:[^"\\]|\\.)*")|,(?=\s*[}\]])/g;
|
|
4228
|
+
var BOM = /^/;
|
|
4229
|
+
var RELATIVE2 = /^\.\.?(?:\/|$)/;
|
|
4230
|
+
function parseJsonc(text) {
|
|
4231
|
+
return JSON.parse(text.replace(BOM, "").replace(COMMENT2, (_, string) => string ?? " ").replace(TRAILING_COMMA, (_, string) => string ?? ""));
|
|
4232
|
+
}
|
|
4233
|
+
function isStrings2(value) {
|
|
4234
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
4235
|
+
}
|
|
4236
|
+
function parse2(file, text) {
|
|
4237
|
+
let value;
|
|
4238
|
+
try {
|
|
4239
|
+
value = parseJsonc(text);
|
|
4240
|
+
} catch (error) {
|
|
4241
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
4242
|
+
}
|
|
4243
|
+
if (!isRecord2(value))
|
|
4244
|
+
return { error: "not an object" };
|
|
4245
|
+
const dir = posix4.dirname(file);
|
|
4246
|
+
const { extends: extended } = value;
|
|
4247
|
+
const read = {
|
|
4248
|
+
extends: typeof extended === "string" ? [extended] : []
|
|
4249
|
+
};
|
|
4250
|
+
if (isStrings2(extended))
|
|
4251
|
+
read.extends = extended;
|
|
4252
|
+
const options = value.compilerOptions;
|
|
4253
|
+
if (isRecord2(options)) {
|
|
4254
|
+
if (typeof options.baseUrl === "string") {
|
|
4255
|
+
read.baseUrl = posix4.join(dir, options.baseUrl);
|
|
4256
|
+
}
|
|
4257
|
+
if (isRecord2(options.paths)) {
|
|
4258
|
+
const map = {};
|
|
4259
|
+
for (const [key, targets] of Object.entries(options.paths)) {
|
|
4260
|
+
if (isStrings2(targets))
|
|
4261
|
+
map[key] = targets;
|
|
4262
|
+
}
|
|
4263
|
+
read.paths = { map, dir };
|
|
4264
|
+
}
|
|
4265
|
+
}
|
|
4266
|
+
return { read };
|
|
4267
|
+
}
|
|
4268
|
+
function extendsPath(file, target) {
|
|
4269
|
+
if (!RELATIVE2.test(target))
|
|
4270
|
+
return;
|
|
4271
|
+
const path = posix4.join(posix4.dirname(file), target);
|
|
4272
|
+
return path.startsWith("../") || path === ".." ? undefined : path;
|
|
4273
|
+
}
|
|
4274
|
+
async function readTsconfigs(files, read) {
|
|
4275
|
+
const parsed = new Map;
|
|
4276
|
+
const load = async (file, extended) => {
|
|
4277
|
+
if (parsed.has(file))
|
|
4278
|
+
return;
|
|
4279
|
+
parsed.set(file, undefined);
|
|
4280
|
+
const text = await read(file, extended);
|
|
4281
|
+
if (text === undefined) {
|
|
4282
|
+
if (!file.endsWith(".json"))
|
|
4283
|
+
await load(`${file}.json`, true);
|
|
4284
|
+
return;
|
|
4285
|
+
}
|
|
4286
|
+
const result = parse2(file, text);
|
|
4287
|
+
parsed.set(file, result);
|
|
4288
|
+
if (!("read" in result))
|
|
4289
|
+
return;
|
|
4290
|
+
await Promise.all(result.read.extends.map((target) => {
|
|
4291
|
+
const path = extendsPath(file, target);
|
|
4292
|
+
return path === undefined ? undefined : load(path, true);
|
|
4293
|
+
}));
|
|
4294
|
+
};
|
|
4295
|
+
await Promise.all(files.map((file) => load(file, false)));
|
|
4296
|
+
const found = (path) => {
|
|
4297
|
+
const at = parsed.get(path);
|
|
4298
|
+
if (at !== undefined || path.endsWith(".json"))
|
|
4299
|
+
return at;
|
|
4300
|
+
return parsed.get(`${path}.json`);
|
|
4301
|
+
};
|
|
4302
|
+
const effective = (file, seen) => {
|
|
4303
|
+
const at = found(file);
|
|
4304
|
+
if (at === undefined)
|
|
4305
|
+
return { extends: [] };
|
|
4306
|
+
if (!("read" in at) || seen.has(file))
|
|
4307
|
+
return;
|
|
4308
|
+
const chain = new Set([...seen, file]);
|
|
4309
|
+
const merged = { extends: [] };
|
|
4310
|
+
for (const target of at.read.extends) {
|
|
4311
|
+
const path = extendsPath(file, target);
|
|
4312
|
+
if (path === undefined)
|
|
4313
|
+
continue;
|
|
4314
|
+
const base = effective(path, chain);
|
|
4315
|
+
if (base === undefined)
|
|
4316
|
+
return;
|
|
4317
|
+
if (base.baseUrl !== undefined)
|
|
4318
|
+
merged.baseUrl = base.baseUrl;
|
|
4319
|
+
if (base.paths !== undefined)
|
|
4320
|
+
merged.paths = base.paths;
|
|
4321
|
+
}
|
|
4322
|
+
if (at.read.baseUrl !== undefined)
|
|
4323
|
+
merged.baseUrl = at.read.baseUrl;
|
|
4324
|
+
if (at.read.paths !== undefined)
|
|
4325
|
+
merged.paths = at.read.paths;
|
|
4326
|
+
return merged;
|
|
4327
|
+
};
|
|
4328
|
+
const chain = (file, into) => {
|
|
4329
|
+
if (into.has(file))
|
|
4330
|
+
return;
|
|
4331
|
+
into.add(file);
|
|
4332
|
+
const at = found(file);
|
|
4333
|
+
if (at === undefined && !file.endsWith(".json"))
|
|
4334
|
+
into.add(`${file}.json`);
|
|
4335
|
+
if (at === undefined || !("read" in at))
|
|
4336
|
+
return;
|
|
4337
|
+
for (const target of at.read.extends) {
|
|
4338
|
+
const path = extendsPath(file, target);
|
|
4339
|
+
if (path !== undefined)
|
|
4340
|
+
chain(path, into);
|
|
4341
|
+
}
|
|
4342
|
+
};
|
|
4343
|
+
const tsconfigs = new Map;
|
|
4344
|
+
const sources = new Map;
|
|
4345
|
+
for (const file of files) {
|
|
4346
|
+
if (parsed.get(file) === undefined)
|
|
4347
|
+
continue;
|
|
4348
|
+
const chained = new Set;
|
|
4349
|
+
chain(file, chained);
|
|
4350
|
+
sources.set(posix4.dirname(file), [...chained]);
|
|
4351
|
+
const options = effective(file, new Set);
|
|
4352
|
+
const resolved = { paths: {} };
|
|
4353
|
+
if (options?.baseUrl !== undefined)
|
|
4354
|
+
resolved.baseUrl = options.baseUrl;
|
|
4355
|
+
const { paths } = options ?? {};
|
|
4356
|
+
if (paths) {
|
|
4357
|
+
const base = options?.baseUrl ?? paths.dir;
|
|
4358
|
+
for (const [key, targets] of Object.entries(paths.map)) {
|
|
4359
|
+
resolved.paths[key] = targets.map((target) => posix4.join(base, target));
|
|
4360
|
+
}
|
|
4361
|
+
}
|
|
4362
|
+
tsconfigs.set(posix4.dirname(file), resolved);
|
|
4363
|
+
}
|
|
4364
|
+
const unparsed = [];
|
|
4365
|
+
for (const file of sorted(parsed.keys())) {
|
|
4366
|
+
const at = parsed.get(file);
|
|
4367
|
+
if (at && "error" in at)
|
|
4368
|
+
unparsed.push({ file, reason: at.error });
|
|
4369
|
+
}
|
|
4370
|
+
return { tsconfigs, sources, unparsed };
|
|
4371
|
+
}
|
|
4372
|
+
|
|
4373
|
+
// src/settings.ts
|
|
4374
|
+
var METADATA = new Set([
|
|
4375
|
+
"author",
|
|
4376
|
+
"bugs",
|
|
4377
|
+
"contributors",
|
|
4378
|
+
"description",
|
|
4379
|
+
"engines",
|
|
4380
|
+
"files",
|
|
4381
|
+
"funding",
|
|
4382
|
+
"homepage",
|
|
4383
|
+
"keywords",
|
|
4384
|
+
"license",
|
|
4385
|
+
"maintainers",
|
|
4386
|
+
"private",
|
|
4387
|
+
"publishConfig",
|
|
4388
|
+
"repository",
|
|
4389
|
+
"version"
|
|
4390
|
+
]);
|
|
4391
|
+
var MANIFEST2 = /(?:^|\/)package\.json$/;
|
|
4392
|
+
var TSCONFIG = /(?:^|\/)tsconfig\.json$/;
|
|
4393
|
+
function within(file, dir) {
|
|
4394
|
+
return dir === "." || file.startsWith(`${dir}/`);
|
|
4395
|
+
}
|
|
4396
|
+
function governed(files, dirs, dir) {
|
|
4397
|
+
const deeper = dirs.filter((other) => other !== dir && within(other, dir));
|
|
4398
|
+
return files.filter((file) => within(file, dir) && !deeper.some((other) => within(file, other)));
|
|
4399
|
+
}
|
|
4400
|
+
function parsed(text, jsonc) {
|
|
4401
|
+
if (text === undefined)
|
|
4402
|
+
return;
|
|
4403
|
+
try {
|
|
4404
|
+
return jsonc ? parseJsonc(text) : JSON.parse(text);
|
|
4405
|
+
} catch {
|
|
4406
|
+
return null;
|
|
4407
|
+
}
|
|
4408
|
+
}
|
|
4409
|
+
function changedKeys(before, after) {
|
|
4410
|
+
if (!(isRecord2(before) && isRecord2(after)))
|
|
4411
|
+
return;
|
|
4412
|
+
const keys = new Set([...Object.keys(before), ...Object.keys(after)]);
|
|
4413
|
+
return [...keys].filter((key) => JSON.stringify(before[key]) !== JSON.stringify(after[key]));
|
|
4414
|
+
}
|
|
4415
|
+
async function settingChanges({
|
|
4416
|
+
changed,
|
|
4417
|
+
files,
|
|
4418
|
+
tsconfigs,
|
|
4419
|
+
packages,
|
|
4420
|
+
accounted,
|
|
4421
|
+
base,
|
|
4422
|
+
head
|
|
4423
|
+
}) {
|
|
4424
|
+
const tsDirs = [...tsconfigs.keys()];
|
|
4425
|
+
const packageDirs = [...packages];
|
|
4426
|
+
const candidates = changed.filter((file) => TSCONFIG.test(file) || MANIFEST2.test(file) && !accounted.has(file) || tsDirs.some((dir) => tsconfigs.get(dir)?.includes(file)));
|
|
4427
|
+
const texts = await Promise.all(candidates.map((file) => Promise.all([base(file), head(file)])));
|
|
4428
|
+
const result = new Map;
|
|
4429
|
+
for (const [i, file] of candidates.entries()) {
|
|
4430
|
+
const [before, after] = texts[i];
|
|
4431
|
+
const manifest = MANIFEST2.test(file);
|
|
4432
|
+
const keys = changedKeys(parsed(before, !manifest), parsed(after, !manifest));
|
|
4433
|
+
const counted = keys?.filter((key) => !(manifest && METADATA.has(key)));
|
|
4434
|
+
const own = posix5.dirname(file);
|
|
4435
|
+
const dirs = manifest ? [own] : tsDirs.filter((dir) => tsconfigs.get(dir)?.includes(file));
|
|
4436
|
+
if (TSCONFIG.test(file) && !dirs.includes(own))
|
|
4437
|
+
dirs.push(own);
|
|
4438
|
+
const kinds = manifest ? packageDirs : tsDirs;
|
|
4439
|
+
const all = [...new Set([...kinds, ...dirs])];
|
|
4440
|
+
if (keys !== undefined && counted?.length === 0) {
|
|
4441
|
+
const what = keys.length === 0 ? "only formatting or comments" : `only ${keys.join(", ")}`;
|
|
4442
|
+
result.set(file, {
|
|
4443
|
+
seeds: [],
|
|
4444
|
+
note: `${file}: ${what} changed, so no file resolves or runs differently`
|
|
4445
|
+
});
|
|
4446
|
+
continue;
|
|
4447
|
+
}
|
|
4448
|
+
const seeds = [
|
|
4449
|
+
...new Set(dirs.flatMap((dir) => governed(files, all, dir)))
|
|
4450
|
+
];
|
|
4451
|
+
const what = manifest ? "resolve or run" : "resolve or compile";
|
|
4452
|
+
result.set(file, {
|
|
4453
|
+
seeds,
|
|
4454
|
+
note: `${file}: changes how the ${plural(seeds.length, "file")} it governs ${what}`
|
|
4455
|
+
});
|
|
4456
|
+
}
|
|
4457
|
+
return result;
|
|
4458
|
+
}
|
|
4459
|
+
|
|
4460
|
+
// src/compute.ts
|
|
4461
|
+
function hasCode(error, codes) {
|
|
4462
|
+
return error instanceof Error && "code" in error && typeof error.code === "string" && codes.includes(error.code);
|
|
4463
|
+
}
|
|
4464
|
+
async function readIfFile(path) {
|
|
4465
|
+
try {
|
|
4466
|
+
return await readFile3(path, "utf-8");
|
|
4467
|
+
} catch (error) {
|
|
4468
|
+
if (hasCode(error, ["ENOENT", "EISDIR"]))
|
|
4469
|
+
return;
|
|
4470
|
+
throw error;
|
|
4471
|
+
}
|
|
4472
|
+
}
|
|
4473
|
+
function collectPackages(rootJson, sources) {
|
|
4474
|
+
const packages = new Map;
|
|
4475
|
+
const parse = (text) => {
|
|
4476
|
+
try {
|
|
4477
|
+
return toPackageJson(JSON.parse(text));
|
|
4478
|
+
} catch {
|
|
4479
|
+
return {};
|
|
4480
|
+
}
|
|
4481
|
+
};
|
|
4482
|
+
if (rootJson !== undefined)
|
|
4483
|
+
packages.set(".", parse(rootJson));
|
|
4484
|
+
for (const [path, text] of sources) {
|
|
4485
|
+
if (path === "package.json" || !path.endsWith("/package.json"))
|
|
4486
|
+
continue;
|
|
4487
|
+
packages.set(posix6.dirname(path), parse(text));
|
|
4488
|
+
}
|
|
4489
|
+
return packages;
|
|
4490
|
+
}
|
|
4491
|
+
var READS_IN_FLIGHT = 64;
|
|
4492
|
+
async function inParallel(tasks, limit) {
|
|
4493
|
+
let next = 0;
|
|
4494
|
+
const worker = () => next < tasks.length ? tasks[next++]().then(worker) : Promise.resolve();
|
|
4495
|
+
await Promise.all(Array.from({ length: Math.min(limit, tasks.length) }, worker));
|
|
4496
|
+
}
|
|
4497
|
+
async function isFile(path) {
|
|
4498
|
+
try {
|
|
4499
|
+
return (await stat(path)).isFile();
|
|
4500
|
+
} catch (error) {
|
|
4501
|
+
if (hasCode(error, ["ENOENT", "ENOTDIR"]))
|
|
4502
|
+
return false;
|
|
4503
|
+
throw error;
|
|
4504
|
+
}
|
|
4505
|
+
}
|
|
4506
|
+
async function extractRevision(root, revision, cache) {
|
|
4507
|
+
const extractions = new Map;
|
|
4508
|
+
const texts = new Map;
|
|
4509
|
+
const missing = new Set;
|
|
4510
|
+
const tasks = [];
|
|
4511
|
+
let cached = 0;
|
|
4512
|
+
const read = async (file) => {
|
|
4513
|
+
const text = await readIfFile(join4(root, file));
|
|
4514
|
+
if (text === undefined)
|
|
4515
|
+
missing.add(file);
|
|
4516
|
+
return text;
|
|
4517
|
+
};
|
|
4518
|
+
for (const file of revision.files) {
|
|
4519
|
+
if (SCANNED.test(file)) {
|
|
4520
|
+
const format = formatOf(file);
|
|
4521
|
+
const oid = revision.blobs.get(file);
|
|
4522
|
+
const hit = oid === undefined ? undefined : cache.get(oid, format);
|
|
4523
|
+
if (hit) {
|
|
4524
|
+
extractions.set(file, hit);
|
|
4525
|
+
cached++;
|
|
4526
|
+
continue;
|
|
4527
|
+
}
|
|
4528
|
+
tasks.push(() => read(file).then((source) => {
|
|
4529
|
+
if (source === undefined)
|
|
4530
|
+
return;
|
|
4531
|
+
const extraction = extract(format, source);
|
|
4532
|
+
extractions.set(file, extraction);
|
|
4533
|
+
if (oid !== undefined)
|
|
4534
|
+
cache.set(oid, format, extraction);
|
|
4535
|
+
}));
|
|
4536
|
+
} else if (file.endsWith("/package.json")) {
|
|
4537
|
+
tasks.push(() => read(file).then((source) => {
|
|
4538
|
+
if (source !== undefined)
|
|
4539
|
+
texts.set(file, source);
|
|
4540
|
+
}));
|
|
4541
|
+
} else if (!revision.present.has(file)) {
|
|
4542
|
+
tasks.push(() => isFile(join4(root, file)).then((found) => {
|
|
4543
|
+
if (!found)
|
|
4544
|
+
missing.add(file);
|
|
4545
|
+
}));
|
|
4546
|
+
}
|
|
4547
|
+
}
|
|
4548
|
+
await inParallel(tasks, READS_IN_FLIGHT);
|
|
4549
|
+
const files = revision.files.filter((file) => !missing.has(file));
|
|
4550
|
+
const manifests = new Map;
|
|
4551
|
+
for (const file of files) {
|
|
4552
|
+
const text = texts.get(file);
|
|
4553
|
+
if (text !== undefined)
|
|
4554
|
+
manifests.set(file, text);
|
|
4555
|
+
}
|
|
4556
|
+
return {
|
|
4557
|
+
files,
|
|
4558
|
+
extractions,
|
|
4559
|
+
manifests,
|
|
4560
|
+
extracted: { cached, read: extractions.size - cached }
|
|
4561
|
+
};
|
|
4562
|
+
}
|
|
4563
|
+
function graphView(graph, config) {
|
|
4564
|
+
const nodes = [...graph.keys()];
|
|
4565
|
+
return {
|
|
4566
|
+
nodes: () => nodes,
|
|
4567
|
+
importers: (node) => importersOf(graph, node),
|
|
4568
|
+
dependents: (from) => affectedBy(graph, from, { includeUnknown: false }),
|
|
4569
|
+
tests: (name) => {
|
|
4570
|
+
const suite = config.suites.find((candidate) => candidate.name === name);
|
|
4571
|
+
if (!suite)
|
|
4572
|
+
throw new Error(`bivium: no suite "${name}"`);
|
|
4573
|
+
return nodes.filter(suite.isTest);
|
|
4574
|
+
}
|
|
4575
|
+
};
|
|
4576
|
+
}
|
|
4577
|
+
function messageOf(error) {
|
|
4578
|
+
return error instanceof Error ? error.message : String(error);
|
|
4579
|
+
}
|
|
4580
|
+
async function runNarrowers(root, revision, graph, config, suites, packages, settings) {
|
|
4581
|
+
const { relevant } = classify(graph, revision.changed, config, packages, settings);
|
|
4582
|
+
const wanted = config.narrowers.filter((narrower) => relevant.some(narrower.match) && (suites === undefined || narrower.suites.some((name) => suites.includes(name))));
|
|
4583
|
+
if (wanted.length === 0)
|
|
4584
|
+
return [];
|
|
4585
|
+
const view = graphView(graph, config);
|
|
4586
|
+
const base = {
|
|
4587
|
+
commit: await revision.mergeBase(),
|
|
4588
|
+
read: revision.readBase
|
|
4589
|
+
};
|
|
4590
|
+
const head = {
|
|
4591
|
+
commit: undefined,
|
|
4592
|
+
read: (path) => readIfFile(join4(root, path))
|
|
4593
|
+
};
|
|
4594
|
+
return Promise.all(wanted.map(async (narrower) => {
|
|
4595
|
+
const changed = relevant.filter(narrower.match);
|
|
4596
|
+
try {
|
|
4597
|
+
const result = await narrower.narrow({
|
|
4598
|
+
root,
|
|
4599
|
+
base,
|
|
4600
|
+
head,
|
|
4601
|
+
changed,
|
|
4602
|
+
graph: view
|
|
4603
|
+
});
|
|
4604
|
+
return { narrower, result };
|
|
4605
|
+
} catch (error) {
|
|
4606
|
+
return { narrower, result: { all: `failed: ${messageOf(error)}` } };
|
|
4607
|
+
}
|
|
4608
|
+
}));
|
|
4609
|
+
}
|
|
4610
|
+
async function computePlan(root, base, config, {
|
|
4611
|
+
cache: cacheOption = config.cache,
|
|
4612
|
+
suites,
|
|
4613
|
+
mergeBase = false
|
|
4614
|
+
} = {}) {
|
|
4615
|
+
const dir = cacheDir(root, cacheOption);
|
|
4616
|
+
const [revision, rootJson, cache] = await Promise.all([
|
|
4617
|
+
readRevision(root, base, config.roots, {
|
|
4618
|
+
blobs: dir !== undefined,
|
|
4619
|
+
mergeBase
|
|
4620
|
+
}),
|
|
4621
|
+
readFile3(join4(root, "package.json"), "utf-8").catch(() => {
|
|
4622
|
+
return;
|
|
4623
|
+
}),
|
|
4624
|
+
ExtractionCache.open(dir)
|
|
4625
|
+
]);
|
|
4626
|
+
if (revision.changed.some((file) => config.narrowers.some((narrower) => narrower.match(file)))) {
|
|
4627
|
+
revision.mergeBase();
|
|
4628
|
+
}
|
|
4629
|
+
let listed;
|
|
4630
|
+
const readTsconfig = async (file, extended) => {
|
|
4631
|
+
if (extended) {
|
|
4632
|
+
listed ??= new Set(revision.listed);
|
|
4633
|
+
if (!listed.has(file))
|
|
4634
|
+
return;
|
|
4635
|
+
}
|
|
4636
|
+
return readIfFile(join4(root, file));
|
|
4637
|
+
};
|
|
4638
|
+
const [{ files, extractions, manifests, extracted }, ts, packageDiff] = await Promise.all([
|
|
4639
|
+
extractRevision(root, revision, cache),
|
|
4640
|
+
readTsconfigs([
|
|
4641
|
+
"tsconfig.json",
|
|
4642
|
+
...revision.files.filter((file) => file.endsWith("/tsconfig.json"))
|
|
4643
|
+
], readTsconfig),
|
|
4644
|
+
readPackageDiff(revision.changed, revision.listed, revision.readBase, (path) => readIfFile(join4(root, path)))
|
|
4645
|
+
]);
|
|
4646
|
+
const { tsconfigs } = ts;
|
|
4647
|
+
const basePending = new Map(revision.changed.filter((file) => config.isBarrel(file) && extractions.get(file)?.barrel).map((file) => [file, revision.readBase(file)]));
|
|
4648
|
+
const packages = collectPackages(rootJson, manifests);
|
|
4649
|
+
const graph = graphFrom(files, extractions, {
|
|
4650
|
+
config,
|
|
4651
|
+
packages,
|
|
4652
|
+
tsconfigs
|
|
4653
|
+
});
|
|
4654
|
+
const saved = cache.save();
|
|
4655
|
+
const settings = await settingChanges({
|
|
4656
|
+
changed: revision.changed,
|
|
4657
|
+
files,
|
|
4658
|
+
tsconfigs: ts.sources,
|
|
4659
|
+
packages: packages.keys(),
|
|
4660
|
+
accounted: new Set(packageDiff?.files.keys()),
|
|
4661
|
+
base: revision.readBase,
|
|
4662
|
+
head: (path) => readIfFile(join4(root, path))
|
|
4663
|
+
});
|
|
4664
|
+
const moved = await Promise.all([...basePending].map(async ([file, pending]) => {
|
|
4665
|
+
const head = graph.get(file)?.barrel;
|
|
4666
|
+
if (!head)
|
|
4667
|
+
return [];
|
|
4668
|
+
const before = baseBarrelNames(file, await pending, { config, packages, tsconfigs }, graph);
|
|
4669
|
+
const names = before ? changedBarrelNames(before, head) : [...head.keys()];
|
|
4670
|
+
return names.map((name) => `${file}#${name}`);
|
|
4671
|
+
}));
|
|
4672
|
+
const barrelNames = moved.flat();
|
|
4673
|
+
const narrowed = await runNarrowers(root, revision, graph, config, suites, packageDiff, settings);
|
|
4674
|
+
const planned = planTests(graph, revision.changed, config, {
|
|
4675
|
+
barrelNames,
|
|
4676
|
+
layout: revision.layout,
|
|
4677
|
+
narrowed,
|
|
4678
|
+
packages: packageDiff,
|
|
4679
|
+
settings
|
|
4680
|
+
});
|
|
4681
|
+
await saved;
|
|
4682
|
+
return {
|
|
4683
|
+
plan: planned,
|
|
4684
|
+
graph,
|
|
4685
|
+
changed: revision.changed,
|
|
4686
|
+
mergeBase: revision.mergeBase,
|
|
4687
|
+
barrelNames,
|
|
4688
|
+
narrowed,
|
|
4689
|
+
packages,
|
|
4690
|
+
tsconfigs,
|
|
4691
|
+
unparsedTsconfigs: ts.unparsed,
|
|
4692
|
+
extractions,
|
|
4693
|
+
listed: revision.listed,
|
|
4694
|
+
fallbacks: new Map([...extractions].flatMap(([file, extraction]) => extraction.fallback === undefined ? [] : [[file, extraction.fallback]])),
|
|
4695
|
+
extracted
|
|
4696
|
+
};
|
|
4697
|
+
}
|
|
4698
|
+
async function prepare({
|
|
4699
|
+
cwd = process.cwd(),
|
|
4700
|
+
base,
|
|
4701
|
+
config,
|
|
4702
|
+
configFile
|
|
4703
|
+
}) {
|
|
4704
|
+
const root = resolve2(cwd);
|
|
4705
|
+
let resolved;
|
|
4706
|
+
if (config) {
|
|
4707
|
+
resolved = resolveConfig(checkConfig(config));
|
|
4708
|
+
} else {
|
|
4709
|
+
const loaded = await loadConfig(root, configFile);
|
|
4710
|
+
const isSelf = loaded.config.isSelf;
|
|
4711
|
+
resolved = {
|
|
4712
|
+
...loaded.config,
|
|
4713
|
+
file: loaded.file,
|
|
4714
|
+
isSelf: (path) => path === loaded.file || isSelf(path)
|
|
4715
|
+
};
|
|
4716
|
+
}
|
|
4717
|
+
return {
|
|
4718
|
+
root,
|
|
4719
|
+
base: base ?? resolved.base ?? await defaultBase(root),
|
|
4720
|
+
config: resolved
|
|
4721
|
+
};
|
|
4722
|
+
}
|
|
4723
|
+
async function plan2(options = {}) {
|
|
4724
|
+
const { root, base, config } = await prepare(options);
|
|
4725
|
+
const computed = await computePlan(root, base, config, {
|
|
4726
|
+
...options.cache !== undefined && { cache: options.cache },
|
|
4727
|
+
mergeBase: true
|
|
4728
|
+
});
|
|
4729
|
+
return {
|
|
4730
|
+
suites: computed.plan.suites,
|
|
4731
|
+
reasons: computed.plan.reasons,
|
|
4732
|
+
causes: computed.plan.causes,
|
|
4733
|
+
totals: computed.plan.totals,
|
|
4734
|
+
packages: computed.plan.packages,
|
|
4735
|
+
base,
|
|
4736
|
+
mergeBase: await computed.mergeBase(),
|
|
4737
|
+
changed: computed.changed,
|
|
4738
|
+
narrowed: computed.narrowed.map(({ narrower, result }) => ({
|
|
4739
|
+
name: narrower.name,
|
|
4740
|
+
result
|
|
4741
|
+
}))
|
|
4742
|
+
};
|
|
4743
|
+
}
|
|
4744
|
+
|
|
4745
|
+
export { version, BiviumError, didYouMean, globToRegExp, defineConfig2, inRoot, sorted, QUERY, UNDECLARED_READS, RELATIVE, aliasPath, createResolver, SCANNED, formatOf, extract, stringsOf, finish, gotoPage, walksByTypes, importersOf, affectedBy, formatSelection, orphanOf, plural, computePlan, prepare, plan2 };
|