@cplieger/deadset-ts 8.0.0-dev.18 → 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.
@@ -9,6 +9,7 @@ import type { Host } from "../host.ts";
9
9
  import { isAbsolutePath, joinPath, relativePath, resolvePath } from "../paths.ts";
10
10
  import { isRecord } from "../json-document.ts";
11
11
  import { MANIFEST, manifestAt, readJSON } from "../fs/manifest.ts";
12
+ import type { HeldText } from "../commands/held.ts";
12
13
  import { pathsNamed, readCommands } from "../commands/read.ts";
13
14
 
14
15
  /** The three members that name an entry point beside `exports` and `bin`. */
@@ -41,12 +42,12 @@ export interface ManifestEntry {
41
42
  /** The directory of the manifest that names it, which `path` was resolved against. */
42
43
  readonly dir: string;
43
44
  /**
44
- * On an entry that names no entry point, whose path is the manifest's directory, the text
45
- * layer 3 holds: a whole command string the package runs, or the whole text of nodemon's
45
+ * On an entry that names no entry point, whose path is the manifest's directory, the texts
46
+ * layer 3 reads: a whole command string the package runs, or the values of nodemon's
46
47
  * configuration one is read from. Rooting enters no file for it and holds each file
47
- * `spelledFiles` reads the text to name.
48
+ * `spelledFiles` reads their words to name.
48
49
  */
49
- readonly held?: string;
50
+ readonly held?: readonly HeldText[];
50
51
  }
51
52
 
52
53
  /**
@@ -68,8 +69,9 @@ function below(member: string, key: string): string {
68
69
 
69
70
  /**
70
71
  * The whole text of nodemon's configuration, the manifest's `nodemonConfig` member and the
71
- * `nodemon.json` beside it, by the member each is. Layer 3 holds it whole, because nodemon runs
72
- * commands from keys besides `exec`, `events` among them: https://github.com/remy/nodemon#config-files
72
+ * `nodemon.json` beside it, by the member each is. The dependency rule reads it whole, and
73
+ * layer 3 each of its values, because nodemon runs commands from keys besides `exec`, `events`
74
+ * among them: https://github.com/remy/nodemon#config-files
73
75
  */
74
76
  export function nodemonTexts(
75
77
  host: Host,
@@ -89,6 +91,40 @@ export function nodemonTexts(
89
91
  ];
90
92
  }
91
93
 
94
+ /**
95
+ * The texts layer 3 reads of one text of nodemon's configuration: each value of it but its
96
+ * `exec` command, which is read as a command string of its own, those under `events` read as
97
+ * command strings too, and the whole text where it does not parse. Its keys are never read.
98
+ */
99
+ function nodemonValues(text: string): readonly HeldText[] {
100
+ let config: unknown;
101
+ try {
102
+ config = JSON.parse(text);
103
+ } catch {
104
+ return [{ text, shell: false }];
105
+ }
106
+ const found: HeldText[] = [];
107
+ const visit = (value: unknown, shell: boolean): void => {
108
+ if (Array.isArray(value) || isRecord(value)) {
109
+ for (const one of Object.values(value)) {
110
+ visit(one, shell);
111
+ }
112
+ } else if (
113
+ typeof value === "string" ||
114
+ typeof value === "number" ||
115
+ typeof value === "boolean"
116
+ ) {
117
+ found.push({ text: String(value), shell });
118
+ }
119
+ };
120
+ for (const [key, value] of Object.entries(isRecord(config) ? config : { "": config })) {
121
+ if (key !== "exec" || typeof value !== "string") {
122
+ visit(value, key === "events");
123
+ }
124
+ }
125
+ return found;
126
+ }
127
+
92
128
  /**
93
129
  * The command strings one manifest runs, each at the member it is written under: each of its
94
130
  * `scripts`, and the `exec` command of nodemon's configuration, in the manifest's `nodemonConfig`
@@ -219,10 +255,19 @@ export function readManifest(host: Host, targetRoot: string): Manifest {
219
255
  for (const path of new Set(pathsNamed(readCommands(command, ".")).map((one) => one.path))) {
220
256
  found.push({ member, path, role: "script", published: false });
221
257
  }
222
- found.push({ member, path: ".", role: "script", published: false, held: command });
258
+ found.push({
259
+ member,
260
+ path: ".",
261
+ role: "script",
262
+ published: false,
263
+ held: [{ text: command, shell: true, tested: member.startsWith("scripts[") }],
264
+ });
223
265
  }
224
266
  for (const { member, text } of nodemonTexts(host, targetRoot, manifest)) {
225
- found.push({ member, path: ".", role: "script", published: false, held: text });
267
+ const held = nodemonValues(text);
268
+ if (held.length > 0) {
269
+ found.push({ member, path: ".", role: "script", published: false, held });
270
+ }
226
271
  }
227
272
 
228
273
  return {
@@ -22,6 +22,7 @@ import { typeQueryAliases } from "../type-query-alias.ts";
22
22
  import { bytewise } from "../text/compare.ts";
23
23
  import { holdsManifest } from "../fs/manifest.ts";
24
24
  import { globExpression } from "../text/glob.ts";
25
+ import { heldWords, type HeldText } from "../commands/held.ts";
25
26
  import { spelledFiles } from "../text/named-files.ts";
26
27
  import type { Workflows } from "../entries/workflow-steps.ts";
27
28
  import { extensionOf, OUTPUT_EXTENSIONS } from "../extensions.ts";
@@ -104,6 +105,8 @@ export interface HeldScripts {
104
105
  readonly dir: string;
105
106
  /** Each file, by its path below the target root. */
