@cplieger/deadset-ts 8.0.0-dev.17 → 8.0.0-dev.19

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.
Files changed (86) hide show
  1. package/dist/src/analysis.js +30 -72
  2. package/dist/src/augmentations.js +1 -1
  3. package/dist/src/calls.js +267 -48
  4. package/dist/src/commands/held.js +499 -0
  5. package/dist/src/commands/read.js +16 -10
  6. package/dist/src/commands/shell.js +109 -15
  7. package/dist/src/commands/tools.js +1540 -149
  8. package/dist/src/conformance-record.js +1 -1
  9. package/dist/src/contract/contract.generated.js +9 -2
  10. package/dist/src/entries/conventions.js +7 -6
  11. package/dist/src/entries/test-runners.js +6 -40
  12. package/dist/src/entries/workflow-steps.js +202 -28
  13. package/dist/src/exempt.js +0 -26
  14. package/dist/src/exposure.js +400 -0
  15. package/dist/src/findings/completion.js +4 -17
  16. package/dist/src/findings/emitters.js +22 -33
  17. package/dist/src/findings/intra-function.js +3 -3
  18. package/dist/src/findings/non-code-artifacts.js +11 -5
  19. package/dist/src/findings/reads-and-writes.js +71 -15
  20. package/dist/src/findings/unused-declarations.js +2 -22
  21. package/dist/src/findings/visibility-narrowing.js +14 -20
  22. package/dist/src/inventory.js +18 -0
  23. package/dist/src/{components.js → liveness/components.js} +1 -1
  24. package/dist/src/{graph.js → liveness/graph.js} +38 -21
  25. package/dist/src/liveness/matrix.js +99 -0
  26. package/dist/src/liveness/sweep.js +558 -0
  27. package/dist/src/query.js +46 -0
  28. package/dist/src/references/sink.js +87 -1
  29. package/dist/src/references.js +1 -1
  30. package/dist/src/roots/manifest.js +47 -4
  31. package/dist/src/roots/published-reach.js +4 -4
  32. package/dist/src/roots/root-set.js +14 -16
  33. package/dist/src/text/named-files.js +257 -34
  34. package/dist/src/verbs/analyze.js +16 -1
  35. package/dist/src/verbs/explain.js +73 -152
  36. package/dist/src/verbs/print-retained.js +1 -1
  37. package/jsr.json +1 -1
  38. package/package.json +1 -1
  39. package/src/analysis.ts +54 -108
  40. package/src/augmentations.ts +1 -1
  41. package/src/calls.ts +326 -52
  42. package/src/commands/held.ts +632 -0
  43. package/src/commands/read.ts +26 -12
  44. package/src/commands/shell.ts +117 -15
  45. package/src/commands/tools.ts +1972 -202
  46. package/src/conformance-record.ts +1 -1
  47. package/src/contract/contract.generated.ts +9 -2
  48. package/src/dependencies.ts +1 -1
  49. package/src/entries/conventions.ts +8 -12
  50. package/src/entries/test-runners.ts +12 -45
  51. package/src/entries/workflow-steps.ts +227 -38
  52. package/src/exempt.ts +31 -46
  53. package/src/exposure.ts +492 -0
  54. package/src/file-facts.ts +5 -0
  55. package/src/finding.ts +1 -1
  56. package/src/findings/boundary.ts +7 -0
  57. package/src/findings/completion.ts +5 -19
  58. package/src/findings/emitters.ts +23 -41
  59. package/src/findings/interfaces.ts +3 -3
  60. package/src/findings/intra-function.ts +3 -3
  61. package/src/findings/non-code-artifacts.ts +16 -5
  62. package/src/findings/reads-and-writes.ts +99 -26
  63. package/src/findings/unused-declarations.ts +4 -28
  64. package/src/findings/visibility-narrowing.ts +25 -26
  65. package/src/interface-satisfaction.ts +1 -1
  66. package/src/inventory.ts +19 -0
  67. package/src/{components.ts → liveness/components.ts} +3 -3
  68. package/src/{graph.ts → liveness/graph.ts} +74 -26
  69. package/src/liveness/matrix.ts +162 -0
  70. package/src/liveness/sweep.ts +855 -0
  71. package/src/query.ts +81 -0
  72. package/src/references/sink.ts +101 -0
  73. package/src/references.ts +8 -1
  74. package/src/roots/manifest.ts +53 -8
  75. package/src/roots/published-reach.ts +9 -4
  76. package/src/roots/root-set.ts +18 -16
  77. package/src/stores.ts +2 -1
  78. package/src/tests/file-class.ts +19 -2
  79. package/src/text/named-files.ts +333 -36
  80. package/src/verbs/analyze.ts +16 -1
  81. package/src/verbs/explain.ts +81 -199
  82. package/src/verbs/print-retained.ts +1 -1
  83. package/dist/src/matrix.js +0 -275
  84. package/dist/src/sweep.js +0 -234
  85. package/src/matrix.ts +0 -466
  86. package/src/sweep.ts +0 -384
