@stateward/core 1.2.2 → 1.3.1
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/dist/analyses.d.ts +37 -0
- package/dist/analyses.js +57 -0
- package/dist/backend/fingerprints.d.ts +3 -1
- package/dist/backend/fingerprints.js +198 -9
- package/dist/backend/program-record.d.ts +7 -0
- package/dist/backend/program-size.d.ts +32 -0
- package/dist/backend/program-size.js +166 -0
- package/dist/backend/typescript6.d.ts +62 -32
- package/dist/backend/typescript6.js +1615 -164
- package/dist/baseline.d.ts +1 -1
- package/dist/baseline.js +14 -10
- package/dist/cache.d.ts +92 -4
- package/dist/cache.js +257 -68
- package/dist/checker.d.ts +28 -3
- package/dist/checker.js +42 -8
- package/dist/class-id.d.ts +34 -0
- package/dist/class-id.js +93 -0
- package/dist/code-text.d.ts +8 -0
- package/dist/code-text.js +66 -0
- package/dist/config-file.js +7 -5
- package/dist/config.d.ts +10 -2
- package/dist/config.js +490 -48
- package/dist/define.d.ts +8 -2
- package/dist/disk-cache.d.ts +34 -2
- package/dist/disk-cache.js +82 -15
- package/dist/engine.d.ts +64 -3
- package/dist/engine.js +474 -59
- package/dist/fact-lines.d.ts +31 -0
- package/dist/fact-lines.js +197 -0
- package/dist/facts-versions.d.ts +27 -0
- package/dist/facts-versions.js +99 -0
- package/dist/fixes.js +4 -1
- package/dist/ignores.d.ts +11 -0
- package/dist/ignores.js +74 -27
- package/dist/implementation.d.ts +0 -6
- package/dist/implementation.js +0 -17
- package/dist/index.d.ts +9 -9
- package/dist/index.js +12 -7
- package/dist/internal.d.ts +13 -6
- package/dist/internal.js +7 -3
- package/dist/journal.d.ts +40 -0
- package/dist/journal.js +197 -0
- package/dist/lazy.d.ts +8 -1
- package/dist/lazy.js +27 -2
- package/dist/ledger.d.ts +2 -2
- package/dist/ledger.js +2 -2
- package/dist/memory-plan.d.ts +121 -0
- package/dist/memory-plan.js +174 -0
- package/dist/off-by-default.d.ts +13 -0
- package/dist/off-by-default.js +15 -0
- package/dist/options.d.ts +21 -5
- package/dist/options.js +22 -7
- package/dist/package-facts.d.ts +211 -17
- package/dist/package-facts.js +520 -38
- package/dist/plan.d.ts +1 -1
- package/dist/plan.js +7 -2
- package/dist/release.d.ts +12 -21
- package/dist/release.js +26 -38
- package/dist/renamed.d.ts +22 -3
- package/dist/renamed.js +50 -16
- package/dist/rollout.js +7 -4
- package/dist/severity.d.ts +1 -1
- package/dist/severity.js +17 -3
- package/dist/test-runner.d.ts +18 -0
- package/dist/test-runner.js +483 -0
- package/dist/testing.d.ts +3 -3
- package/dist/testing.js +5 -5
- package/dist/timing.d.ts +17 -7
- package/dist/timing.js +60 -38
- package/dist/types.d.ts +704 -20
- package/dist/unread.d.ts +90 -0
- package/dist/unread.js +405 -0
- package/dist/vocabulary.d.ts +1 -1
- package/dist/vocabulary.js +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import picomatch from "picomatch";
|
|
4
|
+
import ts from "typescript";
|
|
5
|
+
const JEST_FILES = ["jest.config.ts", "jest.config.mts", "jest.config.cts", "jest.config.js"]
|
|
6
|
+
.concat(["jest.config.mjs", "jest.config.cjs", "jest.config.json"])
|
|
7
|
+
.map((name) => ({ name, runner: "jest" }));
|
|
8
|
+
const VITEST_FILES = ["vitest.config", "vite.config"]
|
|
9
|
+
.flatMap((base) => ["ts", "mts", "cts", "js", "mjs", "cjs"].map((ext) => `${base}.${ext}`))
|
|
10
|
+
.map((name) => ({ name, runner: "vitest" }));
|
|
11
|
+
/** How many `const` names a value is followed through before it counts as computed. */
|
|
12
|
+
const FOLLOW_LIMIT = 5;
|
|
13
|
+
/** The test files the test-runner configs in each of `dirs` name (see `TestRunnerFiles`). */
|
|
14
|
+
export function testRunnerFiles(dirs) {
|
|
15
|
+
const reads = [];
|
|
16
|
+
const unread = [];
|
|
17
|
+
const seen = new Set();
|
|
18
|
+
const readFile = (file, runner) => {
|
|
19
|
+
if (seen.has(file))
|
|
20
|
+
return;
|
|
21
|
+
seen.add(file);
|
|
22
|
+
const read = readConfigFile(file, runner);
|
|
23
|
+
if (read === undefined)
|
|
24
|
+
return;
|
|
25
|
+
reads.push(read);
|
|
26
|
+
if (!read.complete)
|
|
27
|
+
unread.push(file);
|
|
28
|
+
};
|
|
29
|
+
for (const dir of [...new Set(dirs.map((entry) => path.resolve(entry)))]) {
|
|
30
|
+
for (const { name, runner } of [...JEST_FILES, ...VITEST_FILES]) {
|
|
31
|
+
const file = path.join(dir, name);
|
|
32
|
+
if (existsSync(file))
|
|
33
|
+
readFile(file, runner);
|
|
34
|
+
}
|
|
35
|
+
const manifestFile = path.join(dir, "package.json");
|
|
36
|
+
const manifest = readManifest(manifestFile);
|
|
37
|
+
if (manifest === undefined)
|
|
38
|
+
continue;
|
|
39
|
+
const jest = manifestJest(manifest, dir);
|
|
40
|
+
if (jest !== undefined)
|
|
41
|
+
reads.push(jest);
|
|
42
|
+
for (const named of scriptConfigs(manifest)) {
|
|
43
|
+
if (named.kind === "unread") {
|
|
44
|
+
unread.push(manifestFile);
|
|
45
|
+
}
|
|
46
|
+
else if (named.kind === "inline") {
|
|
47
|
+
reads.push(jestFromJson(named.config, dir));
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const file = path.resolve(dir, named.file);
|
|
51
|
+
if (CONFIG_EXTENSION.test(file) && existsSync(file))
|
|
52
|
+
readFile(file, named.runner);
|
|
53
|
+
else
|
|
54
|
+
unread.push(file);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const matchers = reads.map((read) => {
|
|
59
|
+
const globs = read.globs.length === 0 ? () => false : picomatch([...read.globs], { dot: true });
|
|
60
|
+
return (file) => {
|
|
61
|
+
const relative = path.relative(read.root, file);
|
|
62
|
+
if (relative.startsWith("..") || path.isAbsolute(relative))
|
|
63
|
+
return false;
|
|
64
|
+
const posix = relative.split(path.sep).join("/");
|
|
65
|
+
const absolute = file.split(path.sep).join("/");
|
|
66
|
+
return globs(posix) || read.regexes.some((regex) => regex.test(absolute));
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
matches: (file) => matchers.some((matches) => matches(file)),
|
|
71
|
+
unread: [...new Set(unread)].sort(),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function readManifest(file) {
|
|
75
|
+
if (!existsSync(file))
|
|
76
|
+
return undefined;
|
|
77
|
+
let manifest;
|
|
78
|
+
try {
|
|
79
|
+
manifest = JSON.parse(readFileSync(file, "utf8"));
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// A package.json that is not JSON names no tests; TypeScript and npm report it.
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
return isRecord(manifest) ? manifest : undefined;
|
|
86
|
+
}
|
|
87
|
+
function manifestJest(manifest, dir) {
|
|
88
|
+
return isRecord(manifest.jest) ? jestFromJson(manifest.jest, dir) : undefined;
|
|
89
|
+
}
|
|
90
|
+
/** The config files stateward reads by their extension: JSON, and JavaScript or TypeScript. */
|
|
91
|
+
const CONFIG_EXTENSION = /\.(?:json|[cm]?[jt]s)$/;
|
|
92
|
+
/**
|
|
93
|
+
* The configs the `package.json` scripts pass to `jest` or `vitest` with `--config` or `-c`, run
|
|
94
|
+
* directly or through `yarn`, `npx`, `pnpm` (`exec`) or `npm exec`. The scripts are read as text,
|
|
95
|
+
* never run; a config written with a shell variable or a substitution is `unread`.
|
|
96
|
+
*/
|
|
97
|
+
function scriptConfigs(manifest) {
|
|
98
|
+
if (!isRecord(manifest.scripts))
|
|
99
|
+
return [];
|
|
100
|
+
const found = [];
|
|
101
|
+
for (const script of Object.values(manifest.scripts)) {
|
|
102
|
+
if (typeof script !== "string")
|
|
103
|
+
continue;
|
|
104
|
+
for (const command of script.split(/&&|\|\||[;|&\n]/)) {
|
|
105
|
+
const words = shellWords(command);
|
|
106
|
+
if (words === undefined) {
|
|
107
|
+
if (/\b(?:jest|vitest)\b/.test(command))
|
|
108
|
+
found.push({ kind: "unread" });
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const runner = runnerOf(words);
|
|
112
|
+
if (runner === undefined)
|
|
113
|
+
continue;
|
|
114
|
+
const config = configArgument(runner.args);
|
|
115
|
+
if (config === undefined)
|
|
116
|
+
continue;
|
|
117
|
+
found.push(scriptConfig(config, runner.runner));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return found;
|
|
121
|
+
}
|
|
122
|
+
function scriptConfig(value, runner) {
|
|
123
|
+
if (/[$`*?]/.test(value) || value === "")
|
|
124
|
+
return { kind: "unread" };
|
|
125
|
+
if (value.startsWith("{")) {
|
|
126
|
+
// Jest takes the config itself as JSON in place of a path.
|
|
127
|
+
let parsed;
|
|
128
|
+
try {
|
|
129
|
+
parsed = JSON.parse(value);
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
return { kind: "unread" };
|
|
133
|
+
}
|
|
134
|
+
return runner === "jest" && isRecord(parsed)
|
|
135
|
+
? { kind: "inline", config: parsed }
|
|
136
|
+
: { kind: "unread" };
|
|
137
|
+
}
|
|
138
|
+
return { kind: "file", file: value, runner };
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The words of one shell command, quotes taken out; undefined when a quote is left open or the
|
|
142
|
+
* command holds a substitution (`$(…)`), which only running it would tell.
|
|
143
|
+
*/
|
|
144
|
+
function shellWords(command) {
|
|
145
|
+
if (command.includes("$("))
|
|
146
|
+
return undefined;
|
|
147
|
+
const words = [];
|
|
148
|
+
let current;
|
|
149
|
+
let quote;
|
|
150
|
+
for (const char of command) {
|
|
151
|
+
if (quote !== undefined) {
|
|
152
|
+
if (char === quote)
|
|
153
|
+
quote = undefined;
|
|
154
|
+
else
|
|
155
|
+
current = (current ?? "") + char;
|
|
156
|
+
}
|
|
157
|
+
else if (char === '"' || char === "'") {
|
|
158
|
+
quote = char;
|
|
159
|
+
current ??= "";
|
|
160
|
+
}
|
|
161
|
+
else if (/\s/.test(char)) {
|
|
162
|
+
if (current !== undefined)
|
|
163
|
+
words.push(current);
|
|
164
|
+
current = undefined;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
current = (current ?? "") + char;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (quote !== undefined)
|
|
171
|
+
return undefined;
|
|
172
|
+
if (current !== undefined)
|
|
173
|
+
words.push(current);
|
|
174
|
+
return words;
|
|
175
|
+
}
|
|
176
|
+
/** The runner a command starts and its arguments, past environment settings and package runners. */
|
|
177
|
+
function runnerOf(words) {
|
|
178
|
+
let rest = [...words];
|
|
179
|
+
while (rest[0] !== undefined && /^[A-Za-z_]\w*=/.test(rest[0]))
|
|
180
|
+
rest = rest.slice(1);
|
|
181
|
+
const [first, second] = rest;
|
|
182
|
+
if (first === "yarn")
|
|
183
|
+
rest = rest.slice(second === "run" || second === "exec" ? 2 : 1);
|
|
184
|
+
else if (first === "pnpm")
|
|
185
|
+
rest = rest.slice(second === "exec" || second === "run" ? 2 : 1);
|
|
186
|
+
else if (first === "npm" && second === "exec")
|
|
187
|
+
rest = rest.slice(2);
|
|
188
|
+
else if (first === "npx")
|
|
189
|
+
rest = rest.slice(1);
|
|
190
|
+
if (first === "npx" || first === "npm") {
|
|
191
|
+
while (rest[0]?.startsWith("-") === true) {
|
|
192
|
+
rest = rest.slice(rest[0] === "-p" || rest[0] === "--package" ? 2 : 1);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const [program, ...args] = rest;
|
|
196
|
+
if (program === undefined)
|
|
197
|
+
return undefined;
|
|
198
|
+
const name = program.split("/").pop();
|
|
199
|
+
if (name === "jest")
|
|
200
|
+
return { runner: "jest", args };
|
|
201
|
+
if (name === "vitest")
|
|
202
|
+
return { runner: "vitest", args };
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
/** The value of `--config <value>`, `--config=<value>` or `-c <value>`. */
|
|
206
|
+
function configArgument(args) {
|
|
207
|
+
for (const [index, arg] of args.entries()) {
|
|
208
|
+
if (arg === "--config" || arg === "-c")
|
|
209
|
+
return args[index + 1] ?? "";
|
|
210
|
+
if (arg.startsWith("--config="))
|
|
211
|
+
return arg.slice("--config=".length);
|
|
212
|
+
}
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
function readConfigFile(file, runner) {
|
|
216
|
+
const dir = path.dirname(file);
|
|
217
|
+
const text = readFileSync(file, "utf8");
|
|
218
|
+
if (file.endsWith(".json")) {
|
|
219
|
+
let value;
|
|
220
|
+
try {
|
|
221
|
+
value = JSON.parse(text);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
return { root: dir, globs: [], regexes: [], complete: false };
|
|
225
|
+
}
|
|
226
|
+
return isRecord(value) ? jestFromJson(value, dir) : undefined;
|
|
227
|
+
}
|
|
228
|
+
const sourceFile = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, false);
|
|
229
|
+
const exported = exportedValue(sourceFile);
|
|
230
|
+
const config = exported === undefined ? { kind: "unread" } : literalOf(exported, sourceFile);
|
|
231
|
+
if (config.kind !== "object") {
|
|
232
|
+
// A config that is computed is not read; Vite's names tests only under a `test` key.
|
|
233
|
+
return runner === "vitest" && !namesProperty(sourceFile, "test")
|
|
234
|
+
? undefined
|
|
235
|
+
: { root: dir, globs: [], regexes: [], complete: false };
|
|
236
|
+
}
|
|
237
|
+
return runner === "jest"
|
|
238
|
+
? jestFromObject(config.node, sourceFile, dir)
|
|
239
|
+
: vitestFromObject(config.node, sourceFile, dir);
|
|
240
|
+
}
|
|
241
|
+
function jestFromJson(config, dir) {
|
|
242
|
+
const rootDir = typeof config.rootDir === "string" ? path.resolve(dir, config.rootDir) : dir;
|
|
243
|
+
const globs = stringsOf(config.testMatch);
|
|
244
|
+
const patterns = stringsOf(config.testRegex);
|
|
245
|
+
const regexes = (patterns ?? []).flatMap((pattern) => regexOf(pattern) ?? []);
|
|
246
|
+
const complete = globs !== undefined && regexes.length === patterns?.length && config.projects === undefined;
|
|
247
|
+
return {
|
|
248
|
+
root: rootDir,
|
|
249
|
+
globs: (globs ?? []).map((glob) => rootRelative(glob, rootDir)),
|
|
250
|
+
regexes,
|
|
251
|
+
complete,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function jestFromObject(node, sourceFile, dir) {
|
|
255
|
+
const props = propertiesOf(node, sourceFile);
|
|
256
|
+
const root = props.get("rootDir");
|
|
257
|
+
const rootDir = root?.kind === "string" ? path.resolve(dir, root.value) : dir;
|
|
258
|
+
const testMatch = listOf(props.get("testMatch"));
|
|
259
|
+
const testRegex = listOf(props.get("testRegex"));
|
|
260
|
+
const regexes = testRegex.values.flatMap((pattern) => regexOf(pattern) ?? []);
|
|
261
|
+
return {
|
|
262
|
+
root: rootDir,
|
|
263
|
+
globs: testMatch.values.map((glob) => rootRelative(glob, rootDir)),
|
|
264
|
+
regexes,
|
|
265
|
+
complete: props.complete &&
|
|
266
|
+
(root === undefined || root.kind === "string") &&
|
|
267
|
+
testMatch.complete &&
|
|
268
|
+
testRegex.complete &&
|
|
269
|
+
regexes.length === testRegex.values.length &&
|
|
270
|
+
!props.has("projects"),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function vitestFromObject(node, sourceFile, dir) {
|
|
274
|
+
const props = propertiesOf(node, sourceFile);
|
|
275
|
+
const test = props.get("test");
|
|
276
|
+
if (test === undefined) {
|
|
277
|
+
return props.complete ? undefined : { root: dir, globs: [], regexes: [], complete: false };
|
|
278
|
+
}
|
|
279
|
+
if (test.kind !== "object")
|
|
280
|
+
return { root: dir, globs: [], regexes: [], complete: false };
|
|
281
|
+
const testProps = propertiesOf(test.node, sourceFile);
|
|
282
|
+
const include = listOf(testProps.get("include"));
|
|
283
|
+
const root = testProps.get("root") ?? props.get("root");
|
|
284
|
+
const rootDir = root?.kind === "string" ? path.resolve(dir, root.value) : dir;
|
|
285
|
+
return {
|
|
286
|
+
root: rootDir,
|
|
287
|
+
globs: include.values.map((glob) => glob.replace(/^\.\//, "")),
|
|
288
|
+
regexes: [],
|
|
289
|
+
complete: props.complete &&
|
|
290
|
+
testProps.complete &&
|
|
291
|
+
include.complete &&
|
|
292
|
+
(root === undefined || root.kind === "string") &&
|
|
293
|
+
!testProps.has("projects") &&
|
|
294
|
+
!testProps.has("workspace"),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
/** A Jest glob with `<rootDir>` taken out, relative to the root it names. */
|
|
298
|
+
function rootRelative(glob, rootDir) {
|
|
299
|
+
const replaced = glob.replace(/^<rootDir>\/?/, "");
|
|
300
|
+
if (!path.isAbsolute(replaced))
|
|
301
|
+
return replaced.replace(/^\.\//, "");
|
|
302
|
+
return path.relative(rootDir, replaced).split(path.sep).join("/");
|
|
303
|
+
}
|
|
304
|
+
function regexOf(pattern) {
|
|
305
|
+
try {
|
|
306
|
+
return new RegExp(pattern);
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
// Jest rejects it too; it names no file here.
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
/** A string or a list of strings; undefined when it is neither, an empty list when it is absent. */
|
|
314
|
+
function stringsOf(value) {
|
|
315
|
+
if (value === undefined)
|
|
316
|
+
return [];
|
|
317
|
+
if (typeof value === "string")
|
|
318
|
+
return [value];
|
|
319
|
+
if (Array.isArray(value)) {
|
|
320
|
+
const list = value;
|
|
321
|
+
const strings = list.filter((entry) => typeof entry === "string");
|
|
322
|
+
if (strings.length === list.length)
|
|
323
|
+
return strings;
|
|
324
|
+
}
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
function listOf(value) {
|
|
328
|
+
if (value === undefined)
|
|
329
|
+
return { values: [], complete: true };
|
|
330
|
+
switch (value.kind) {
|
|
331
|
+
case "string":
|
|
332
|
+
return { values: [value.value], complete: true };
|
|
333
|
+
case "strings":
|
|
334
|
+
return { values: value.values, complete: value.complete };
|
|
335
|
+
case "object":
|
|
336
|
+
case "unread":
|
|
337
|
+
return { values: [], complete: false };
|
|
338
|
+
default: {
|
|
339
|
+
const unreachable = value;
|
|
340
|
+
return unreachable;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/** The properties of an object literal written with plain names, and whether every one was read. */
|
|
345
|
+
function propertiesOf(node, sourceFile) {
|
|
346
|
+
const values = new Map();
|
|
347
|
+
let complete = true;
|
|
348
|
+
for (const property of node.properties) {
|
|
349
|
+
if (ts.isPropertyAssignment(property)) {
|
|
350
|
+
const name = propertyName(property.name);
|
|
351
|
+
if (name === undefined)
|
|
352
|
+
complete = false;
|
|
353
|
+
else
|
|
354
|
+
values.set(name, literalOf(property.initializer, sourceFile));
|
|
355
|
+
}
|
|
356
|
+
else if (ts.isShorthandPropertyAssignment(property)) {
|
|
357
|
+
values.set(property.name.text, literalOf(property.name, sourceFile));
|
|
358
|
+
}
|
|
359
|
+
else if (ts.isSpreadAssignment(property)) {
|
|
360
|
+
// A spread may hold any setting.
|
|
361
|
+
complete = false;
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
// A method or accessor is not a setting the runner reads as a list.
|
|
365
|
+
complete = false;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return { get: (name) => values.get(name), has: (name) => values.has(name), complete };
|
|
369
|
+
}
|
|
370
|
+
function propertyName(name) {
|
|
371
|
+
if (ts.isIdentifier(name) ||
|
|
372
|
+
ts.isStringLiteral(name) ||
|
|
373
|
+
ts.isNoSubstitutionTemplateLiteral(name)) {
|
|
374
|
+
return name.text;
|
|
375
|
+
}
|
|
376
|
+
return undefined;
|
|
377
|
+
}
|
|
378
|
+
/** The value an expression writes as a literal, following `const` names of the same file. */
|
|
379
|
+
function literalOf(start, sourceFile, follows = 0) {
|
|
380
|
+
const node = unwrap(start);
|
|
381
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
382
|
+
return { kind: "string", value: node.text };
|
|
383
|
+
}
|
|
384
|
+
if (ts.isObjectLiteralExpression(node))
|
|
385
|
+
return { kind: "object", node };
|
|
386
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
387
|
+
const values = [];
|
|
388
|
+
let complete = true;
|
|
389
|
+
for (const element of node.elements) {
|
|
390
|
+
const value = literalOf(element, sourceFile, follows);
|
|
391
|
+
if (value.kind === "string")
|
|
392
|
+
values.push(value.value);
|
|
393
|
+
else
|
|
394
|
+
complete = false;
|
|
395
|
+
}
|
|
396
|
+
return { kind: "strings", values, complete };
|
|
397
|
+
}
|
|
398
|
+
// `defineConfig({ … })`: a helper that takes the config as its one argument and returns it.
|
|
399
|
+
if (ts.isCallExpression(node) && node.arguments.length === 1) {
|
|
400
|
+
const [argument] = node.arguments;
|
|
401
|
+
if (argument !== undefined) {
|
|
402
|
+
const inner = literalOf(argument, sourceFile, follows);
|
|
403
|
+
if (inner.kind === "object")
|
|
404
|
+
return inner;
|
|
405
|
+
}
|
|
406
|
+
return { kind: "unread" };
|
|
407
|
+
}
|
|
408
|
+
if (ts.isIdentifier(node) && follows < FOLLOW_LIMIT) {
|
|
409
|
+
const initializer = constInitializer(sourceFile, node.text);
|
|
410
|
+
if (initializer !== undefined)
|
|
411
|
+
return literalOf(initializer, sourceFile, follows + 1);
|
|
412
|
+
}
|
|
413
|
+
return { kind: "unread" };
|
|
414
|
+
}
|
|
415
|
+
function unwrap(node) {
|
|
416
|
+
let current = node;
|
|
417
|
+
for (;;) {
|
|
418
|
+
if (ts.isParenthesizedExpression(current) ||
|
|
419
|
+
ts.isAsExpression(current) ||
|
|
420
|
+
ts.isSatisfiesExpression(current) ||
|
|
421
|
+
ts.isNonNullExpression(current) ||
|
|
422
|
+
ts.isTypeAssertionExpression(current)) {
|
|
423
|
+
current = current.expression;
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
return current;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/** The initializer of a top-level `const name = …` of the file. */
|
|
431
|
+
function constInitializer(sourceFile, name) {
|
|
432
|
+
for (const statement of sourceFile.statements) {
|
|
433
|
+
if (!ts.isVariableStatement(statement))
|
|
434
|
+
continue;
|
|
435
|
+
if ((statement.declarationList.flags & ts.NodeFlags.Const) === 0)
|
|
436
|
+
continue;
|
|
437
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
438
|
+
if (ts.isIdentifier(declaration.name) && declaration.name.text === name) {
|
|
439
|
+
return declaration.initializer;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
return undefined;
|
|
444
|
+
}
|
|
445
|
+
/** What the config file exports: `export default …`, `export = …` or `module.exports = …`. */
|
|
446
|
+
function exportedValue(sourceFile) {
|
|
447
|
+
for (const statement of sourceFile.statements) {
|
|
448
|
+
if (ts.isExportAssignment(statement))
|
|
449
|
+
return statement.expression;
|
|
450
|
+
if (ts.isExpressionStatement(statement) &&
|
|
451
|
+
ts.isBinaryExpression(statement.expression) &&
|
|
452
|
+
statement.expression.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
453
|
+
const target = statement.expression.left;
|
|
454
|
+
if (ts.isPropertyAccessExpression(target) &&
|
|
455
|
+
ts.isIdentifier(target.expression) &&
|
|
456
|
+
target.expression.text === "module" &&
|
|
457
|
+
target.name.text === "exports") {
|
|
458
|
+
return statement.expression.right;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return undefined;
|
|
463
|
+
}
|
|
464
|
+
/** Whether any object literal of the file has a property named `name`. */
|
|
465
|
+
function namesProperty(sourceFile, name) {
|
|
466
|
+
let found = false;
|
|
467
|
+
const visit = (node) => {
|
|
468
|
+
if (found)
|
|
469
|
+
return;
|
|
470
|
+
if ((ts.isPropertyAssignment(node) || ts.isShorthandPropertyAssignment(node)) &&
|
|
471
|
+
propertyName(node.name) === name) {
|
|
472
|
+
found = true;
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
ts.forEachChild(node, visit);
|
|
476
|
+
};
|
|
477
|
+
visit(sourceFile);
|
|
478
|
+
return found;
|
|
479
|
+
}
|
|
480
|
+
function isRecord(value) {
|
|
481
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
482
|
+
}
|
|
483
|
+
//# sourceMappingURL=test-runner.js.map
|
package/dist/testing.d.ts
CHANGED
|
@@ -5,14 +5,14 @@ export declare class FixtureConfigError extends Error {
|
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
7
|
* Checks the project in `dir` (it needs a tsconfig.json) with just `plugins`, the `recommended`
|
|
8
|
-
* preset (a rule's tests judge it as most teams turn it on)
|
|
9
|
-
* "<plugin>/<addon>". Checking the same
|
|
8
|
+
* preset (a rule's tests judge it as most teams turn it on), the `addons` named
|
|
9
|
+
* "<plugin>/<addon>", and `conventions` (the config's `conventions: true`). Checking the same
|
|
10
10
|
* folder again with the same plugin objects and rule settings, while no file in it changed,
|
|
11
11
|
* returns the earlier result, so a test file asking many questions of one fixture checks it once.
|
|
12
12
|
* Plugins built anew for each call (`guards()`) are never matched: a test of determinism that
|
|
13
13
|
* does that always checks again.
|
|
14
14
|
*/
|
|
15
|
-
export declare function checkFixture(dir: string, plugins: readonly Plugin[], rules?: Record<string, RuleConfig>, addons?: readonly string[]): CheckResult;
|
|
15
|
+
export declare function checkFixture(dir: string, plugins: readonly Plugin[], rules?: Record<string, RuleConfig>, addons?: readonly string[], conventions?: boolean): CheckResult;
|
|
16
16
|
/**
|
|
17
17
|
* The code of one rule of a plugin, with its `explain` text and its kind. A lazy rule must be
|
|
18
18
|
* loaded already: the test setup (vitest.setup.ts) loads every pack's rules before a test file
|
package/dist/testing.js
CHANGED
|
@@ -35,16 +35,16 @@ function samePlugins(a, b) {
|
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
37
|
* Checks the project in `dir` (it needs a tsconfig.json) with just `plugins`, the `recommended`
|
|
38
|
-
* preset (a rule's tests judge it as most teams turn it on)
|
|
39
|
-
* "<plugin>/<addon>". Checking the same
|
|
38
|
+
* preset (a rule's tests judge it as most teams turn it on), the `addons` named
|
|
39
|
+
* "<plugin>/<addon>", and `conventions` (the config's `conventions: true`). Checking the same
|
|
40
40
|
* folder again with the same plugin objects and rule settings, while no file in it changed,
|
|
41
41
|
* returns the earlier result, so a test file asking many questions of one fixture checks it once.
|
|
42
42
|
* Plugins built anew for each call (`guards()`) are never matched: a test of determinism that
|
|
43
43
|
* does that always checks again.
|
|
44
44
|
*/
|
|
45
|
-
export function checkFixture(dir, plugins, rules = {}, addons = []) {
|
|
45
|
+
export function checkFixture(dir, plugins, rules = {}, addons = [], conventions = false) {
|
|
46
46
|
const key = path.resolve(dir);
|
|
47
|
-
const rulesKey = JSON.stringify({ rules, addons });
|
|
47
|
+
const rulesKey = JSON.stringify({ rules, addons, conventions });
|
|
48
48
|
const signature = signatureOf(key);
|
|
49
49
|
const known = checkCache.get(key) ?? [];
|
|
50
50
|
const hit = known.find((entry) => entry.rules === rulesKey &&
|
|
@@ -52,7 +52,7 @@ export function checkFixture(dir, plugins, rules = {}, addons = []) {
|
|
|
52
52
|
samePlugins(entry.plugins, plugins));
|
|
53
53
|
if (hit !== undefined)
|
|
54
54
|
return hit.result;
|
|
55
|
-
const parsed = parseConfig({ plugins, preset: "recommended", rules, addons }, dir);
|
|
55
|
+
const parsed = parseConfig({ plugins, preset: "recommended", rules, addons, conventions }, dir);
|
|
56
56
|
if (!parsed.ok)
|
|
57
57
|
throw new FixtureConfigError(parsed.errors.join("\n"));
|
|
58
58
|
const checked = check({ config: parsed.value, cwd: dir });
|
package/dist/timing.d.ts
CHANGED
|
@@ -17,7 +17,8 @@ export type RuleCost = {
|
|
|
17
17
|
readonly projectMs: number;
|
|
18
18
|
/**
|
|
19
19
|
* Of `ms`, the time inside type questions (`TypeQueries`): mostly the type checking a question
|
|
20
|
-
* triggers first, which later questions of any rule then reuse.
|
|
20
|
+
* triggers first, which later questions of any rule then reuse. The rest of `ms` is the rule's
|
|
21
|
+
* own work.
|
|
21
22
|
*/
|
|
22
23
|
readonly typeMs: number;
|
|
23
24
|
/** Files the rule ran on (not counting results reused from the cache). */
|
|
@@ -33,12 +34,6 @@ export type RuleCost = {
|
|
|
33
34
|
readonly warmTypeMs: number | undefined;
|
|
34
35
|
/** Files whose result was taken from the cache instead. */
|
|
35
36
|
readonly reused: number;
|
|
36
|
-
/**
|
|
37
|
-
* Bytes the heap grew by while the rule ran, summed over its runs: an estimate of what it
|
|
38
|
-
* allocated (most of it freed again), not memory it holds. It undercounts when a garbage
|
|
39
|
-
* collection lands inside a run.
|
|
40
|
-
*/
|
|
41
|
-
readonly allocatedBytes: number;
|
|
42
37
|
};
|
|
43
38
|
/** One run's costs: the phases around the rules, and each rule (by time, largest first). */
|
|
44
39
|
export type RunTiming = {
|
|
@@ -48,6 +43,12 @@ export type RunTiming = {
|
|
|
48
43
|
readonly programMs: number;
|
|
49
44
|
/** Of the rules' time, inside type questions: the type checking the rules ask for. */
|
|
50
45
|
readonly typeMs: number;
|
|
46
|
+
/**
|
|
47
|
+
* The heap still in use at the end of the file phase, after a full garbage collection: about
|
|
48
|
+
* what the check needs at its largest (the programs, their type checkers and the results), as
|
|
49
|
+
* opposed to the peak, which follows the heap limit. Undefined when no collection could be forced.
|
|
50
|
+
*/
|
|
51
|
+
readonly liveHeapBytes: number | undefined;
|
|
51
52
|
/** Dependency fingerprints for the results cache. */
|
|
52
53
|
readonly fingerprintMs: number;
|
|
53
54
|
/**
|
|
@@ -76,6 +77,11 @@ export type TimingRecorder = {
|
|
|
76
77
|
readonly secondPass: boolean;
|
|
77
78
|
/** Runs `run` as `ruleId`'s file side on one file in the second pass and records its cost. */
|
|
78
79
|
warmSide(ruleId: string, kind: "file" | "project", run: () => void): void;
|
|
80
|
+
/**
|
|
81
|
+
* A project's file phase has ended, its program not yet let go: the live heap is measured, and
|
|
82
|
+
* the largest over the projects kept (`RunTiming.liveHeapBytes`).
|
|
83
|
+
*/
|
|
84
|
+
filesDone(): void;
|
|
79
85
|
/** Runs `run` as a project rule's check and records its cost. */
|
|
80
86
|
projectSide<T>(ruleId: string, run: () => T): T;
|
|
81
87
|
/** `types` with the time of every question added to the rule that is running. */
|
|
@@ -85,6 +91,10 @@ export type TimingRecorder = {
|
|
|
85
91
|
/** The costs so far, with the run's total measured up to now. */
|
|
86
92
|
result(): RunTiming;
|
|
87
93
|
};
|
|
94
|
+
/**
|
|
95
|
+
* The recorder holds one record per rule and a few totals, whatever the size of the project: a
|
|
96
|
+
* type question adds its time to the rule that asked it and keeps nothing.
|
|
97
|
+
*/
|
|
88
98
|
export declare function createTimingRecorder({ secondPass, }?: {
|
|
89
99
|
/** Run every rule a second time after the check (`RuleCost.warmMs`): for benchmarks. */
|
|
90
100
|
secondPass?: boolean;
|