@cplieger/deadset-ts 2.2.0 → 2.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.
@@ -0,0 +1,385 @@
1
+ /**
2
+ * The files a tool or the platform enters without any import naming them: the
3
+ * configuration files the test runner, the linter, the mutation tester and the
4
+ * browser-test runner load by their file name, the test files the runner executes,
5
+ * and a worker a call addresses by a string literal.
6
+ *
7
+ * Each rule is read from the program and nothing else: a file name the tool
8
+ * itself looks for, a literal written in a configuration file, or a literal
9
+ * argument of a call the checker resolves to the platform's own declaration. A
10
+ * value that is not a literal is not evaluated and not guessed, so a file it would
11
+ * have named is entered by nothing here.
12
+ */
13
+ import { isArrayLiteralExpression, isAsExpression, isCallExpression, isExportAssignment, isIdentifier, isMetaProperty, isNewExpression, isNoSubstitutionTemplateLiteral, isObjectLiteralExpression, isParenthesizedExpression, isPropertyAccessExpression, isPropertyAssignment, isSatisfiesExpression, isStringLiteral, } from "@typescript/native/unstable/ast";
14
+ import { SymbolFlags } from "@typescript/native/unstable/sync";
15
+ import { globExpression } from "./glob.js";
16
+ import { dirnamePath, relativePath, resolvePath } from "./paths.js";
17
+ /** The extensions the test runner and the browser-test runner load a configuration from. */
18
+ const MODULE_EXTENSIONS = ["js", "mjs", "cjs", "ts", "mts", "cts"];
19
+ /** The extensions the linter's flat configuration and the mutation tester's are loaded from. */
20
+ const SCRIPT_EXTENSIONS = ["js", "mjs", "cjs"];
21
+ const CONVENTIONS = [
22
+ {
23
+ rule: "test-runner",
24
+ stems: ["vitest.config", "vitest.workspace", "vitest.projects"],
25
+ extensions: MODULE_EXTENSIONS,
26
+ testRunner: true,
27
+ },
28
+ {
29
+ rule: "lint-configuration",
30
+ stems: ["eslint.config"],
31
+ extensions: MODULE_EXTENSIONS,
32
+ testRunner: false,
33
+ },
34
+ {
35
+ rule: "mutation-testing",
36
+ stems: ["stryker.config", "stryker.conf", ".stryker.config", ".stryker.conf"],
37
+ extensions: SCRIPT_EXTENSIONS,
38
+ testRunner: false,
39
+ },
40
+ {
41
+ rule: "mutation-testing",
42
+ stems: ["vitest.stryker.config"],
43
+ extensions: MODULE_EXTENSIONS,
44
+ testRunner: true,
45
+ },
46
+ {
47
+ rule: "browser-tests",
48
+ stems: ["playwright.config"],
49
+ extensions: MODULE_EXTENSIONS,
50
+ testRunner: false,
51
+ },
52
+ ];
53
+ /** The browser-test runner's own test-file pattern, which holds where a configuration names none. */
54
+ const BROWSER_TEST_MATCH = "**/*.{spec,test}.{js,ts,jsx,tsx,cjs,cts,cjsx,ctsx,mjs,mts,mjsx,mtsx}";
55
+ /** The members of a test-runner configuration's `test` object that name setup files. */
56
+ const SETUP_MEMBERS = ["setupFiles", "globalSetup"];
57
+ /** A member that moves the directory every path of a test-runner configuration is read from. */
58
+ const ROOT_MEMBER = "root";
59
+ /** The prefixes a literal is relative with, in a module specifier and a URL reference alike. */
60
+ const RELATIVE_PREFIXES = ["./", "../"];
61
+ /** The constructors whose first argument addresses a worker script. */
62
+ const WORKER_CONSTRUCTORS = new Set(["Worker", "SharedWorker"]);
63
+ /** The file's name, the last segment of its path. */
64
+ function basename(path) {
65
+ return path.slice(path.lastIndexOf("/") + 1);
66
+ }
67
+ /** The convention one file name is, with the spelling a root reports it by. */
68
+ function conventionOf(name) {
69
+ for (const convention of CONVENTIONS) {
70
+ for (const stem of convention.stems) {
71
+ if (convention.extensions.some((ext) => name === `${stem}.${ext}`)) {
72
+ return { convention, spelled: `${stem}.*` };
73
+ }
74
+ }
75
+ }
76
+ return undefined;
77
+ }
78
+ /** The text a string literal or a template with no substitution denotes. */
79
+ function literalOf(node) {
80
+ if (node !== undefined && (isStringLiteral(node) || isNoSubstitutionTemplateLiteral(node))) {
81
+ return node.text;
82
+ }
83
+ return undefined;
84
+ }
85
+ /** The expression a wrapper that changes no value holds: parentheses and type assertions. */
86
+ function unwrapped(node) {
87
+ let at = node;
88
+ while (isParenthesizedExpression(at) || isAsExpression(at) || isSatisfiesExpression(at)) {
89
+ at = at.expression;
90
+ }
91
+ return at;
92
+ }
93
+ /**
94
+ * The object literals one configuration file's default export is written as: the
95
+ * literal itself, or each literal argument of the call it is, at any depth of calls.
96
+ * A default export written as anything else, a name or a computation, yields none,
97
+ * because its value is not in the file's text.
98
+ */
99
+ function configObjects(file) {
100
+ const found = [];
101
+ const collect = (node) => {
102
+ const at = unwrapped(node);
103
+ if (isObjectLiteralExpression(at)) {
104
+ found.push(at);
105
+ return;
106
+ }
107
+ if (isCallExpression(at)) {
108
+ for (const argument of at.arguments) {
109
+ collect(argument);
110
+ }
111
+ }
112
+ };
113
+ for (const statement of file.statements) {
114
+ if (isExportAssignment(statement) && !statement.isExportEquals) {
115
+ collect(statement.expression);
116
+ }
117
+ }
118
+ return found;
119
+ }
120
+ /**
121
+ * The value one member of an object literal is written with, when it is written as
122
+ * `name: value`, and undefined where the literal writes no such member.
123
+ */
124
+ function memberOf(object, name) {
125
+ if (!isObjectLiteralExpression(object)) {
126
+ return undefined;
127
+ }
128
+ for (const property of object.properties) {
129
+ if (isPropertyAssignment(property) &&
130
+ isIdentifier(property.name) &&
131
+ property.name.text === name) {
132
+ return property.initializer;
133
+ }
134
+ }
135
+ return undefined;
136
+ }
137
+ /**
138
+ * The strings one member holds, when it is written as a literal or an array of
139
+ * literals. `undefined` is a value the text does not state.
140
+ */
141
+ function literalsOf(value) {
142
+ const at = unwrapped(value);
143
+ const single = literalOf(at);
144
+ if (single !== undefined) {
145
+ return [single];
146
+ }
147
+ if (!isArrayLiteralExpression(at)) {
148
+ return undefined;
149
+ }
150
+ const found = [];
151
+ for (const element of at.elements) {
152
+ const text = literalOf(unwrapped(element));
153
+ if (text === undefined) {
154
+ return undefined;
155
+ }
156
+ found.push(text);
157
+ }
158
+ return found;
159
+ }
160
+ /** Whether a literal is relative in the sense a module specifier and a URL reference share. */
161
+ function isRelative(text) {
162
+ return RELATIVE_PREFIXES.some((prefix) => text.startsWith(prefix));
163
+ }
164
+ /** A URL reference with its query and its fragment removed, which name no part of the file. */
165
+ function pathPart(text) {
166
+ const cut = text.search(/[?#]/u);
167
+ return cut < 0 ? text : text.slice(0, cut);
168
+ }
169
+ /** The own files one relative literal names, read against a directory. */
170
+ function filesNamed(files, dir, text) {
171
+ if (!isRelative(text)) {
172
+ return [];
173
+ }
174
+ return files.named(resolvePath(dir, pathPart(text)));
175
+ }
176
+ /**
177
+ * The setup files one test-runner configuration names: every literal under
178
+ * `test.setupFiles` and `test.globalSetup`, read against the configuration's own
179
+ * directory. A configuration that moves its root names them against a directory its
180
+ * text may not state, so none is read from it.
181
+ */
182
+ function setupFilesOf(file, files) {
183
+ const objects = configObjects(file);
184
+ const tests = objects
185
+ .map((object) => memberOf(object, "test"))
186
+ .filter((value) => value !== undefined)
187
+ .map(unwrapped);
188
+ if ([...objects, ...tests].some((object) => memberOf(object, ROOT_MEMBER) !== undefined)) {
189
+ return [];
190
+ }
191
+ const dir = dirnamePath(file.fileName);
192
+ const found = [];
193
+ for (const test of tests) {
194
+ for (const name of SETUP_MEMBERS) {
195
+ const value = memberOf(test, name);
196
+ for (const text of value === undefined ? [] : (literalsOf(value) ?? [])) {
197
+ for (const named of filesNamed(files, dir, text)) {
198
+ found.push({ file: named, rule: "test-runner", source: text, exports: false });
199
+ }
200
+ }
201
+ }
202
+ }
203
+ return found;
204
+ }
205
+ /**
206
+ * The test files one browser-test configuration names: every own file below its
207
+ * test directory that its test-file pattern matches. The directory is the literal
208
+ * `testDir` reads against the configuration's directory, or that directory where no
209
+ * object of the configuration writes one; the pattern is the literal `testMatch`, or
210
+ * the runner's own where none is written. A member whose value is not a literal
211
+ * leaves the set unknown, and an unknown set enters nothing.
212
+ */
213
+ function browserTestsOf(file, files) {
214
+ const objects = configObjects(file);
215
+ if (objects.length === 0) {
216
+ return [];
217
+ }
218
+ let testDir = ".";
219
+ let patterns = [BROWSER_TEST_MATCH];
220
+ for (const object of objects) {
221
+ const dir = memberOf(object, "testDir");
222
+ if (dir !== undefined) {
223
+ testDir = literalOf(unwrapped(dir));
224
+ }
225
+ const match = memberOf(object, "testMatch");
226
+ if (match !== undefined) {
227
+ patterns = literalsOf(match);
228
+ }
229
+ }
230
+ if (testDir === undefined || patterns === undefined) {
231
+ return [];
232
+ }
233
+ const under = resolvePath(dirnamePath(file.fileName), testDir);
234
+ const found = [];
235
+ for (const pattern of patterns) {
236
+ const expression = globExpression(pattern);
237
+ for (const candidate of files.byName.values()) {
238
+ const below = relativePath(under, candidate.fileName);
239
+ if (below !== undefined && expression.test(below)) {
240
+ found.push({ file: candidate, rule: "browser-tests", source: pattern, exports: false });
241
+ }
242
+ }
243
+ }
244
+ return found;
245
+ }
246
+ /**
247
+ * The address one worker argument writes: a relative literal, or a `URL` built from
248
+ * a literal against `import.meta.url`. Any other argument is computed and addresses
249
+ * nothing the text states.
250
+ */
251
+ function workerAddress(argument) {
252
+ if (argument === undefined) {
253
+ return undefined;
254
+ }
255
+ const at = unwrapped(argument);
256
+ const literal = literalOf(at);
257
+ if (literal !== undefined) {
258
+ return { literal, names: [] };
259
+ }
260
+ if (!isNewExpression(at) || !isIdentifier(at.expression) || at.expression.text !== "URL") {
261
+ return undefined;
262
+ }
263
+ const [reference, base] = at.arguments ?? [];
264
+ const text = literalOf(reference === undefined ? undefined : unwrapped(reference));
265
+ if (text === undefined || base === undefined) {
266
+ return undefined;
267
+ }
268
+ const meta = unwrapped(base);
269
+ if (!isPropertyAccessExpression(meta) ||
270
+ !isMetaProperty(meta.expression) ||
271
+ !isIdentifier(meta.name) ||
272
+ meta.name.text !== "url") {
273
+ return undefined;
274
+ }
275
+ return { literal: text, names: [at.expression] };
276
+ }
277
+ /**
278
+ * Every call one file writes that addresses a worker: `new Worker(...)`,
279
+ * `new SharedWorker(...)` and `navigator.serviceWorker.register(...)`, each with an
280
+ * address its text states.
281
+ */
282
+ function workerSitesOf(file) {
283
+ const found = [];
284
+ const visit = (node) => {
285
+ if (isNewExpression(node) &&
286
+ isIdentifier(node.expression) &&
287
+ WORKER_CONSTRUCTORS.has(node.expression.text)) {
288
+ const address = workerAddress(node.arguments?.[0]);
289
+ if (address !== undefined) {
290
+ found.push({
291
+ literal: address.literal,
292
+ platformNames: [node.expression, ...address.names],
293
+ });
294
+ }
295
+ }
296
+ if (isCallExpression(node)) {
297
+ const callee = node.expression;
298
+ if (isPropertyAccessExpression(callee) &&
299
+ isIdentifier(callee.name) &&
300
+ callee.name.text === "register" &&
301
+ isPropertyAccessExpression(callee.expression) &&
302
+ isIdentifier(callee.expression.name) &&
303
+ callee.expression.name.text === "serviceWorker" &&
304
+ isIdentifier(callee.expression.expression) &&
305
+ callee.expression.expression.text === "navigator") {
306
+ const address = workerAddress(node.arguments[0]);
307
+ if (address !== undefined) {
308
+ found.push({
309
+ literal: address.literal,
310
+ platformNames: [callee.expression.expression, ...address.names],
311
+ });
312
+ }
313
+ }
314
+ }
315
+ node.forEachChild(visit);
316
+ };
317
+ file.forEachChild(visit);
318
+ return found;
319
+ }
320
+ /**
321
+ * The workers one file addresses. A call's names must resolve to the platform's own
322
+ * declarations, which no file of the target declares and no import brings in; a
323
+ * name the target binds itself is the target's own constructor or object, which
324
+ * addresses no worker this rule knows. One batch per file that writes such a call.
325
+ */
326
+ function workersOf(project, file, files) {
327
+ const sites = workerSitesOf(file);
328
+ if (sites.length === 0) {
329
+ return [];
330
+ }
331
+ const names = sites.flatMap((site) => site.platformNames);
332
+ const symbols = project.symbolsAt(names.map((name) => project.handle(name)));
333
+ const platform = new Set();
334
+ names.forEach((name, index) => {
335
+ const symbol = symbols[index];
336
+ if (symbol !== undefined &&
337
+ (symbol.flags & SymbolFlags.Alias) === 0 &&
338
+ symbol.declarations.length > 0 &&
339
+ symbol.declarations.every((handle) => !files.byName.has(handle.path))) {
340
+ platform.add(name);
341
+ }
342
+ });
343
+ const dir = dirnamePath(file.fileName);
344
+ const found = [];
345
+ for (const site of sites) {
346
+ if (!site.platformNames.every((name) => platform.has(name))) {
347
+ continue;
348
+ }
349
+ for (const named of filesNamed(files, dir, site.literal)) {
350
+ found.push({ file: named, rule: "worker", source: site.literal, exports: false });
351
+ }
352
+ }
353
+ return found;
354
+ }
355
+ /**
356
+ * Every file of one project the lifted rules enter, in no particular order and
357
+ * possibly more than once: a file two rules enter appears once per rule.
358
+ *
359
+ * `testFiles` is the configuration's test-file patterns, matched against each
360
+ * file's path below the target root; a file one of them matches is a test file the
361
+ * runner executes.
362
+ */
363
+ export function entryPoints(project, files, testFiles) {
364
+ const found = [];
365
+ const tests = testFiles.map((pattern) => ({ pattern, expression: globExpression(pattern) }));
366
+ for (const [path, file] of files.byPath) {
367
+ const named = conventionOf(basename(path));
368
+ if (named !== undefined) {
369
+ found.push({ file, rule: named.convention.rule, source: named.spelled, exports: true });
370
+ if (named.convention.testRunner) {
371
+ found.push(...setupFilesOf(file, files));
372
+ }
373
+ if (named.convention.rule === "browser-tests") {
374
+ found.push(...browserTestsOf(file, files));
375
+ }
376
+ }
377
+ for (const { pattern, expression } of tests) {
378
+ if (expression.test(path)) {
379
+ found.push({ file, rule: "test-runner", source: pattern, exports: false });
380
+ }
381
+ }
382
+ found.push(...workersOf(project, file, files));
383
+ }
384
+ return found;
385
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The members of a finding its emitter decides: the code, where the subject is,
3
+ * what the subject is, and the sentence the finding reports. They are the
4
+ * Contract's finding members of the same names, spelled in this language's case.
5
+ *
6
+ * Every other member the finding schema requires follows from these and from the
7
+ * run: the kind's name, its fixability and its ceiling come from the code's row of
8
+ * the issue-kind vocabulary, the severity and the confidence from the resolved
9
+ * configuration, the language from the analyzer. A report completes them, so an
10
+ * emitter cannot state one that disagrees with the vocabulary.
11
+ */
12
+ export {};
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Glob patterns over the paths of the target's own files, as the configuration
3
+ * writes them. Both `ts.test_files` and `ts.entry_files` name files this way, and
4
+ * one grammar answers for both.
5
+ *
6
+ * A configured root is not written in this grammar. It names a symbol reference
7
+ * rather than a path, `*` there spans the solidus and a brace list means nothing,
8
+ * so that rule is written where it is read.
9
+ */
10
+ /**
11
+ * One literal run of a pattern, with every character the expression syntax claims
12
+ * escaped.
13
+ *
14
+ * The set is the syntax characters and the solidus, and it is closed: an escape of any
15
+ * other character is refused outright by the expression syntax under the unicode flag,
16
+ * so escaping a hyphen, which needs none outside a character class, would refuse every
17
+ * pattern that names a hyphenated path.
18
+ */
19
+ function quoted(text) {
20
+ return text.replaceAll(/[.*+?^${}()|[\]\\/]/gu, (character) => `\\${character}`);
21
+ }
22
+ /**
23
+ * The expression one glob pattern denotes. `**` spans path separators while `*` and `?`
24
+ * do not, a brace list is an alternation of its members, and every other character
25
+ * stands for itself.
26
+ */
27
+ export function globExpression(pattern) {
28
+ let source = "^";
29
+ let at = 0;
30
+ while (at < pattern.length) {
31
+ const rest = pattern.slice(at);
32
+ if (rest.startsWith("**/")) {
33
+ source += "(?:[^/]*/)*";
34
+ at += 3;
35
+ continue;
36
+ }
37
+ if (rest.startsWith("**")) {
38
+ source += ".*";
39
+ at += 2;
40
+ continue;
41
+ }
42
+ const character = pattern[at] ?? "";
43
+ at += 1;
44
+ if (character === "*") {
45
+ source += "[^/]*";
46
+ continue;
47
+ }
48
+ if (character === "?") {
49
+ source += "[^/]";
50
+ continue;
51
+ }
52
+ if (character === "{") {
53
+ const close = pattern.indexOf("}", at);
54
+ if (close < 0) {
55
+ source += "\\{";
56
+ continue;
57
+ }
58
+ source += `(?:${pattern.slice(at, close).split(",").map(quoted).join("|")})`;
59
+ at = close + 1;
60
+ continue;
61
+ }
62
+ source += quoted(character);
63
+ }
64
+ return new RegExp(`${source}$`, "u");
65
+ }
package/dist/src/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { run, SETTING_OPTIONS } from "./run.js";
2
+ export { DECLINED_CONVENTIONS } from "./entry-point-gaps.js";
2
3
  export { inventory, nodeKey, } from "./inventory.js";
3
4
  export { DEFAULT_BATCH_CAP, references, } from "./references.js";
4
5
  export { byPosition, positionKey, PositionError, renderPosition, } from "./position.js";
@@ -0,0 +1,120 @@
1
+ /**
2
+ * The entry points a package manifest names, which are roots because something
3
+ * outside the analyzed program reaches them: a consumer importing the package, or a
4
+ * shell running its command.
5
+ *
6
+ * Only the target's own manifest is read. A manifest below the target root belongs
7
+ * to another package, whose entry points are that package's roots and not this
8
+ * target's.
9
+ */
10
+ import { isAbsolutePath, joinPath, resolvePath } from "./paths.js";
11
+ /** The manifest's own name. */
12
+ const MANIFEST = "package.json";
13
+ /** The three members that name an entry point beside `exports` and `bin`. */
14
+ const LEGACY_MEMBERS = ["main", "module", "types"];
15
+ function isRecord(value) {
16
+ return typeof value === "object" && value !== null && !Array.isArray(value);
17
+ }
18
+ /**
19
+ * Whether one member's value names a file of this package rather than another
20
+ * package or a URL. A target written with no leading dot is a path too, which is
21
+ * how the members that preceded `exports` are written.
22
+ */
23
+ function isTarget(value, requireDot) {
24
+ if (value === "" || isAbsolutePath(value) || value.includes("://")) {
25
+ return false;
26
+ }
27
+ return !requireDot || value.startsWith("./");
28
+ }
29
+ /** One member's spelling below another: `exports` then `exports["."]`. */
30
+ function below(member, key) {
31
+ return `${member}[${JSON.stringify(key)}]`;
32
+ }
33
+ /**
34
+ * Every target one `exports` value carries, at the member each is written under.
35
+ *
36
+ * The value is a string, an object whose keys are subpaths or conditions, or an
37
+ * array of alternatives, nested to any depth, and a `null` blocks a subpath rather
38
+ * than naming a target. Every level is walked because a condition names a different
39
+ * file of the same package and each is an entry point of its own; a key's meaning
40
+ * is not read, because a subpath and a condition both lead to a target and the
41
+ * walk needs no more than that.
42
+ */
43
+ function exportTargets(value, member, found) {
44
+ if (typeof value === "string") {
45
+ if (isTarget(value, true)) {
46
+ found.push({ member, path: value, role: "import", published: true });
47
+ }
48
+ return;
49
+ }
50
+ if (Array.isArray(value)) {
51
+ value.forEach((alternative, index) => {
52
+ exportTargets(alternative, below(member, String(index)), found);
53
+ });
54
+ return;
55
+ }
56
+ if (!isRecord(value)) {
57
+ return;
58
+ }
59
+ for (const [key, held] of Object.entries(value)) {
60
+ exportTargets(held, below(member, key), found);
61
+ }
62
+ }
63
+ /** The manifest at the target root, decoded, or undefined where there is none to read. */
64
+ function manifestOf(host, targetRoot) {
65
+ let text;
66
+ try {
67
+ text = host.readFile(joinPath(targetRoot, MANIFEST));
68
+ }
69
+ catch {
70
+ return undefined;
71
+ }
72
+ let value;
73
+ try {
74
+ value = JSON.parse(text);
75
+ }
76
+ catch {
77
+ // A manifest this program cannot read names no entry point, which is the same
78
+ // answer as an absent one. What a manifest's contents say is not refused here:
79
+ // the package manager owns that refusal, and an analysis of a tree it rejects
80
+ // is not the run's subject.
81
+ return undefined;
82
+ }
83
+ return isRecord(value) ? value : undefined;
84
+ }
85
+ /**
86
+ * What the target's own manifest declares about where the package is entered.
87
+ *
88
+ * A target is taken as written. It is not resolved to a source file here, because
89
+ * which file a target names is a fact about the compiler configuration that emits
90
+ * it and there is one such configuration per project, while there is one manifest.
91
+ */
92
+ export function readManifest(host, targetRoot) {
93
+ const manifest = manifestOf(host, targetRoot);
94
+ if (manifest === undefined) {
95
+ return { entries: [], declaresExports: false };
96
+ }
97
+ const found = [];
98
+ for (const member of LEGACY_MEMBERS) {
99
+ const value = manifest[member];
100
+ if (typeof value === "string" && isTarget(value, false)) {
101
+ found.push({ member, path: value, role: "import", published: true });
102
+ }
103
+ }
104
+ const bin = manifest["bin"];
105
+ if (typeof bin === "string" && isTarget(bin, false)) {
106
+ found.push({ member: "bin", path: bin, role: "run", published: false });
107
+ }
108
+ else if (isRecord(bin)) {
109
+ for (const [command, value] of Object.entries(bin)) {
110
+ if (typeof value === "string" && isTarget(value, false)) {
111
+ found.push({ member: below("bin", command), path: value, role: "run", published: false });
112
+ }
113
+ }
114
+ }
115
+ exportTargets(manifest["exports"], "exports", found);
116
+ return {
117
+ entries: found.map((entry) => ({ ...entry, path: resolvePath(targetRoot, entry.path) })),
118
+ declaresExports: manifest["exports"] !== undefined,
119
+ };
120
+ }