@@ -13,12 +13,15 @@ export class Sink {
13
13
  #testFile;
14
14
  #consumer;
15
15
  #found = [];
16
+ #declaredAt;
16
17
  #valued = new Map();
18
+ #named = new Map();
17
19
  #held = new Set();
18
- constructor(root, testFile, consumer) {
20
+ constructor(root, testFile, consumer, declaredAt) {
19
21
  this.#root = root;
20
22
  this.#testFile = testFile;
21
23
  this.#consumer = consumer;
24
+ this.#declaredAt = declaredAt;
22
25
  }
23
26
  /** Whether a test file wrote one walked file's uses. */
24
27
  isTest(file) {
@@ -47,6 +50,11 @@ export class Sink {
47
50
  test,
48
51
  ...(target.guessed === true ? { guessed: true } : {}),
49
52
  });
53
+ if (this.#consumer === undefined && !("column" in at)) {
54
+ const named = this.#named.get(file) ?? [];
55
+ named.push({ index: this.#found.length - 1, at, annotated: this.#annotates(at, from) });
56
+ this.#named.set(file, named);
57
+ }
50
58
  if (use === "write" && value !== undefined) {
51
59
  const valued = this.#valued.get(file) ?? [];
52
60
  valued.push({ index: this.#found.length - 1, value });
@@ -54,6 +62,26 @@ export class Sink {
54
62
  }
55
63
  }
56
64
  }
65
+ /**
66
+ * Whether a name sits in the annotation of `from`'s own declaration. An annotation holds
67
+ * no block, so the walk stops at the first declaration or block above the name.
68
+ */
69
+ #annotates(name, from) {
70
+ if (name.kind === SyntaxKind.SourceFile) {
71
+ return false;
72
+ }
73
+ let child = name;
74
+ let parent = name.parent;
75
+ while (parent.kind !== SyntaxKind.Block && parent.kind !== SyntaxKind.SourceFile) {
76
+ const id = this.#declaredAt(parent);
77
+ if (id !== undefined) {
78
+ return id === from && parent.type === child;
79
+ }
80
+ child = parent;
81
+ parent = parent.parent;
82
+ }
83
+ return false;
84
+ }
57
85
  /** Holds declarations an unanswered question could have made live, and the one whose read asked it. */
58
86
  hold(ids) {
59
87
  for (const id of ids) {
@@ -83,6 +111,14 @@ export class Sink {
83
111
  }
84
112
  }
85
113
  }
114
+ for (const [file, named] of this.#named) {
115
+ for (const { index, carriers } of carriersIn(named, this.#valued.get(file) ?? [], found)) {
116
+ const reference = found[index];
117
+ if (reference !== undefined) {
118
+ found[index] = { ...reference, carriers };
119
+ }
120
+ }
121
+ }
86
122
  found.sort((a, b) => byPosition(a.position, b.position) ||
87
123
  bytewise(a.to, b.to) ||
88
124
  bytewise(a.from, b.from) ||
@@ -149,3 +185,53 @@ function aloneIn(file, uses, writes, found) {
149
185
  }
150
186
  return result;
151
187
  }
188
+ /**
189
+ * The carriers of each named record of one file: `from` where its own annotation holds the
190
+ * name, then the target of each write whose stored value holds it and that names another
191
+ * declaration, the innermost value first. Values nest or are disjoint.
192
+ */
193
+ function carriersIn(named, writes, found) {
194
+ const byStart = [...named].sort((a, b) => a.at.pos - b.at.pos);
195
+ const holding = new Map();
196
+ for (const write of writes) {
197
+ let low = 0;
198
+ let high = byStart.length;
199
+ while (low < high) {
200
+ const middle = (low + high) >>> 1;
201
+ if ((byStart[middle]?.at.pos ?? 0) < write.value.pos) {
202
+ low = middle + 1;
203
+ }
204
+ else {
205
+ high = middle;
206
+ }
207
+ }
208
+ for (let at = low; at < byStart.length; at += 1) {
209
+ const one = byStart[at];
210
+ if (one === undefined || one.at.pos >= write.value.end) {
211
+ break;
212
+ }
213
+ if (one.at.end <= write.value.end) {
214
+ holding.set(one.index, [...(holding.get(one.index) ?? []), write]);
215
+ }
216
+ }
217
+ }
218
+ const result = [];
219
+ for (const one of named) {
220
+ const reference = found[one.index];
221
+ if (reference === undefined) {
222
+ continue;
223
+ }
224
+ const carriers = one.annotated ? [reference.from] : [];
225
+ const values = [...(holding.get(one.index) ?? [])].sort((a, b) => a.value.end - a.value.pos - (b.value.end - b.value.pos));
226
+ for (const write of values) {
227
+ const to = found[write.index]?.to;
228
+ if (to !== undefined && to !== reference.to && !carriers.includes(to)) {
229
+ carriers.push(to);
230
+ }
231
+ }
232
+ if (carriers.length > 0) {
233
+ result.push({ index: one.index, carriers });
234
+ }
235
+ }
236
+ return result;
237
+ }
@@ -49,10 +49,10 @@ export function references(project, held, targetRoot, options) {
49
49
  .ownSourceFiles()
50
50
  .filter((file) => relativePath(consumer.root, file.fileName) !== undefined &&
51
51
  relativePath(targetRoot, file.fileName) === undefined);
52
- const sink = new Sink(consumer?.root ?? targetRoot, options.testFile, consumer);
53
52
  const ownPaths = project.ownPaths();
54
53
  const fileIds = new Set(held.symbols.filter((symbol) => symbol.kind === "file").map((symbol) => symbol.id));
55
54
  const declaredAt = (node) => declarations.get(nodeKey(node.getSourceFile(), node));
55
+ const sink = new Sink(consumer?.root ?? targetRoot, options.testFile, consumer, declaredAt);
56
56
  let moduleBodies;
57
57
  /**
58
58
  * The declarations one module holds directly: a file's top level, or the body of a
@@ -27,8 +27,9 @@ function below(member, key) {
27
27
  }
28
28
  /**
29
29
  * The whole text of nodemon's configuration, the manifest's `nodemonConfig` member and the
30
- * `nodemon.json` beside it, by the member each is. Layer 3 holds it whole, because nodemon runs
31
- * commands from keys besides `exec`, `events` among them: https://github.com/remy/nodemon#config-files
30
+ * `nodemon.json` beside it, by the member each is. The dependency rule reads it whole, and
31
+ * layer 3 each of its values, because nodemon runs commands from keys besides `exec`, `events`
32
+ * among them: https://github.com/remy/nodemon#config-files
32
33
  */
33
34
  export function nodemonTexts(host, dir, manifest) {
34
35
  const member = manifest["nodemonConfig"];
@@ -44,6 +45,39 @@ export function nodemonTexts(host, dir, manifest) {
44
45
  ...(file === undefined ? [] : [{ member: "nodemon.json", text: file }]),
45
46
  ];
46
47
  }
48
+ /**
49
+ * The texts layer 3 reads of one text of nodemon's configuration: each value of it but its
50
+ * `exec` command, which is read as a command string of its own, those under `events` read as
51
+ * command strings too, and the whole text where it does not parse. Its keys are never read.
52
+ */
53
+ function nodemonValues(text) {
54
+ let config;
55
+ try {
56
+ config = JSON.parse(text);
57
+ }
58
+ catch {
59
+ return [{ text, shell: false }];
60
+ }
61
+ const found = [];
62
+ const visit = (value, shell) => {
63
+ if (Array.isArray(value) || isRecord(value)) {
64
+ for (const one of Object.values(value)) {
65
+ visit(one, shell);
66
+ }
67
+ }
68
+ else if (typeof value === "string" ||
69
+ typeof value === "number" ||
70
+ typeof value === "boolean") {
71
+ found.push({ text: String(value), shell });
72
+ }
73
+ };
74
+ for (const [key, value] of Object.entries(isRecord(config) ? config : { "": config })) {
75
+ if (key !== "exec" || typeof value !== "string") {
76
+ visit(value, key === "events");
77
+ }
78
+ }
79
+ return found;
80
+ }
47
81
  /**
48
82
  * The command strings one manifest runs, each at the member it is written under: each of its
49
83
  * `scripts`, and the `exec` command of nodemon's configuration, in the manifest's `nodemonConfig`
@@ -141,10 +175,19 @@ export function readManifest(host, targetRoot) {
141
175
  for (const path of new Set(pathsNamed(readCommands(command, ".")).map((one) => one.path))) {
142
176
  found.push({ member, path, role: "script", published: false });
143
177
  }
144
- found.push({ member, path: ".", role: "script", published: false, held: command });
178
+ found.push({
179
+ member,
180
+ path: ".",
181
+ role: "script",
182
+ published: false,
183
+ held: [{ text: command, shell: true, tested: member.startsWith("scripts[") }],
184
+ });
145
185
  }
146
186
  for (const { member, text } of nodemonTexts(host, targetRoot, manifest)) {
147
- found.push({ member, path: ".", role: "script", published: false, held: text });
187
+ const held = nodemonValues(text);
188
+ if (held.length > 0) {
189
+ found.push({ member, path: ".", role: "script", published: false, held });
190
+ }
148
191
  }
149
192
  return {
150
193
  entries: found.map((entry) => ({
@@ -182,7 +182,7 @@ function declarationNodes(held, byPath) {
182
182
  return {
183
183
  typeNamesOf: (symbol) => {
184
184
  const { byId, nodes } = nodesIn(symbol.position.path);
185
- return (byId.get(symbol.id) ?? []).flatMap((declaration) => typeNamesOf(declaration, nodes));
185
+ return (byId.get(symbol.id) ?? []).flatMap((declaration) => typeNamesOf(declaration, (nested) => nodes.has(nested)));
186
186
  },
187
187
  nodesOf: (symbol) => nodesIn(symbol.position.path).byId.get(symbol.id) ?? [],
188
188
  };
@@ -369,12 +369,12 @@ function inferredTypeSymbols(project, declarations) {
369
369
  /**
370
370
  * The names of types one declaration's own syntax writes: in a type reference, a
371
371
  * heritage clause or a type query. A function body is not read, and neither is a
372
- * declaration nested in this one, which `nested` holds and which is read on its own.
372
+ * declaration nested in this one that `readAlone` says is read on its own.
373
373
  */
374
- function typeNamesOf(declaration, nested) {
374
+ export function typeNamesOf(declaration, readAlone) {
375
375
  const found = [];
376
376
  const visit = (node) => {
377
- if (node !== declaration && nested.has(node)) {
377
+ if (node !== declaration && readAlone(node)) {
378
378
  return;
379
379
  }
380
380
  if (isTypeReferenceNode(node)) {
@@ -16,6 +16,7 @@ import { typeQueryAliases } from "../type-query-alias.js";
16
16
  import { bytewise } from "../text/compare.js";
17
17
  import { holdsManifest } from "../fs/manifest.js";
18
18
  import { globExpression } from "../text/glob.js";
19
+ import { heldWords } from "../commands/held.js";
19
20
  import { spelledFiles } from "../text/named-files.js";
20
21
  import { extensionOf, OUTPUT_EXTENSIONS } from "../extensions.js";
21
22
  import { publishedAPI, releaseTagged } from "./published-reach.js";
@@ -159,11 +160,12 @@ export function matchRef(pattern, ref) {
159
160
  return matchGlob(Array.from(pattern), Array.from(ref));
160
161
  }
161
162
  /**
162
- * Per package, the own files its held texts name, as `spelledFiles` reads them, but those a
163
- * root enters with all they export: a command can use no more of such a file than the root
164
- * keeps live. A manifest's are its entries' held texts, and the target root's package has the
165
- * workflow steps, git hooks and makefile recipes beside its own, with the whole text of each
166
- * file they are read from.
163
+ * Per package, the own files the words of its held texts name, as `spelledFiles` reads them,
164
+ * but those a root enters with all they export: a command can use no more of such a file than
165
+ * the root keeps live. A manifest's are its entries' held texts, and the target root's package
166
+ * has the workflow steps, git hooks and makefile recipes beside its own, with the values of
167
+ * each file they are read from. A package is listed too where a word that may write the file
168
+ * a command runs writes no path text.
167
169
  */
168
170
  function scriptHolds(entries, files, entered, host, targetRoot, workflows) {
169
171
  const rooted = new Set(entered.filter((one) => one.exports && one.names === undefined).map((one) => one.file.fileName));
@@ -171,24 +173,20 @@ function scriptHolds(entries, files, entered, host, targetRoot, workflows) {
171
173
  .filter((file) => !rooted.has(file.fileName))
172
174
  .map((file) => ({ file, paths: [file.fileName, ...files.writtenAs(file)] }));
173
175
  const cwd = host.workingDirectory();
174
- const commands = new Map([
175
- [
176
- resolvePath(cwd, targetRoot),
177
- [...workflows.readings.map((reading) => reading.held), ...workflows.texts],
178
- ],
179
- ]);
176
+ const root = resolvePath(cwd, targetRoot);
177
+ const commands = new Map([[root, [...workflows.held]]]);
180
178
  for (const entry of entries) {
181
179
  if (entry.held !== undefined) {
182
180
  const dir = resolvePath(cwd, entry.dir);
183
- commands.set(dir, [...(commands.get(dir) ?? []), entry.held]);
181
+ commands.set(dir, [...(commands.get(dir) ?? []), ...entry.held]);
184
182
  }
185
183
  }
186
184
  const held = [];
187
185
  for (const [dir, texts] of commands) {
188
- const named = spelledFiles(texts.join("\n"), dir, spelled);
189
- if (named.length > 0) {
190
- const paths = named.map((file) => relativePath(targetRoot, file.fileName) ?? file.fileName);
191
- held.push({ dir, files: paths });
186
+ const named = spelledFiles(heldWords(texts), dir, root, spelled);
187
+ if (named.files.length > 0 || named.unwritten) {
188
+ const paths = named.files.map((file) => relativePath(targetRoot, file.fileName) ?? file.fileName);
189
+ held.push({ dir, files: paths, unwritten: named.unwritten });
192
190
  }
193
191
  }
194
192
  return held;
@@ -1,59 +1,282 @@
1
- /**
2
- * Layer 3 of the command reader: the files and the package names a command's text names, read
3
- * as plain text. The text is searched for each name, which every spelling of what it names
4
- * contains, so no part of it is split, unquoted or skipped first.
5
- */
1
+ /** Layer 3 of the command reader: the files its words name, and the package names a command's text names. */
2
+ import { PART } from "../commands/shell.js";
3
+ import { APPENDED_EXTENSIONS, extensionOf } from "../extensions.js";
6
4
  import { dirnamePath, normalizePath, relativePath } from "../paths.js";
7
- /** The extension a source or an emitted file carries: a JavaScript or TypeScript one, or a declaration's. */
8
- const SOURCE_EXTENSION = /\.(?:d\.[cm]?ts|[cm]?[jt]s|[jt]sx)$/u;
5
+ /** The extensions a name of a file drops: a source's, an emitted file's, and a declaration's (roots.13). */
6
+ const NAMED_EXTENSIONS = [...APPENDED_EXTENSIONS, ".d.ts", ".d.mts", ".d.cts"];
7
+ /** A character a package's or a command's name may hold, so one beside a name makes it part of a longer one. */
8
+ const NAME_CHARACTER = /[\w.@-]/u;
9
+ const SEPARATOR = /[/\\]/u;
9
10
  /**
10
- * The names whose presence in a text holds one path below `dir`: its last name without the
11
- * extension, or with it where nothing else is left, and for an `index` file its directory's
12
- * last name, and `.` too where that is `dir`. Every spelling of the path relative to `dir`,
13
- * with or without `./`, `/` or `\`, its extension or its `/index` part, and every longer path
14
- * ending in one, holds one of them. None is empty, because an empty name is in every text.
11
+ * The names whose appearance as a path in a text holds one path below `dir`: its last segment
12
+ * without the extension, or whole where nothing else is left or it has none, and for an
13
+ * `index` file its directory's last segment, and `.` too where that is `dir`. Every spelling
14
+ * of the path that writes it as a path holds one of them (roots.13). None is empty, because
15
+ * an empty name is in every text.
15
16
  */
16
- function stemsOf(path, dir) {
17
+ function namesOf(path, dir) {
17
18
  const last = path.slice(path.lastIndexOf("/") + 1);
18
- const bare = last.replace(SOURCE_EXTENSION, "");
19
- const name = bare === "" ? last : bare;
20
- if (name !== "index") {
21
- return [name];
19
+ const bare = last.slice(0, last.length - (extensionOf(last, NAMED_EXTENSIONS)?.length ?? 0));
20
+ const own = bare === "" || bare === last ? { text: last, kind: "segment" } : { text: bare, kind: "stem" };
21
+ if (own.text !== "index") {
22
+ return [own];
22
23
  }
23
24
  const parent = dirnamePath(path);
25
+ const directory = parent.slice(parent.lastIndexOf("/") + 1);
24
26
  return [
25
- name,
26
- parent.slice(parent.lastIndexOf("/") + 1),
27
- ...(parent === normalizePath(dir) ? ["."] : []),
28
- ].filter((stem) => stem !== "");
27
+ own,
28
+ ...(directory === "" ? [] : [{ text: directory, kind: "directory" }]),
29
+ ...(parent === normalizePath(dir) ? [{ text: ".", kind: "root" }] : []),
30
+ ];
31
+ }
32
+ /**
33
+ * Whether a name that ends at `end` ends there as a path: no name character follows, nor a
34
+ * separator, which goes on to a path below it, nor a part, whose text goes on with the name,
35
+ * nor a `.` that a word character or a part follows, which goes on to another extension.
36
+ */
37
+ function endsAt(text, end) {
38
+ const next = text.charAt(end);
39
+ const after = text.charAt(end + 1);
40
+ return (!/[\w@/\\-]/u.test(next) &&
41
+ next !== PART &&
42
+ !(next === "." && (/\w/u.test(after) || after === PART)));
43
+ }
44
+ /** A character a word that writes a path may hold, a part only its run knows among them. */
45
+ const PATH_CHARACTER = new RegExp(`[\\w.@+/\\\\${PART}-]`, "u");
46
+ /** A run of {@link PATH_CHARACTER}s: a word as {@link noFileEnd} reads one. */
47
+ const PATH_WORD = new RegExp(`${PATH_CHARACTER.source}+`, "gu");
48
+ const SCOPED_PACKAGE = /^@[\w-][\w.-]*\/[\w-][\w.-]*(?:@[\w.-]*)?$/u;
49
+ /**
50
+ * The end of what `text` writes from `start`, where `word` starts, that names an image or a
51
+ * package and never a file, or `start` where it names none (roots.13): a value of a `docker:`
52
+ * that starts no longer name, and a scoped package's name, which no part goes on with, since
53
+ * a part beside it may make it a path.
54
+ */
55
+ function noFileEnd(text, start, word) {
56
+ const end = start + word.length;
57
+ const image = word.startsWith("//") &&
58
+ start >= 7 &&
59
+ text.startsWith("docker:", start - 7) &&
60
+ !NAME_CHARACTER.test(text.charAt(start - 8));
61
+ return image || (SCOPED_PACKAGE.test(word) && !holdsExtension(word)) ? end : start;
62
+ }
63
+ /** Each place of `text` that {@link noFileEnd} rules out, as a 1. */
64
+ function placesNamingNoFile(text) {
65
+ const ruledOut = new Uint8Array(text.length);
66
+ for (const { 0: word, index: start } of text.matchAll(PATH_WORD)) {
67
+ ruledOut.fill(1, start, noFileEnd(text, start, word));
68
+ }
69
+ return ruledOut;
70
+ }
71
+ /** The place after the separators `text` holds from `at`. */
72
+ function pastSeparators(text, at) {
73
+ let after = at;
74
+ while (SEPARATOR.test(text.charAt(after))) {
75
+ after += 1;
76
+ }
77
+ return after;
78
+ }
79
+ /** Whether `name`, found in `text` at `at`, appears there as a path (roots.13). */
80
+ function appearsAsPath(text, at, name) {
81
+ const before = text.charAt(at - 1);
82
+ const end = at + name.text.length;
83
+ if (before === PART) {
84
+ return false;
85
+ }
86
+ if (name.kind === "root") {
87
+ const after = pastSeparators(text, end);
88
+ return ([before, text.charAt(after)].every((one) => !NAME_CHARACTER.test(one) && !SEPARATOR.test(one)) && text.charAt(after) !== PART);
89
+ }
90
+ if (NAME_CHARACTER.test(before)) {
91
+ return false;
92
+ }
93
+ const after = pastSeparators(text, end);
94
+ if (name.kind === "directory" && after > end && endsAt(text, after)) {
95
+ return true;
96
+ }
97
+ if ((SEPARATOR.test(before) || (name.kind === "segment" && name.text.indexOf(".", 1) > 0)) &&
98
+ endsAt(text, end)) {
99
+ return true;
100
+ }
101
+ return (name.kind === "stem" &&
102
+ NAMED_EXTENSIONS.some((extension) => text.startsWith(extension, end) && endsAt(text, end + extension.length)));
103
+ }
104
+ /** Whether some text holds one of the extensions as a whole. */
105
+ function holdsExtension(written) {
106
+ return NAMED_EXTENSIONS.some((extension) => new RegExp(`${extension.replaceAll(".", "\\.")}(?!\\w)`, "u").test(written));
107
+ }
108
+ /** Whether a word's segment writes path text: text beside its parts other than `.` and `..`. */
109
+ function writesPath(segment) {
110
+ return segment.split(PART).some((piece) => piece !== "" && piece !== "." && piece !== "..");
111
+ }
112
+ /**
113
+ * Each run of path characters of `word`, at `start`, whose last segment holds a part, read as
114
+ * roots.13 reads one from past its last segment before the last that writes no path: one that
115
+ * writes none names nothing and is `unwritten` where the word may write the file a command runs,
116
+ * where a bare one names a file too. The word stands `depth` segments below the target root,
117
+ * whose own directories no checkout names alike: a leading `..` past it writes none, and an
118
+ * absolute run reads from its last segment that writes a path, `unwritten` where more is left.
119
+ */
120
+ function dynamicWords(word, start, ruledOut, depth) {
121
+ const { text, runs } = word;
122
+ const found = [];
123
+ let unwritten = false;
124
+ const read = (from, next) => {
125
+ const entry = next < 0;
126
+ // A `.` the word ends with ends a sentence, as endsAt reads one after a name.
127
+ const run = entry ? text : text.slice(from, next).replace(/\.+$/u, "");
128
+ const all = run.replaceAll("\\", "/").split("/");
129
+ const directory = all.length > 1 && all.at(-1) === "";
130
+ const written = all.filter((segment) => segment !== "" && segment !== ".");
131
+ const last = (written.at(-1) ?? "").split(PART);
132
+ // A runtime's entry is a path whatever it looks like, a scoped package's name among them.
133
+ if (!entry && (last.length < 2 || ruledOut[start + from] === 1)) {
134
+ return;
135
+ }
136
+ const climbs = written.findIndex((segment) => segment !== "..");
137
+ const above = Math.max(0, (climbs < 0 ? written.length : climbs) - depth);
138
+ const segments = written.filter((_, at) => at < climbs || at >= climbs + above);
139
+ if (!segments.some(writesPath)) {
140
+ unwritten ||= runs && (!entry || above > 0);
141
+ }
142
+ else if (all.length > 1 || holdsExtension(last.join("")) || runs) {
143
+ const kept = directory ? segments.length : segments.length - 1;
144
+ const anyDirectory = segments.slice(0, kept).findLastIndex((one) => !writesPath(one));
145
+ const absolute = all[0] === "" && all.length > 1 && anyDirectory < 0;
146
+ const from = absolute ? segments.findLastIndex(writesPath) : anyDirectory + 1;
147
+ unwritten ||= runs && absolute && from < segments.length - 1;
148
+ const glob = segments.slice(from).join("/");
149
+ found.push({ glob: glob === "" ? ["", ""] : glob.split(PART), directory });
150
+ }
151
+ };
152
+ if (runs && !text.includes(PART)) {
153
+ read(0, -1);
154
+ }
155
+ let at = text.indexOf(PART);
156
+ while (at >= 0) {
157
+ let from = at;
158
+ while (from > 0 && PATH_CHARACTER.test(text.charAt(from - 1))) {
159
+ from -= 1;
160
+ }
161
+ let next = at;
162
+ while (next < text.length && PATH_CHARACTER.test(text.charAt(next))) {
163
+ next += 1;
164
+ }
165
+ read(from, next);
166
+ at = text.indexOf(PART, Math.max(next, at + 1));
167
+ }
168
+ return { found, unwritten };
169
+ }
170
+ /** Whether `segment` is the text `glob` writes, any text standing between two of its parts. */
171
+ function standsFor(glob, segment) {
172
+ const first = glob[0] ?? "";
173
+ const last = glob.at(-1) ?? "";
174
+ if (glob.length === 1) {
175
+ return segment === first;
176
+ }
177
+ if (!segment.startsWith(first) ||
178
+ !segment.endsWith(last) ||
179
+ first.length + last.length > segment.length) {
180
+ return false;
181
+ }
182
+ let at = first.length;
183
+ const end = segment.length - last.length;
184
+ for (const middle of glob.slice(1, -1)) {
185
+ const found = segment.indexOf(middle, at);
186
+ if (found < 0 || found + middle.length > end) {
187
+ return false;
188
+ }
189
+ at = found + middle.length;
190
+ }
191
+ return true;
192
+ }
193
+ /** Whether a word's glob may write `path` or the end of it after a `/`. */
194
+ function mayStandFor(glob, path) {
195
+ let at = 0;
196
+ while (!standsFor(glob, path.slice(at))) {
197
+ const slash = path.indexOf("/", at);
198
+ if (slash < 0) {
199
+ return false;
200
+ }
201
+ at = slash + 1;
202
+ }
203
+ return true;
29
204
  }
30
205
  /**
31
- * Every file of `files` below `dir` that `text` names: one whose {@link stemsOf} a path of it
32
- * spells appears anywhere in the text. A file the text does not name may be held too, and no
33
- * file it names is missed.
206
+ * Each spelling of one path below `dir` a word may write the end of: the whole path from `root`,
207
+ * so a word may write the directories between them too, but no checkout's own, without its
208
+ * extension, and without its `/index` part, which alone is a directory's spelling.
34
209
  */
35
- export function spelledFiles(text, dir, files) {
36
- const byStem = new Map();
210
+ function spellingsBelow(path, dir, root) {
211
+ const fromRoot = relativePath(root, path);
212
+ if (relativePath(dir, path) === undefined || fromRoot === undefined) {
213
+ return { file: [], directory: [] };
214
+ }
215
+ const whole = `/${fromRoot}`;
216
+ const extension = extensionOf(whole, NAMED_EXTENSIONS);
217
+ const bare = extension === undefined ? whole : whole.slice(0, -extension.length);
218
+ const directory = bare.endsWith("/index") ? [bare.slice(0, -"/index".length)] : [];
219
+ return { file: [whole, bare, ...directory], directory };
220
+ }
221
+ /**
222
+ * Every file of `files` below `dir` that `words` name: one of whose {@link namesOf} a path of it
223
+ * has appears in a word as a path, or a spelling of one of whose paths a word that writes a
224
+ * path with a part only its run knows may stand for, and whether a word that may write the
225
+ * file a command runs writes no path text. A file the words do not name may be named too, and
226
+ * no file a word writes as a path outside an image's or a package's name is missed.
227
+ */
228
+ export function spelledFiles(words, dir, root, files) {
229
+ const byName = new Map();
37
230
  for (const { file, paths } of files) {
38
231
  const [own] = paths;
39
232
  if (own === undefined || relativePath(dir, own) === undefined) {
40
233
  continue;
41
234
  }
42
- for (const stem of new Set(paths.flatMap((path) => stemsOf(path, dir)))) {
43
- byStem.set(stem, [...(byStem.get(stem) ?? []), file]);
235
+ const seen = new Set();
236
+ for (const name of paths.flatMap((path) => namesOf(path, dir))) {
237
+ const key = `${name.kind}\u0000${name.text}`;
238
+ if (!seen.has(key)) {
239
+ seen.add(key);
240
+ byName.set(name.text, [...(byName.get(name.text) ?? []), { file, name }]);
241
+ }
44
242
  }
45
243
  }
46
244
  const found = new Set();
47
- // One pass per stem length: each place of the text is looked up once for every length.
48
- for (const length of new Set([...byStem.keys()].map((stem) => stem.length))) {
245
+ const text = words.map((word) => word.text).join("\n");
246
+ const ruledOut = placesNamingNoFile(text);
247
+ // One pass per name length: each place of the text is looked up once for every length.
248
+ for (const length of new Set([...byName.keys()].map((name) => name.length))) {
49
249
  for (let at = 0; at + length <= text.length; at += 1) {
50
- byStem.get(text.slice(at, at + length))?.forEach((file) => found.add(file));
250
+ for (const { file, name } of byName.get(text.slice(at, at + length)) ?? []) {
251
+ if (!found.has(file) && ruledOut[at] !== 1 && appearsAsPath(text, at, name)) {
252
+ found.add(file);
253
+ }
254
+ }
51
255
  }
52
256
  }
53
- return [...found];
257
+ const dynamic = [];
258
+ let unwritten = false;
259
+ let start = 0;
260
+ const depth = normalizePath(dir) === normalizePath(root)
261
+ ? 0
262
+ : (relativePath(root, dir)?.split("/").length ?? 0);
263
+ for (const word of words) {
264
+ const read = dynamicWords(word, start, ruledOut, depth);
265
+ dynamic.push(...read.found);
266
+ unwritten ||= read.unwritten;
267
+ start += word.text.length + 1;
268
+ }
269
+ if (dynamic.length > 0) {
270
+ for (const { file, paths } of files) {
271
+ const spellings = paths.map((path) => spellingsBelow(path, dir, root));
272
+ if (!found.has(file) &&
273
+ dynamic.some((word) => spellings.some((spelled) => (word.directory ? spelled.directory : spelled.file).some((one) => mayStandFor(word.glob, one))))) {
274
+ found.add(file);
275
+ }
276
+ }
277
+ }
278
+ return { files: [...found], unwritten };
54
279
  }
55
- /** A character a package's or a command's name may hold, so one beside a name makes it part of a longer one. */
56
- const NAME_CHARACTER = /[\w.@-]/u;
57
280
  /**
58
281
  * Each of `names`, a package's or a command's name, that `text` holds with no name character
59
282
  * before it and no character after it but one a path or a version may go on with, `/`, `.` or
@@ -98,13 +98,28 @@ function unseenLoadNotes(loads) {
98
98
  /** One note per package whose command strings name files a configuration holds, naming every such file. */
99
99
  function heldScriptNotes(held, targetRoot) {
100
100
  const files = new Map();
101
+ const unwritten = new Set();
101
102
  for (const one of held) {
102
103
  files.set(one.dir, new Set([...(files.get(one.dir) ?? []), ...one.files]));
104
+ if (one.unwritten) {
105
+ unwritten.add(one.dir);
106
+ }
103
107
  }
104
108
  return [...files].map(([dir, paths]) => ({
105
109
  kind: "unread-command",
106
110
  path: relativePath(targetRoot, dir) ?? ".",
107
- message: `commands the analysis does not read name ${[...paths].sort(bytewise).join(", ")}, so no finding is reported about ${paths.size === 1 ? "that file itself or the names it exports" : "those files themselves or the names they export"}`,
111
+ message: [
112
+ ...(paths.size === 0
113
+ ? []
114
+ : [
115
+ `commands the analysis does not read name ${[...paths].sort(bytewise).join(", ")}, so no finding is reported about ${paths.size === 1 ? "that file itself or the names it exports" : "those files themselves or the names they export"}`,
116
+ ]),
117
+ ...(unwritten.has(dir)
118
+ ? [
119
+ "a command runs a file whose path no word of it writes, so a finding that a file of the package is never built or never imported is possible at most",
120
+ ]
121
+ : []),
122
+ ].join("; "),
108
123
  }));
109
124
  }
110
125
  function unresolvedNotes(specifiers) {