106
107
  readonly files: readonly string[];
108
+ /** Whether a word that may write the file a command runs writes no path text (`roots.13`). */
109
+ readonly unwritten: boolean;
107
110
  }
108
111
 
109
112
  /** What decides the roots beside the declarations themselves. */
@@ -304,11 +307,12 @@ export function matchRef(pattern: string, ref: string): boolean {
304
307
  }
305
308
 
306
309
  /**
307
- * Per package, the own files its held texts name, as `spelledFiles` reads them, but those a
308
- * root enters with all they export: a command can use no more of such a file than the root
309
- * keeps live. A manifest's are its entries' held texts, and the target root's package has the
310
- * workflow steps, git hooks and makefile recipes beside its own, with the whole text of each
311
- * file they are read from.
310
+ * Per package, the own files the words of its held texts name, as `spelledFiles` reads them,
311
+ * but those a root enters with all they export: a command can use no more of such a file than
312
+ * the root keeps live. A manifest's are its entries' held texts, and the target root's package
313
+ * has the workflow steps, git hooks and makefile recipes beside its own, with the values of
314
+ * each file they are read from. A package is listed too where a word that may write the file
315
+ * a command runs writes no path text.
312
316
  */
313
317
  function scriptHolds(
314
318
  entries: readonly ManifestEntry[],
@@ -325,24 +329,22 @@ function scriptHolds(
325
329
  .filter((file) => !rooted.has(file.fileName))
326
330
  .map((file) => ({ file, paths: [file.fileName, ...files.writtenAs(file)] }));
327
331
  const cwd = host.workingDirectory();
328
- const commands = new Map<string, string[]>([
329
- [
330
- resolvePath(cwd, targetRoot),
331
- [...workflows.readings.map((reading) => reading.held), ...workflows.texts],
332
- ],
333
- ]);
332
+ const root = resolvePath(cwd, targetRoot);
333
+ const commands = new Map<string, HeldText[]>([[root, [...workflows.held]]]);
334
334
  for (const entry of entries) {
335
335
  if (entry.held !== undefined) {
336
336
  const dir = resolvePath(cwd, entry.dir);
337
- commands.set(dir, [...(commands.get(dir) ?? []), entry.held]);
337
+ commands.set(dir, [...(commands.get(dir) ?? []), ...entry.held]);
338
338
  }
339
339
  }
340
340
  const held: HeldScripts[] = [];
341
341
  for (const [dir, texts] of commands) {
342
- const named = spelledFiles(texts.join("\n"), dir, spelled);
343
- if (named.length > 0) {
344
- const paths = named.map((file) => relativePath(targetRoot, file.fileName) ?? file.fileName);
345
- held.push({ dir, files: paths });
342
+ const named = spelledFiles(heldWords(texts), dir, root, spelled);
343
+ if (named.files.length > 0 || named.unwritten) {
344
+ const paths = named.files.map(
345
+ (file) => relativePath(targetRoot, file.fileName) ?? file.fileName,
346
+ );
347
+ held.push({ dir, files: paths, unwritten: named.unwritten });
346
348
  }
347
349
  }
348
350
  return held;
@@ -1,13 +1,30 @@
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. */
6
2
 
3
+ import type { HeldWord } from "../commands/held.ts";
4
+ import { PART } from "../commands/shell.ts";
5
+ import { APPENDED_EXTENSIONS, extensionOf } from "../extensions.ts";
7
6
  import { dirnamePath, normalizePath, relativePath } from "../paths.ts";
8
7
 
9
- /** The extension a source or an emitted file carries: a JavaScript or TypeScript one, or a declaration's. */
10
- const SOURCE_EXTENSION = /\.(?:d\.[cm]?ts|[cm]?[jt]s|[jt]sx)$/u;
8
+ /** The extensions a name of a file drops: a source's, an emitted file's, and a declaration's (roots.13). */
9
+ const NAMED_EXTENSIONS = [...APPENDED_EXTENSIONS, ".d.ts", ".d.mts", ".d.cts"];
10
+
11
+ /** A character a package's or a command's name may hold, so one beside a name makes it part of a longer one. */
12
+ const NAME_CHARACTER = /[\w.@-]/u;
13
+
14
+ const SEPARATOR = /[/\\]/u;
15
+
16
+ /**
17
+ * What makes one name of a file a path where it appears: a `stem`, a last segment without its
18
+ * extension, may have the extension after it; a `segment`, a whole last segment, is one where
19
+ * it holds an extension of its own; a `directory`, an `index` file's, may have a separator
20
+ * after it; and `root` is `.`, the package's own directory.
21
+ */
22
+ type NameKind = "stem" | "segment" | "directory" | "root";
23
+
24
+ interface FileName {
25
+ readonly text: string;
26
+ readonly kind: NameKind;
27
+ }
11
28
 
12
29
  /** One file, and every path it is spelled by: its own and each one the project writes it to. */
13
30
  interface Spelled<File> {
@@ -16,60 +33,340 @@ interface Spelled<File> {
16
33
  }
17
34
 
18
35
  /**
19
- * The names whose presence in a text holds one path below `dir`: its last name without the
20
- * extension, or with it where nothing else is left, and for an `index` file its directory's
21
- * last name, and `.` too where that is `dir`. Every spelling of the path relative to `dir`,
22
- * with or without `./`, `/` or `\`, its extension or its `/index` part, and every longer path
23
- * ending in one, holds one of them. None is empty, because an empty name is in every text.
36
+ * The names whose appearance as a path in a text holds one path below `dir`: its last segment
37
+ * without the extension, or whole where nothing else is left or it has none, and for an
38
+ * `index` file its directory's last segment, and `.` too where that is `dir`. Every spelling
39
+ * of the path that writes it as a path holds one of them (roots.13). None is empty, because
40
+ * an empty name is in every text.
24
41
  */
25
- function stemsOf(path: string, dir: string): readonly string[] {
42
+ function namesOf(path: string, dir: string): readonly FileName[] {
26
43
  const last = path.slice(path.lastIndexOf("/") + 1);
27
- const bare = last.replace(SOURCE_EXTENSION, "");
28
- const name = bare === "" ? last : bare;
29
- if (name !== "index") {
30
- return [name];
44
+ const bare = last.slice(0, last.length - (extensionOf(last, NAMED_EXTENSIONS)?.length ?? 0));
45
+ const own: FileName =
46
+ bare === "" || bare === last ? { text: last, kind: "segment" } : { text: bare, kind: "stem" };
47
+ if (own.text !== "index") {
48
+ return [own];
31
49
  }
32
50
  const parent = dirnamePath(path);
51
+ const directory = parent.slice(parent.lastIndexOf("/") + 1);
33
52
  return [
34
- name,
35
- parent.slice(parent.lastIndexOf("/") + 1),
36
- ...(parent === normalizePath(dir) ? ["."] : []),
37
- ].filter((stem) => stem !== "");
53
+ own,
54
+ ...(directory === "" ? [] : [{ text: directory, kind: "directory" as const }]),
55
+ ...(parent === normalizePath(dir) ? [{ text: ".", kind: "root" as const }] : []),
56
+ ];
57
+ }
58
+
59
+ /**
60
+ * Whether a name that ends at `end` ends there as a path: no name character follows, nor a
61
+ * separator, which goes on to a path below it, nor a part, whose text goes on with the name,
62
+ * nor a `.` that a word character or a part follows, which goes on to another extension.
63
+ */
64
+ function endsAt(text: string, end: number): boolean {
65
+ const next = text.charAt(end);
66
+ const after = text.charAt(end + 1);
67
+ return (
68
+ !/[\w@/\\-]/u.test(next) &&
69
+ next !== PART &&
70
+ !(next === "." && (/\w/u.test(after) || after === PART))
71
+ );
72
+ }
73
+
74
+ /** A character a word that writes a path may hold, a part only its run knows among them. */
75
+ const PATH_CHARACTER = new RegExp(`[\\w.@+/\\\\${PART}-]`, "u");
76
+
77
+ /** A run of {@link PATH_CHARACTER}s: a word as {@link noFileEnd} reads one. */
78
+ const PATH_WORD = new RegExp(`${PATH_CHARACTER.source}+`, "gu");
79
+
80
+ const SCOPED_PACKAGE = /^@[\w-][\w.-]*\/[\w-][\w.-]*(?:@[\w.-]*)?$/u;
81
+
82
+ /**
83
+ * The end of what `text` writes from `start`, where `word` starts, that names an image or a
84
+ * package and never a file, or `start` where it names none (roots.13): a value of a `docker:`
85
+ * that starts no longer name, and a scoped package's name, which no part goes on with, since
86
+ * a part beside it may make it a path.
87
+ */
88
+ function noFileEnd(text: string, start: number, word: string): number {
89
+ const end = start + word.length;
90
+ const image =
91
+ word.startsWith("//") &&
92
+ start >= 7 &&
93
+ text.startsWith("docker:", start - 7) &&
94
+ !NAME_CHARACTER.test(text.charAt(start - 8));
95
+ return image || (SCOPED_PACKAGE.test(word) && !holdsExtension(word)) ? end : start;
96
+ }
97
+
98
+ /** Each place of `text` that {@link noFileEnd} rules out, as a 1. */
99
+ function placesNamingNoFile(text: string): Uint8Array {
100
+ const ruledOut = new Uint8Array(text.length);
101
+ for (const { 0: word, index: start } of text.matchAll(PATH_WORD)) {
102
+ ruledOut.fill(1, start, noFileEnd(text, start, word));
103
+ }
104
+ return ruledOut;
105
+ }
106
+
107
+ /** The place after the separators `text` holds from `at`. */
108
+ function pastSeparators(text: string, at: number): number {
109
+ let after = at;
110
+ while (SEPARATOR.test(text.charAt(after))) {
111
+ after += 1;
112
+ }
113
+ return after;
114
+ }
115
+
116
+ /** Whether `name`, found in `text` at `at`, appears there as a path (roots.13). */
117
+ function appearsAsPath(text: string, at: number, name: FileName): boolean {
118
+ const before = text.charAt(at - 1);
119
+ const end = at + name.text.length;
120
+ if (before === PART) {
121
+ return false;
122
+ }
123
+ if (name.kind === "root") {
124
+ const after = pastSeparators(text, end);
125
+ return (
126
+ [before, text.charAt(after)].every(
127
+ (one) => !NAME_CHARACTER.test(one) && !SEPARATOR.test(one),
128
+ ) && text.charAt(after) !== PART
129
+ );
130
+ }
131
+ if (NAME_CHARACTER.test(before)) {
132
+ return false;
133
+ }
134
+ const after = pastSeparators(text, end);
135
+ if (name.kind === "directory" && after > end && endsAt(text, after)) {
136
+ return true;
137
+ }
138
+ if (
139
+ (SEPARATOR.test(before) || (name.kind === "segment" && name.text.indexOf(".", 1) > 0)) &&
140
+ endsAt(text, end)
141
+ ) {
142
+ return true;
143
+ }
144
+ return (
145
+ name.kind === "stem" &&
146
+ NAMED_EXTENSIONS.some(
147
+ (extension) => text.startsWith(extension, end) && endsAt(text, end + extension.length),
148
+ )
149
+ );
150
+ }
151
+
152
+ /** Whether some text holds one of the extensions as a whole. */
153
+ function holdsExtension(written: string): boolean {
154
+ return NAMED_EXTENSIONS.some((extension) =>
155
+ new RegExp(`${extension.replaceAll(".", "\\.")}(?!\\w)`, "u").test(written),
156
+ );
157
+ }
158
+
159
+ /**
160
+ * A word with a part only its run knows: the text a path it names ends in after a `/`, split
161
+ * at each part, and whether it names a directory.
162
+ */
163
+ interface DynamicWord {
164
+ readonly glob: readonly string[];
165
+ readonly directory: boolean;
166
+ }
167
+
168
+ /** Whether a word's segment writes path text: text beside its parts other than `.` and `..`. */
169
+ function writesPath(segment: string): boolean {
170
+ return segment.split(PART).some((piece) => piece !== "" && piece !== "." && piece !== "..");
38
171
  }
39
172
 
40
173
  /**
41
- * Every file of `files` below `dir` that `text` names: one whose {@link stemsOf} a path of it
42
- * spells appears anywhere in the text. A file the text does not name may be held too, and no
43
- * file it names is missed.
174
+ * Each run of path characters of `word`, at `start`, whose last segment holds a part, read as
175
+ * roots.13 reads one from past its last segment before the last that writes no path: one that
176
+ * writes none names nothing and is `unwritten` where the word may write the file a command runs,
177
+ * where a bare one names a file too. The word stands `depth` segments below the target root,
178
+ * whose own directories no checkout names alike: a leading `..` past it writes none, and an
179
+ * absolute run reads from its last segment that writes a path, `unwritten` where more is left.
180
+ */
181
+ function dynamicWords(
182
+ word: HeldWord,
183
+ start: number,
184
+ ruledOut: Uint8Array,
185
+ depth: number,
186
+ ): { readonly found: readonly DynamicWord[]; readonly unwritten: boolean } {
187
+ const { text, runs } = word;
188
+ const found: DynamicWord[] = [];
189
+ let unwritten = false;
190
+ const read = (from: number, next: number): void => {
191
+ const entry = next < 0;
192
+ // A `.` the word ends with ends a sentence, as endsAt reads one after a name.
193
+ const run = entry ? text : text.slice(from, next).replace(/\.+$/u, "");
194
+ const all = run.replaceAll("\\", "/").split("/");
195
+ const directory = all.length > 1 && all.at(-1) === "";
196
+ const written = all.filter((segment) => segment !== "" && segment !== ".");
197
+ const last = (written.at(-1) ?? "").split(PART);
198
+ // A runtime's entry is a path whatever it looks like, a scoped package's name among them.
199
+ if (!entry && (last.length < 2 || ruledOut[start + from] === 1)) {
200
+ return;
201
+ }
202
+ const climbs = written.findIndex((segment) => segment !== "..");
203
+ const above = Math.max(0, (climbs < 0 ? written.length : climbs) - depth);
204
+ const segments = written.filter((_, at) => at < climbs || at >= climbs + above);
205
+ if (!segments.some(writesPath)) {
206
+ unwritten ||= runs && (!entry || above > 0);
207
+ } else if (all.length > 1 || holdsExtension(last.join("")) || runs) {
208
+ const kept = directory ? segments.length : segments.length - 1;
209
+ const anyDirectory = segments.slice(0, kept).findLastIndex((one) => !writesPath(one));
210
+ const absolute = all[0] === "" && all.length > 1 && anyDirectory < 0;
211
+ const from = absolute ? segments.findLastIndex(writesPath) : anyDirectory + 1;
212
+ unwritten ||= runs && absolute && from < segments.length - 1;
213
+ const glob = segments.slice(from).join("/");
214
+ found.push({ glob: glob === "" ? ["", ""] : glob.split(PART), directory });
215
+ }
216
+ };
217
+ if (runs && !text.includes(PART)) {
218
+ read(0, -1);
219
+ }
220
+ let at = text.indexOf(PART);
221
+ while (at >= 0) {
222
+ let from = at;
223
+ while (from > 0 && PATH_CHARACTER.test(text.charAt(from - 1))) {
224
+ from -= 1;
225
+ }
226
+ let next = at;
227
+ while (next < text.length && PATH_CHARACTER.test(text.charAt(next))) {
228
+ next += 1;
229
+ }
230
+ read(from, next);
231
+ at = text.indexOf(PART, Math.max(next, at + 1));
232
+ }
233
+ return { found, unwritten };
234
+ }
235
+
236
+ /** Whether `segment` is the text `glob` writes, any text standing between two of its parts. */
237
+ function standsFor(glob: readonly string[], segment: string): boolean {
238
+ const first = glob[0] ?? "";
239
+ const last = glob.at(-1) ?? "";
240
+ if (glob.length === 1) {
241
+ return segment === first;
242
+ }
243
+ if (
244
+ !segment.startsWith(first) ||
245
+ !segment.endsWith(last) ||
246
+ first.length + last.length > segment.length
247
+ ) {
248
+ return false;
249
+ }
250
+ let at = first.length;
251
+ const end = segment.length - last.length;
252
+ for (const middle of glob.slice(1, -1)) {
253
+ const found = segment.indexOf(middle, at);
254
+ if (found < 0 || found + middle.length > end) {
255
+ return false;
256
+ }
257
+ at = found + middle.length;
258
+ }
259
+ return true;
260
+ }
261
+
262
+ /** Whether a word's glob may write `path` or the end of it after a `/`. */
263
+ function mayStandFor(glob: readonly string[], path: string): boolean {
264
+ let at = 0;
265
+ while (!standsFor(glob, path.slice(at))) {
266
+ const slash = path.indexOf("/", at);
267
+ if (slash < 0) {
268
+ return false;
269
+ }
270
+ at = slash + 1;
271
+ }
272
+ return true;
273
+ }
274
+
275
+ /**
276
+ * Each spelling of one path below `dir` a word may write the end of: the whole path from `root`,
277
+ * so a word may write the directories between them too, but no checkout's own, without its
278
+ * extension, and without its `/index` part, which alone is a directory's spelling.
279
+ */
280
+ function spellingsBelow(
281
+ path: string,
282
+ dir: string,
283
+ root: string,
284
+ ): { readonly file: readonly string[]; readonly directory: readonly string[] } {
285
+ const fromRoot = relativePath(root, path);
286
+ if (relativePath(dir, path) === undefined || fromRoot === undefined) {
287
+ return { file: [], directory: [] };
288
+ }
289
+ const whole = `/${fromRoot}`;
290
+ const extension = extensionOf(whole, NAMED_EXTENSIONS);
291
+ const bare = extension === undefined ? whole : whole.slice(0, -extension.length);
292
+ const directory = bare.endsWith("/index") ? [bare.slice(0, -"/index".length)] : [];
293
+ return { file: [whole, bare, ...directory], directory };
294
+ }
295
+
296
+ /**
297
+ * Every file of `files` below `dir` that `words` name: one of whose {@link namesOf} a path of it
298
+ * has appears in a word as a path, or a spelling of one of whose paths a word that writes a
299
+ * path with a part only its run knows may stand for, and whether a word that may write the
300
+ * file a command runs writes no path text. A file the words do not name may be named too, and
301
+ * no file a word writes as a path outside an image's or a package's name is missed.
44
302
  */
45
303
  export function spelledFiles<File>(
46
- text: string,
304
+ words: readonly HeldWord[],
47
305
  dir: string,
306
+ root: string,
48
307
  files: readonly Spelled<File>[],
49
- ): readonly File[] {
50
- const byStem = new Map<string, File[]>();
308
+ ): { readonly files: readonly File[]; readonly unwritten: boolean } {
309
+ const byName = new Map<string, { readonly file: File; readonly name: FileName }[]>();
51
310
  for (const { file, paths } of files) {
52
311
  const [own] = paths;
53
312
  if (own === undefined || relativePath(dir, own) === undefined) {
54
313
  continue;
55
314
  }
56
- for (const stem of new Set(paths.flatMap((path) => stemsOf(path, dir)))) {
57
- byStem.set(stem, [...(byStem.get(stem) ?? []), file]);
315
+ const seen = new Set<string>();
316
+ for (const name of paths.flatMap((path) => namesOf(path, dir))) {
317
+ const key = `${name.kind}\u0000${name.text}`;
318
+ if (!seen.has(key)) {
319
+ seen.add(key);
320
+ byName.set(name.text, [...(byName.get(name.text) ?? []), { file, name }]);
321
+ }
58
322
  }
59
323
  }
60
324
  const found = new Set<File>();
61
- // One pass per stem length: each place of the text is looked up once for every length.
62
- for (const length of new Set([...byStem.keys()].map((stem) => stem.length))) {
325
+ const text = words.map((word) => word.text).join("\n");
326
+ const ruledOut = placesNamingNoFile(text);
327
+ // One pass per name length: each place of the text is looked up once for every length.
328
+ for (const length of new Set([...byName.keys()].map((name) => name.length))) {
63
329
  for (let at = 0; at + length <= text.length; at += 1) {
64
- byStem.get(text.slice(at, at + length))?.forEach((file) => found.add(file));
330
+ for (const { file, name } of byName.get(text.slice(at, at + length)) ?? []) {
331
+ if (!found.has(file) && ruledOut[at] !== 1 && appearsAsPath(text, at, name)) {
332
+ found.add(file);
333
+ }
334
+ }
65
335
  }
66
336
  }
67
- return [...found];
337
+ const dynamic: DynamicWord[] = [];
338
+ let unwritten = false;
339
+ let start = 0;
340
+ const depth =
341
+ normalizePath(dir) === normalizePath(root)
342
+ ? 0
343
+ : (relativePath(root, dir)?.split("/").length ?? 0);
344
+ for (const word of words) {
345
+ const read = dynamicWords(word, start, ruledOut, depth);
346
+ dynamic.push(...read.found);
347
+ unwritten ||= read.unwritten;
348
+ start += word.text.length + 1;
349
+ }
350
+ if (dynamic.length > 0) {
351
+ for (const { file, paths } of files) {
352
+ const spellings = paths.map((path) => spellingsBelow(path, dir, root));
353
+ if (
354
+ !found.has(file) &&
355
+ dynamic.some((word) =>
356
+ spellings.some((spelled) =>
357
+ (word.directory ? spelled.directory : spelled.file).some((one) =>
358
+ mayStandFor(word.glob, one),
359
+ ),
360
+ ),
361
+ )
362
+ ) {
363
+ found.add(file);
364
+ }
365
+ }
366
+ }
367
+ return { files: [...found], unwritten };
68
368
  }
69
369
 
70
- /** A character a package's or a command's name may hold, so one beside a name makes it part of a longer one. */
71
- const NAME_CHARACTER = /[\w.@-]/u;
72
-
73
370
  /**
74
371
  * Each of `names`, a package's or a command's name, that `text` holds with no name character
75
372
  * before it and no character after it but one a path or a version may go on with, `/`, `.` or
@@ -137,13 +137,28 @@ function unseenLoadNotes(loads: readonly UnseenLoad[]): readonly ReportNote[] {
137
137
  /** One note per package whose command strings name files a configuration holds, naming every such file. */
138
138
  function heldScriptNotes(held: readonly HeldScripts[], targetRoot: string): readonly ReportNote[] {
139
139
  const files = new Map<string, Set<string>>();
140
+ const unwritten = new Set<string>();
140
141
  for (const one of held) {
141
142
  files.set(one.dir, new Set([...(files.get(one.dir) ?? []), ...one.files]));
143
+ if (one.unwritten) {
144
+ unwritten.add(one.dir);
145
+ }
142
146
  }
143
147
  return [...files].map(([dir, paths]) => ({
144
148
  kind: "unread-command",
145
149
  path: relativePath(targetRoot, dir) ?? ".",
146
- 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"}`,
150
+ message: [
151
+ ...(paths.size === 0
152
+ ? []
153
+ : [
154
+ `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"}`,
155
+ ]),
156
+ ...(unwritten.has(dir)
157
+ ? [
158
+ "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",
159
+ ]
160
+ : []),
161
+ ].join("; "),
147
162
  }));
148
163
  }
149
164