@stateward/core 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyses.d.ts +37 -0
- package/dist/analyses.js +57 -0
- package/dist/backend/program-record.d.ts +7 -0
- package/dist/backend/typescript6.d.ts +35 -32
- package/dist/backend/typescript6.js +3804 -163
- package/dist/baseline.d.ts +1 -1
- package/dist/baseline.js +14 -10
- package/dist/cache.d.ts +89 -5
- package/dist/cache.js +271 -40
- package/dist/checker.d.ts +11 -1
- package/dist/checker.js +32 -2
- 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 +541 -49
- package/dist/define.d.ts +8 -2
- package/dist/disk-cache.d.ts +2 -0
- package/dist/disk-cache.js +77 -11
- package/dist/engine.d.ts +51 -1
- package/dist/engine.js +517 -42
- 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 +9 -4
- package/dist/guarantees.d.ts +63 -11
- package/dist/guarantees.js +151 -21
- 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 +10 -9
- package/dist/index.js +14 -7
- package/dist/internal.d.ts +9 -5
- package/dist/internal.js +6 -3
- package/dist/lazy.d.ts +8 -1
- package/dist/lazy.js +27 -2
- package/dist/ledger.d.ts +77 -0
- package/dist/ledger.js +423 -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 +28 -38
- package/dist/renamed.d.ts +22 -3
- package/dist/renamed.js +50 -16
- package/dist/rollout.js +7 -4
- package/dist/safe-calls.d.ts +26 -0
- package/dist/safe-calls.js +345 -0
- package/dist/severity.d.ts +1 -1
- package/dist/severity.js +17 -3
- package/dist/stated.d.ts +37 -0
- package/dist/stated.js +123 -0
- package/dist/test-runner.d.ts +18 -0
- package/dist/test-runner.js +483 -0
- package/dist/testing-tsc.d.ts +16 -0
- package/dist/testing-tsc.js +86 -0
- package/dist/testing.d.ts +5 -4
- package/dist/testing.js +6 -6
- package/dist/timing.d.ts +15 -0
- package/dist/timing.js +41 -2
- package/dist/types.d.ts +1068 -16
- 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-tsc.d.ts
CHANGED
|
@@ -8,4 +8,20 @@ export declare function typeErrors(dir: string): string;
|
|
|
8
8
|
export declare function typeErrorsOfTexts(texts: readonly string[]): ReadonlyMap<string, string>;
|
|
9
9
|
/** The type errors of one text (see `typeErrorsOfTexts`), "" when it compiles. */
|
|
10
10
|
export declare function typeErrorsOfText(text: string): string;
|
|
11
|
+
/** A reference the language service finds: the file and the offset its name starts at. */
|
|
12
|
+
export type ServiceReference = {
|
|
13
|
+
readonly file: string;
|
|
14
|
+
readonly start: number;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* What the language service's find-references gives for the declaration that starts at `start`
|
|
18
|
+
* in `file` (its name: a function, a variable, a property, a parameter or a type alias; each name
|
|
19
|
+
* of an array destructuring), in the project of `tsconfig`: every reference that is not the declaration's own
|
|
20
|
+
* name. A test oracle for the
|
|
21
|
+
* reference ledger, which is built without a language service.
|
|
22
|
+
*/
|
|
23
|
+
export declare function serviceReferences(tsconfig: string, declarations: readonly {
|
|
24
|
+
readonly file: string;
|
|
25
|
+
readonly start: number;
|
|
26
|
+
}[]): ReadonlyMap<string, readonly ServiceReference[]>;
|
|
11
27
|
//# sourceMappingURL=testing-tsc.d.ts.map
|
package/dist/testing-tsc.js
CHANGED
|
@@ -122,4 +122,90 @@ function typeErrorsOfNamed(texts, nameOf) {
|
|
|
122
122
|
export function typeErrorsOfText(text) {
|
|
123
123
|
return typeErrorsOfNamed([text], () => "a.ts").get(text) ?? "";
|
|
124
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* What the language service's find-references gives for the declaration that starts at `start`
|
|
127
|
+
* in `file` (its name: a function, a variable, a property, a parameter or a type alias; each name
|
|
128
|
+
* of an array destructuring), in the project of `tsconfig`: every reference that is not the declaration's own
|
|
129
|
+
* name. A test oracle for the
|
|
130
|
+
* reference ledger, which is built without a language service.
|
|
131
|
+
*/
|
|
132
|
+
export function serviceReferences(tsconfig, declarations) {
|
|
133
|
+
const read = ts.readConfigFile(tsconfig, (file) => ts.sys.readFile(file));
|
|
134
|
+
const parsed = ts.parseJsonConfigFileContent(read.config, ts.sys, path.dirname(tsconfig));
|
|
135
|
+
const versions = new Map(parsed.fileNames.map((file) => [file, "0"]));
|
|
136
|
+
const host = {
|
|
137
|
+
getScriptFileNames: () => parsed.fileNames,
|
|
138
|
+
getScriptVersion: (file) => versions.get(file) ?? "0",
|
|
139
|
+
getScriptSnapshot: (file) => {
|
|
140
|
+
const text = ts.sys.readFile(file);
|
|
141
|
+
return text === undefined ? undefined : ts.ScriptSnapshot.fromString(text);
|
|
142
|
+
},
|
|
143
|
+
getCurrentDirectory: () => path.dirname(tsconfig),
|
|
144
|
+
getCompilationSettings: () => parsed.options,
|
|
145
|
+
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
|
|
146
|
+
fileExists: (file) => ts.sys.fileExists(file),
|
|
147
|
+
readFile: (file) => ts.sys.readFile(file),
|
|
148
|
+
readDirectory: (...args) => ts.sys.readDirectory(...args),
|
|
149
|
+
directoryExists: (dir) => ts.sys.directoryExists(dir),
|
|
150
|
+
getDirectories: (dir) => ts.sys.getDirectories(dir),
|
|
151
|
+
};
|
|
152
|
+
const service = ts.createLanguageService(host);
|
|
153
|
+
const program = service.getProgram();
|
|
154
|
+
const found = new Map();
|
|
155
|
+
for (const declaration of declarations) {
|
|
156
|
+
const sourceFile = program?.getSourceFile(declaration.file);
|
|
157
|
+
const name = sourceFile === undefined ? undefined : nameAt(sourceFile, declaration.start);
|
|
158
|
+
if (sourceFile === undefined || name === undefined) {
|
|
159
|
+
throw new Error(`no declaration at ${declaration.file}#${String(declaration.start)}`);
|
|
160
|
+
}
|
|
161
|
+
// `const [saving, setSaving] = …`: each name of the pattern is the declaration's.
|
|
162
|
+
const names = ts.isArrayBindingPattern(name)
|
|
163
|
+
? name.elements.flatMap((element) => ts.isBindingElement(element) && ts.isIdentifier(element.name) ? [element.name] : [])
|
|
164
|
+
: [name];
|
|
165
|
+
const own = new Set(names.map((one) => one.getStart(sourceFile)));
|
|
166
|
+
const references = [];
|
|
167
|
+
for (const one of names) {
|
|
168
|
+
for (const symbol of service.findReferences(declaration.file, one.getStart(sourceFile)) ??
|
|
169
|
+
[]) {
|
|
170
|
+
for (const reference of symbol.references) {
|
|
171
|
+
const start = reference.textSpan.start;
|
|
172
|
+
if (reference.fileName === declaration.file && own.has(start))
|
|
173
|
+
continue;
|
|
174
|
+
// A string key (`store["saving"]`) is found inside its quotes; the ledger has the literal.
|
|
175
|
+
const text = program?.getSourceFile(reference.fileName)?.text ?? "";
|
|
176
|
+
const quoted = start > 0 && (text[start - 1] === '"' || text[start - 1] === "'");
|
|
177
|
+
const at = quoted && text[start + reference.textSpan.length] === text[start - 1];
|
|
178
|
+
references.push({ file: reference.fileName, start: at ? start - 1 : start });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
found.set(`${declaration.file}#${String(declaration.start)}`, references);
|
|
183
|
+
}
|
|
184
|
+
service.dispose();
|
|
185
|
+
return found;
|
|
186
|
+
}
|
|
187
|
+
/** The name of the declaration that starts at `start`, or that is named there. */
|
|
188
|
+
function nameAt(sourceFile, start) {
|
|
189
|
+
let found;
|
|
190
|
+
const visit = (node) => {
|
|
191
|
+
if (found !== undefined)
|
|
192
|
+
return;
|
|
193
|
+
if ((ts.isFunctionDeclaration(node) ||
|
|
194
|
+
ts.isMethodDeclaration(node) ||
|
|
195
|
+
ts.isMethodSignature(node) ||
|
|
196
|
+
ts.isVariableDeclaration(node) ||
|
|
197
|
+
ts.isPropertyDeclaration(node) ||
|
|
198
|
+
ts.isPropertySignature(node) ||
|
|
199
|
+
ts.isParameter(node) ||
|
|
200
|
+
ts.isTypeAliasDeclaration(node)) &&
|
|
201
|
+
node.name !== undefined &&
|
|
202
|
+
(node.getStart(sourceFile) === start || node.name.getStart(sourceFile) === start)) {
|
|
203
|
+
found = node.name;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
ts.forEachChild(node, visit);
|
|
207
|
+
};
|
|
208
|
+
visit(sourceFile);
|
|
209
|
+
return found;
|
|
210
|
+
}
|
|
125
211
|
//# sourceMappingURL=testing-tsc.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
|
|
@@ -30,5 +30,6 @@ export declare class MessageStandardError extends Error {
|
|
|
30
30
|
}
|
|
31
31
|
/** Throws a `MessageStandardError` listing every violation in `findings`; use it in rule tests. */
|
|
32
32
|
export declare function expectMessageStandard(findings: readonly Finding[]): void;
|
|
33
|
-
export { typeErrors, typeErrorsOfText, typeErrorsOfTexts } from "./testing-tsc.js";
|
|
33
|
+
export { serviceReferences, typeErrors, typeErrorsOfText, typeErrorsOfTexts, } from "./testing-tsc.js";
|
|
34
|
+
export type { ServiceReference } from "./testing-tsc.js";
|
|
34
35
|
//# sourceMappingURL=testing.d.ts.map
|
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 });
|
|
@@ -214,5 +214,5 @@ export function expectMessageStandard(findings) {
|
|
|
214
214
|
throw new MessageStandardError(`${String(violations.length)} message standard violation(s):\n${violations.join("\n")}`);
|
|
215
215
|
}
|
|
216
216
|
}
|
|
217
|
-
export { typeErrors, typeErrorsOfText, typeErrorsOfTexts } from "./testing-tsc.js";
|
|
217
|
+
export { serviceReferences, typeErrors, typeErrorsOfText, typeErrorsOfTexts, } from "./testing-tsc.js";
|
|
218
218
|
//# sourceMappingURL=testing.js.map
|