@codyswann/lisa 5.1.7 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/all/copy-overwrite/scripts/lib/github-attestation-provider.mjs +15 -0
  2. package/all/copy-overwrite/scripts/lib/npm-update-allocate.mjs +32 -39
  3. package/all/copy-overwrite/scripts/lib/npm-update-authorization.mjs +12 -2
  4. package/all/copy-overwrite/scripts/lib/npm-update-checkpoint.mjs +10 -30
  5. package/all/copy-overwrite/scripts/lib/npm-update-execution-adapter.mjs +4 -1
  6. package/all/copy-overwrite/scripts/lib/npm-update-helper-graph.mjs +1 -1
  7. package/all/copy-overwrite/scripts/lib/npm-update-helper-inventory.mjs +4 -0
  8. package/all/copy-overwrite/scripts/lib/npm-update-hosted-hook.mjs +2 -1
  9. package/all/copy-overwrite/scripts/lib/npm-update-invariants.mjs +15 -1
  10. package/all/copy-overwrite/scripts/lib/npm-update-leaf-contract.mjs +41 -9
  11. package/all/copy-overwrite/scripts/lib/npm-update-leaf.mjs +23 -35
  12. package/all/copy-overwrite/scripts/lib/npm-update-policy.mjs +41 -6
  13. package/all/copy-overwrite/scripts/lib/npm-update-recovery.mjs +16 -3
  14. package/all/copy-overwrite/scripts/lib/npm-update-tracker-jira.mjs +300 -0
  15. package/all/copy-overwrite/scripts/lib/npm-update-tracker-linear.mjs +304 -0
  16. package/all/copy-overwrite/scripts/lib/npm-update-tracker-wire.mjs +298 -0
  17. package/all/copy-overwrite/scripts/lib/npm-update-tracker.mjs +179 -0
  18. package/all/copy-overwrite/scripts/lisa-npm-updater.mjs +16 -2
  19. package/all/copy-overwrite/scripts/lisa-work-item.mjs +1 -1
  20. package/all/copy-overwrite/scripts/npm-updater-helper-graph.json +62 -14
  21. package/dist/core/lisa-owned-hash-ledger.d.ts.map +1 -1
  22. package/dist/core/lisa-owned-hash-ledger.js +96 -0
  23. package/dist/core/lisa-owned-hash-ledger.js.map +1 -1
  24. package/dist/core/nightly-e2e-guard-behavior-certificate.js +2 -2
  25. package/dist/core/upstream-evidence-manifest.d.ts.map +1 -1
  26. package/dist/core/upstream-evidence-manifest.js +84 -77
  27. package/dist/core/upstream-evidence-manifest.js.map +1 -1
  28. package/package.json +4 -5
  29. package/plugins/lisa/.claude-plugin/plugin.json +1 -1
  30. package/plugins/lisa/.codex-plugin/plugin.json +1 -1
  31. package/plugins/lisa-agy/plugin.json +1 -1
  32. package/plugins/lisa-cdk/.claude-plugin/plugin.json +1 -1
  33. package/plugins/lisa-cdk/.codex-plugin/plugin.json +1 -1
  34. package/plugins/lisa-cdk-agy/plugin.json +1 -1
  35. package/plugins/lisa-cdk-copilot/.claude-plugin/plugin.json +1 -1
  36. package/plugins/lisa-cdk-cursor/.claude-plugin/plugin.json +1 -1
  37. package/plugins/lisa-cdk-devin/.claude-plugin/plugin.json +1 -1
  38. package/plugins/lisa-cdk-devin/.devin-plugin/plugin.json +1 -1
  39. package/plugins/lisa-copilot/.claude-plugin/plugin.json +1 -1
  40. package/plugins/lisa-cursor/.claude-plugin/plugin.json +1 -1
  41. package/plugins/lisa-devin/.claude-plugin/plugin.json +1 -1
  42. package/plugins/lisa-devin/.devin-plugin/plugin.json +1 -1
  43. package/plugins/lisa-expo/.claude-plugin/plugin.json +1 -1
  44. package/plugins/lisa-expo/.codex-plugin/plugin.json +1 -1
  45. package/plugins/lisa-expo-agy/plugin.json +1 -1
  46. package/plugins/lisa-expo-copilot/.claude-plugin/plugin.json +1 -1
  47. package/plugins/lisa-expo-cursor/.claude-plugin/plugin.json +1 -1
  48. package/plugins/lisa-expo-devin/.claude-plugin/plugin.json +1 -1
  49. package/plugins/lisa-expo-devin/.devin-plugin/plugin.json +1 -1
  50. package/plugins/lisa-harper-fabric/.claude-plugin/plugin.json +1 -1
  51. package/plugins/lisa-harper-fabric/.codex-plugin/plugin.json +1 -1
  52. package/plugins/lisa-harper-fabric-agy/plugin.json +1 -1
  53. package/plugins/lisa-harper-fabric-copilot/.claude-plugin/plugin.json +1 -1
  54. package/plugins/lisa-harper-fabric-cursor/.claude-plugin/plugin.json +1 -1
  55. package/plugins/lisa-harper-fabric-devin/.claude-plugin/plugin.json +1 -1
  56. package/plugins/lisa-harper-fabric-devin/.devin-plugin/plugin.json +1 -1
  57. package/plugins/lisa-nestjs/.claude-plugin/plugin.json +1 -1
  58. package/plugins/lisa-nestjs/.codex-plugin/plugin.json +1 -1
  59. package/plugins/lisa-nestjs-agy/plugin.json +1 -1
  60. package/plugins/lisa-nestjs-copilot/.claude-plugin/plugin.json +1 -1
  61. package/plugins/lisa-nestjs-cursor/.claude-plugin/plugin.json +1 -1
  62. package/plugins/lisa-nestjs-devin/.claude-plugin/plugin.json +1 -1
  63. package/plugins/lisa-nestjs-devin/.devin-plugin/plugin.json +1 -1
  64. package/plugins/lisa-phaser/.claude-plugin/plugin.json +1 -1
  65. package/plugins/lisa-phaser/.codex-plugin/plugin.json +1 -1
  66. package/plugins/lisa-phaser-agy/plugin.json +1 -1
  67. package/plugins/lisa-phaser-copilot/.claude-plugin/plugin.json +1 -1
  68. package/plugins/lisa-phaser-cursor/.claude-plugin/plugin.json +1 -1
  69. package/plugins/lisa-phaser-devin/.claude-plugin/plugin.json +1 -1
  70. package/plugins/lisa-phaser-devin/.devin-plugin/plugin.json +1 -1
  71. package/plugins/lisa-rails/.claude-plugin/plugin.json +1 -1
  72. package/plugins/lisa-rails/.codex-plugin/plugin.json +1 -1
  73. package/plugins/lisa-rails-agy/plugin.json +1 -1
  74. package/plugins/lisa-rails-copilot/.claude-plugin/plugin.json +1 -1
  75. package/plugins/lisa-rails-cursor/.claude-plugin/plugin.json +1 -1
  76. package/plugins/lisa-rails-devin/.claude-plugin/plugin.json +1 -1
  77. package/plugins/lisa-rails-devin/.devin-plugin/plugin.json +1 -1
  78. package/plugins/lisa-typescript/.claude-plugin/plugin.json +1 -1
  79. package/plugins/lisa-typescript/.codex-plugin/plugin.json +1 -1
  80. package/plugins/lisa-typescript-agy/plugin.json +1 -1
  81. package/plugins/lisa-typescript-copilot/.claude-plugin/plugin.json +1 -1
  82. package/plugins/lisa-typescript-cursor/.claude-plugin/plugin.json +1 -1
  83. package/plugins/lisa-typescript-devin/.claude-plugin/plugin.json +1 -1
  84. package/plugins/lisa-typescript-devin/.devin-plugin/plugin.json +1 -1
  85. package/plugins/lisa-wiki/.claude-plugin/plugin.json +1 -1
  86. package/plugins/lisa-wiki/.codex-plugin/plugin.json +1 -1
  87. package/plugins/lisa-wiki-agy/plugin.json +1 -1
  88. package/plugins/lisa-wiki-copilot/.claude-plugin/plugin.json +1 -1
  89. package/plugins/lisa-wiki-cursor/.claude-plugin/plugin.json +1 -1
  90. package/plugins/lisa-wiki-devin/.claude-plugin/plugin.json +1 -1
  91. package/plugins/lisa-wiki-devin/.devin-plugin/plugin.json +1 -1
  92. package/scripts/check-phrase-manifest-parity.mjs +6 -3
  93. package/scripts/classify-phrase-tests.mjs +109 -20
  94. package/scripts/fix-namespace-test-assertions.mjs +3 -20
  95. package/scripts/lib/phrase-tests/doc-taint.mjs +708 -0
  96. package/scripts/lib/phrase-tests/inventory.mjs +381 -0
  97. package/scripts/lib/phrase-tests/manifest.mjs +14 -2
  98. package/scripts/lib/phrase-tests/marker-classes.mjs +323 -0
  99. package/scripts/lib/phrase-tests/redact.mjs +88 -0
  100. package/scripts/two-channel-couplings.json +1 -1
  101. package/typescript/copy-overwrite/.prettierignore +4 -0
@@ -0,0 +1,708 @@
1
+ /**
2
+ * Which expressions in a test file hold Markdown document text
3
+ * (CodySwannGT/lisa#4867).
4
+ *
5
+ * A file that reads a skill document often also runs a script and asserts on
6
+ * its output; only the first kind of assertion pins document wording. This is
7
+ * a small taint analysis over one file, iterated to a fixed point:
8
+ *
9
+ * - a PATH expression names a repository document: it carries both a
10
+ * Markdown extension (`.md` / `.mdc`) and a document root (`plugins`,
11
+ * `skills`, `rules`, `.claude`, `wiki`, `README.md` ...), spelled as
12
+ * literals or held by bindings (including a loop variable or callback
13
+ * parameter fed by a path list, as in `for (const file of SKILLS)` or
14
+ * `it.each(SKILLS)`). Requiring the root keeps out Markdown a script under
15
+ * test writes, such as a `summary.md` in a temporary directory;
16
+ * - a DOC expression is a file read (`readFileSync` / `readFile`) of a PATH,
17
+ * a call to a local function that does such a read, or names a binding
18
+ * that holds a DOC (so `const section = sliceSection(doc, "x")` is DOC).
19
+ * Taint does NOT flow through a call that runs code: a spawn or exec, a
20
+ * function imported from a module other than a Node built-in or the test
21
+ * runner, or a local function whose body makes such a call. Running a
22
+ * document's shell block and asserting on its stdout tests behaviour, not
23
+ * wording, so `result = runBlock(docBlock)` is not DOC.
24
+ *
25
+ * Bindings are tracked by their TypeScript symbol, not their name, so a
26
+ * `summary` holding script output in one test and a `summary` holding a
27
+ * document in another stay apart.
28
+ *
29
+ * It is a heuristic and errs toward "markdown": a subject that mentions
30
+ * document text in any way counts. A subject rooted in a call to an imported
31
+ * helper with no Markdown path in sight is "unknown", never "other", because
32
+ * the helper may read a document this analysis cannot see.
33
+ * @module scripts/lib/phrase-tests/doc-taint
34
+ */
35
+ import path from "node:path";
36
+
37
+ import ts from "typescript";
38
+
39
+ import { walk } from "./ast.mjs";
40
+
41
+ /** A literal carrying a Markdown extension. */
42
+ export const MARKDOWN_PATH = /\.mdc?\b/u;
43
+
44
+ /** Path segments naming a directory where Lisa keeps its documents. */
45
+ const DOCUMENT_DIRECTORIES = new Set([
46
+ "plugins",
47
+ "skills",
48
+ "rules",
49
+ "agents",
50
+ "commands",
51
+ "reference",
52
+ ".claude",
53
+ ".agents",
54
+ ".cursor",
55
+ ".codex",
56
+ ".github",
57
+ "wiki",
58
+ "docs",
59
+ "all",
60
+ "typescript",
61
+ "expo",
62
+ "nestjs",
63
+ "cdk",
64
+ "rails",
65
+ "npm-package",
66
+ "base",
67
+ ]);
68
+
69
+ /** File names that are documents wherever they live. */
70
+ const DOCUMENT_FILE = /^(?:SKILL|README|AGENTS|CLAUDE|GEMINI)\.md$/u;
71
+
72
+ /**
73
+ * A literal naming a directory or file where Lisa keeps its documents. It has
74
+ * the `test` shape of a RegExp so it can stand where one does.
75
+ */
76
+ export const DOCUMENT_ROOT = Object.freeze({
77
+ /**
78
+ * @param {string} text - A literal.
79
+ * @returns {boolean} True when a segment is a document directory or file.
80
+ */
81
+ test: text =>
82
+ text
83
+ .split("/")
84
+ .some(
85
+ segment =>
86
+ DOCUMENT_DIRECTORIES.has(segment) || DOCUMENT_FILE.test(segment)
87
+ ),
88
+ });
89
+
90
+ /** Calls that read a file's text. */
91
+ const READ_CALL_NAMES = new Set(["readFileSync", "readFile"]);
92
+
93
+ /** Calls that run a process. */
94
+ const RUN_CALL_NAMES = new Set([
95
+ "spawn",
96
+ "spawnSync",
97
+ "exec",
98
+ "execSync",
99
+ "execFile",
100
+ "execFileSync",
101
+ "boundedSpawnSync",
102
+ "execa",
103
+ ]);
104
+
105
+ /**
106
+ * Calls whose result is still a path (or a list of paths) built from their
107
+ * arguments. A path part does not survive any other call: the result of
108
+ * `readJob(".github/workflows/x.yml")` is file text, not a path.
109
+ */
110
+ const PATH_CALL_NAMES = new Set([
111
+ "join",
112
+ "resolve",
113
+ "normalize",
114
+ "relative",
115
+ "dirname",
116
+ "basename",
117
+ "filter",
118
+ "map",
119
+ "flatMap",
120
+ "concat",
121
+ "slice",
122
+ "from",
123
+ "of",
124
+ "readdirSync",
125
+ "globSync",
126
+ "sync",
127
+ "replace",
128
+ "each",
129
+ ]);
130
+
131
+ /** Modules whose imports are plumbing, not code under test. */
132
+ const PLUMBING_MODULE =
133
+ /^(?:node:.*|fs|fs-extra|fs\/promises|path|url|os|vitest|yaml|js-yaml)$/u;
134
+
135
+ /** Fixed-point iteration cap; real files settle in two or three passes. */
136
+ const MAX_PASSES = 12;
137
+
138
+ /**
139
+ * The text of a string literal or template fragment, else undefined.
140
+ * @param {ts.Node} node - Any node.
141
+ * @returns {string | undefined} The literal text.
142
+ */
143
+ function literalText(node) {
144
+ const literal =
145
+ ts.isStringLiteralLike(node) ||
146
+ ts.isTemplateHead(node) ||
147
+ ts.isTemplateMiddle(node) ||
148
+ ts.isTemplateTail(node);
149
+ return literal ? node.text : undefined;
150
+ }
151
+
152
+ /**
153
+ * The name a call invokes: `f(...)` → f, `fs.readFileSync(...)` → readFileSync.
154
+ * @param {ts.CallExpression} call - A call.
155
+ * @returns {string | undefined} The callee's final name.
156
+ */
157
+ function calleeName(call) {
158
+ const callee = call.expression;
159
+ if (ts.isIdentifier(callee)) return callee.text;
160
+ if (ts.isPropertyAccessExpression(callee)) return callee.name.text;
161
+ return undefined;
162
+ }
163
+
164
+ /**
165
+ * Every identifier a binding name introduces (destructuring included).
166
+ * @param {ts.BindingName} name - A binding name.
167
+ * @returns {ts.Identifier[]} The bound identifiers.
168
+ */
169
+ function boundNames(name) {
170
+ if (ts.isIdentifier(name)) return [name];
171
+ return name.elements.flatMap(element =>
172
+ ts.isOmittedExpression(element) ? [] : boundNames(element.name)
173
+ );
174
+ }
175
+
176
+ /**
177
+ * @typedef {object} Taint
178
+ * @property {(identifier: ts.Identifier) => unknown} key - The binding an
179
+ * identifier refers to: its symbol, or its text when it has none.
180
+ * @property {Set<unknown>} md - Bindings holding a Markdown extension.
181
+ * @property {Set<unknown>} roots - Bindings holding a document root.
182
+ * @property {Set<unknown>} docs - Bindings holding Markdown text.
183
+ * @property {Set<string>} readers - Local functions that read a file.
184
+ * @property {Set<string>} imported - Names imported from other modules.
185
+ * @property {Set<string>} codeImports - Names imported from modules that are
186
+ * not Node built-ins, the test runner or a YAML parser.
187
+ * @property {Set<string>} executors - Local functions that run code.
188
+ * @property {Set<string>} pathFns - `<name>:md` / `<name>:root` for local
189
+ * functions that return a path carrying that part.
190
+ * @property {Set<unknown>} exclude - Bindings ignored while summarising a
191
+ * function: its own parameters.
192
+ * @property {Map<string, readonly ts.ParameterDeclaration[]>} functions -
193
+ * Local named functions and their parameters.
194
+ */
195
+
196
+ /**
197
+ * Whether any node under `root` satisfies `test`.
198
+ * @param {ts.Node} root - Where to look.
199
+ * @param {(node: ts.Node) => boolean} test - The predicate.
200
+ * @returns {boolean} True on the first match.
201
+ */
202
+ function some(root, test) {
203
+ let found = false;
204
+ walk(root, node => {
205
+ if (!found && test(node)) found = true;
206
+ });
207
+ return found;
208
+ }
209
+
210
+ /**
211
+ * Whether an expression spells or names a literal matching `pattern`, or a
212
+ * binding in `names`.
213
+ * @param {ts.Node} node - An expression.
214
+ * @param {RegExp} pattern - The literal test.
215
+ * @param {Set<unknown>} names - Tainted bindings.
216
+ * @param {Taint} taint - Supplies the binding key.
217
+ * @returns {boolean} True on a match.
218
+ */
219
+ function carries(node, pattern, names, taint) {
220
+ if (ts.isIdentifier(node)) return held(names, node, taint);
221
+ const text = literalText(node);
222
+ if (text !== undefined) return pattern.test(text);
223
+ if (ts.isCallExpression(node)) {
224
+ const name = calleeName(node) ?? "";
225
+ const kind = names === taint.md ? "md" : "root";
226
+ if (taint.pathFns.has(`${name}:${kind}`)) return true;
227
+ if (!PATH_CALL_NAMES.has(name)) return false;
228
+ }
229
+ return (
230
+ ts.forEachChild(
231
+ node,
232
+ child => carries(child, pattern, names, taint) || undefined
233
+ ) === true
234
+ );
235
+ }
236
+
237
+ /**
238
+ * Whether a binding is in a taint set, ignoring the parameters of the
239
+ * function being summarised (a summary must not depend on one caller).
240
+ * @param {Set<unknown>} names - The taint set.
241
+ * @param {ts.Identifier} identifier - A reference.
242
+ * @param {Taint} taint - Supplies the key and the exclusions.
243
+ * @returns {boolean} True when the binding is tainted.
244
+ */
245
+ function held(names, identifier, taint) {
246
+ const key = taint.key(identifier);
247
+ return names.has(key) && !taint.exclude.has(key);
248
+ }
249
+
250
+ /**
251
+ * Whether an expression carries a Markdown extension.
252
+ * @param {ts.Node} node - An expression.
253
+ * @param {Taint} taint - What is known so far.
254
+ * @returns {boolean} True when it does.
255
+ */
256
+ function hasMarkdown(node, taint) {
257
+ return carries(node, MARKDOWN_PATH, taint.md, taint);
258
+ }
259
+
260
+ /**
261
+ * Whether an expression carries a document root.
262
+ * @param {ts.Node} node - An expression.
263
+ * @param {Taint} taint - What is known so far.
264
+ * @returns {boolean} True when it does.
265
+ */
266
+ function hasRoot(node, taint) {
267
+ return carries(node, DOCUMENT_ROOT, taint.roots, taint);
268
+ }
269
+
270
+ /**
271
+ * Whether a file spells a Markdown extension and a document root anywhere in
272
+ * its string and template literals.
273
+ * @param {ts.SourceFile} sourceFile - The parsed test file.
274
+ * @returns {boolean} True when it may name a repository document.
275
+ */
276
+ export function mentionsDocumentPath(sourceFile) {
277
+ const spells = (/** @type {RegExp} */ pattern) =>
278
+ some(sourceFile, node => {
279
+ const text = literalText(node);
280
+ return text !== undefined && pattern.test(text);
281
+ });
282
+ return spells(MARKDOWN_PATH) && spells(DOCUMENT_ROOT);
283
+ }
284
+
285
+ /**
286
+ * Whether an expression names a repository Markdown document.
287
+ * @param {ts.Node} node - An expression.
288
+ * @param {Taint} taint - What is known so far.
289
+ * @returns {boolean} True for a PATH expression.
290
+ */
291
+ export function isPathy(node, taint) {
292
+ return hasMarkdown(node, taint) && hasRoot(node, taint);
293
+ }
294
+
295
+ /**
296
+ * Whether an expression holds Markdown text.
297
+ * @param {ts.Node} node - An expression.
298
+ * @param {Taint} taint - What is known so far.
299
+ * @returns {boolean} True for a DOC expression.
300
+ */
301
+ export function isDocy(node, taint) {
302
+ if (ts.isIdentifier(node)) return held(taint.docs, node, taint);
303
+ if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) return false;
304
+ if (ts.isCallExpression(node)) {
305
+ const name = calleeName(node) ?? "";
306
+ if (READ_CALL_NAMES.has(name)) return argumentsNameDocument(node, taint);
307
+ if (runsCode(name, taint)) return false;
308
+ if (taint.readers.has(`${name}:doc`)) return true;
309
+ if (taint.readers.has(name) && readsDocument(name, node, taint))
310
+ return true;
311
+ }
312
+ return (
313
+ ts.forEachChild(node, child => isDocy(child, taint) || undefined) === true
314
+ );
315
+ }
316
+
317
+ /**
318
+ * Whether a call to reader `name` reads a document: the reader's body and the
319
+ * call's arguments between them supply both a Markdown extension and a
320
+ * document root, as in `readSkill(root, "x")` with the body adding
321
+ * `SKILL.md` and the argument the `plugins/...` root.
322
+ * @param {string} name - The reader.
323
+ * @param {ts.CallExpression} call - The call.
324
+ * @param {Taint} taint - What is known so far.
325
+ * @returns {boolean} True when the call reads a document.
326
+ */
327
+ function readsDocument(name, call, taint) {
328
+ return (
329
+ (taint.readers.has(`${name}:md`) ||
330
+ call.arguments.some(argument => hasMarkdown(argument, taint))) &&
331
+ (taint.readers.has(`${name}:root`) ||
332
+ call.arguments.some(argument => hasRoot(argument, taint)))
333
+ );
334
+ }
335
+
336
+ /**
337
+ * Whether a file-read call's arguments name a repository document.
338
+ * @param {ts.CallExpression} call - A `readFileSync` / `readFile` call.
339
+ * @param {Taint} taint - What is known so far.
340
+ * @returns {boolean} True when it reads a document.
341
+ */
342
+ function argumentsNameDocument(call, taint) {
343
+ return (
344
+ call.arguments.some(argument => hasMarkdown(argument, taint)) &&
345
+ call.arguments.some(argument => hasRoot(argument, taint))
346
+ );
347
+ }
348
+
349
+ /**
350
+ * Whether calling `name` runs code rather than transforming text.
351
+ * @param {string} name - A callee name.
352
+ * @param {Taint} taint - What is known so far.
353
+ * @returns {boolean} True for a spawn, an imported function or an executor.
354
+ */
355
+ function runsCode(name, taint) {
356
+ return (
357
+ RUN_CALL_NAMES.has(name) ||
358
+ taint.codeImports.has(name) ||
359
+ taint.executors.has(name)
360
+ );
361
+ }
362
+
363
+ /**
364
+ * Record what one binding holds; returns whether anything new was learned.
365
+ * @param {ts.Identifier[]} names - The bound identifiers.
366
+ * @param {ts.Node} source - What they are bound to.
367
+ * @param {Taint} taint - Updated in place.
368
+ * @returns {boolean} True when the taint grew.
369
+ */
370
+ function bind(names, source, taint) {
371
+ /** @type {Set<unknown>[]} */
372
+ const targets = isDocy(source, taint)
373
+ ? [taint.docs]
374
+ : [
375
+ ...(hasMarkdown(source, taint) ? [taint.md] : []),
376
+ ...(hasRoot(source, taint) ? [taint.roots] : []),
377
+ ];
378
+ let grew = false;
379
+ for (const target of targets) {
380
+ for (const key of names.map(taint.key).filter(key => !target.has(key))) {
381
+ target.add(key);
382
+ grew = true;
383
+ }
384
+ }
385
+ return grew;
386
+ }
387
+
388
+ /**
389
+ * The function value a declaration binds, for reader detection.
390
+ * @param {ts.Node} node - Any node.
391
+ * @returns {{ name: string, body: ts.Node } | undefined} The named function.
392
+ */
393
+ function namedFunction(node) {
394
+ if (ts.isFunctionDeclaration(node) && node.name && node.body) {
395
+ return {
396
+ name: node.name.text,
397
+ body: node.body,
398
+ parameters: node.parameters,
399
+ };
400
+ }
401
+ if (
402
+ ts.isVariableDeclaration(node) &&
403
+ ts.isIdentifier(node.name) &&
404
+ node.initializer &&
405
+ (ts.isArrowFunction(node.initializer) ||
406
+ ts.isFunctionExpression(node.initializer))
407
+ ) {
408
+ return {
409
+ name: node.name.text,
410
+ body: node.initializer.body,
411
+ parameters: node.initializer.parameters,
412
+ };
413
+ }
414
+ return undefined;
415
+ }
416
+
417
+ /**
418
+ * Parameters of a local function fed by one call to it, so an assertion
419
+ * helper such as `expectGate(section)` sees what its callers pass.
420
+ * @param {ts.Node} node - Any node.
421
+ * @param {Taint} taint - Supplies the local functions.
422
+ * @returns {{ names: ts.Identifier[], source: ts.Node }[]} One binding per
423
+ * argument.
424
+ */
425
+ function argumentBindings(node, taint) {
426
+ if (!ts.isCallExpression(node) || !ts.isIdentifier(node.expression)) {
427
+ return [];
428
+ }
429
+ const parameters = taint.functions.get(node.expression.text) ?? [];
430
+ return node.arguments.flatMap((argument, index) => {
431
+ const parameter = parameters[index];
432
+ return parameter
433
+ ? [{ names: boundNames(parameter.name), source: argument }]
434
+ : [];
435
+ });
436
+ }
437
+
438
+ /**
439
+ * Callback parameters fed by the call they are passed to: `it.each(X)(t, fn)`
440
+ * and `X.forEach(fn)` / `X.map(fn)` bind fn's parameters to X's elements.
441
+ * @param {ts.Node} node - Any node.
442
+ * @returns {{ names: ts.Identifier[], source: ts.Node } | undefined} The binding.
443
+ */
444
+ function callbackBinding(node) {
445
+ if (!(ts.isArrowFunction(node) || ts.isFunctionExpression(node))) {
446
+ return undefined;
447
+ }
448
+ const call = node.parent;
449
+ if (!ts.isCallExpression(call) || !call.arguments.includes(node)) {
450
+ return undefined;
451
+ }
452
+ const source = ts.isPropertyAccessExpression(call.expression)
453
+ ? call.expression.expression
454
+ : call.expression;
455
+ return {
456
+ names: node.parameters.flatMap(parameter => boundNames(parameter.name)),
457
+ source,
458
+ };
459
+ }
460
+
461
+ /**
462
+ * Add `key` to `set` when `condition` holds and it is new.
463
+ * @param {Set<string>} set - The set.
464
+ * @param {string} key - The key.
465
+ * @param {() => boolean} condition - Evaluated only when the key is new.
466
+ * @returns {boolean} True when the set grew.
467
+ */
468
+ function learn(set, key, condition) {
469
+ if (set.has(key) || !condition()) return false;
470
+ set.add(key);
471
+ return true;
472
+ }
473
+
474
+ /**
475
+ * The file-read and reader calls inside a function body.
476
+ * @param {ts.Node} body - The body.
477
+ * @param {Taint} taint - Supplies the known readers.
478
+ * @returns {ts.CallExpression[]} The calls.
479
+ */
480
+ function readCallsIn(body, taint) {
481
+ /** @type {ts.CallExpression[]} */
482
+ const calls = [];
483
+ walk(body, node => {
484
+ if (!ts.isCallExpression(node)) return;
485
+ const name = calleeName(node) ?? "";
486
+ if (READ_CALL_NAMES.has(name) || taint.readers.has(name)) calls.push(node);
487
+ });
488
+ return calls;
489
+ }
490
+
491
+ /**
492
+ * What one local function is: a reader (reads a file, directly or through
493
+ * another reader), an executor (runs code), and whether calling it yields a
494
+ * document's text (it reads a document path, or returns a slice of a DOC).
495
+ * @param {{ name: string, body: ts.Node }} fn - The function.
496
+ * @param {Taint} taint - Updated in place.
497
+ * @returns {boolean} True when the taint grew.
498
+ */
499
+ function learnFunction(fn, taint) {
500
+ taint.exclude = new Set(
501
+ fn.parameters.flatMap(parameter =>
502
+ boundNames(parameter.name).map(taint.key)
503
+ )
504
+ );
505
+ try {
506
+ return summarise(fn, taint);
507
+ } finally {
508
+ taint.exclude = new Set();
509
+ }
510
+ }
511
+
512
+ /**
513
+ * The body of `learnFunction`, run with the function's parameters excluded.
514
+ * @param {{ name: string, body: ts.Node, parameters: readonly ts.ParameterDeclaration[] }} fn - The function.
515
+ * @param {Taint} taint - Updated in place.
516
+ * @returns {boolean} True when the taint grew.
517
+ */
518
+ function summarise(fn, taint) {
519
+ const callsAny = (/** @type {(name: string) => boolean} */ test) =>
520
+ some(
521
+ fn.body,
522
+ child => ts.isCallExpression(child) && test(calleeName(child) ?? "")
523
+ );
524
+ const reader = learn(taint.readers, fn.name, () =>
525
+ callsAny(name => READ_CALL_NAMES.has(name) || taint.readers.has(name))
526
+ );
527
+ const executor = learn(taint.executors, fn.name, () =>
528
+ callsAny(name => runsCode(name, taint))
529
+ );
530
+ const reads = () => readCallsIn(fn.body, taint);
531
+ const md = learn(taint.readers, `${fn.name}:md`, () =>
532
+ reads().some(call => call.arguments.some(arg => hasMarkdown(arg, taint)))
533
+ );
534
+ const root = learn(taint.readers, `${fn.name}:root`, () =>
535
+ reads().some(call => call.arguments.some(arg => hasRoot(arg, taint)))
536
+ );
537
+ const doc = learn(
538
+ taint.readers,
539
+ `${fn.name}:doc`,
540
+ () => !taint.executors.has(fn.name) && isDocy(fn.body, taint)
541
+ );
542
+ const pathMd = learn(
543
+ taint.pathFns,
544
+ `${fn.name}:md`,
545
+ () => !taint.readers.has(fn.name) && hasMarkdown(fn.body, taint)
546
+ );
547
+ const pathRoot = learn(
548
+ taint.pathFns,
549
+ `${fn.name}:root`,
550
+ () => !taint.readers.has(fn.name) && hasRoot(fn.body, taint)
551
+ );
552
+ return reader || executor || md || root || doc || pathMd || pathRoot;
553
+ }
554
+
555
+ /**
556
+ * The bindings a loop header introduces, with what they iterate.
557
+ * @param {ts.Node} node - Any node.
558
+ * @returns {{ names: ts.Identifier[], source: ts.Node } | undefined} The binding.
559
+ */
560
+ function loopBinding(node) {
561
+ if (!(ts.isForOfStatement(node) || ts.isForInStatement(node))) {
562
+ return undefined;
563
+ }
564
+ if (!ts.isVariableDeclarationList(node.initializer)) return undefined;
565
+ return {
566
+ names: node.initializer.declarations.flatMap(declaration =>
567
+ boundNames(declaration.name)
568
+ ),
569
+ source: node.expression,
570
+ };
571
+ }
572
+
573
+ /**
574
+ * One pass over the file; returns whether the taint grew.
575
+ * @param {ts.SourceFile} sourceFile - The parsed test file.
576
+ * @param {Taint} taint - Updated in place.
577
+ * @returns {boolean} True when anything new was learned.
578
+ */
579
+ function pass(sourceFile, taint) {
580
+ let grew = false;
581
+ walk(sourceFile, node => {
582
+ const fn = namedFunction(node);
583
+ if (fn) grew = learnFunction(fn, taint) || grew;
584
+ const binding =
585
+ ts.isVariableDeclaration(node) && node.initializer
586
+ ? { names: boundNames(node.name), source: node.initializer }
587
+ : (loopBinding(node) ?? callbackBinding(node));
588
+ if (binding) grew = bind(binding.names, binding.source, taint) || grew;
589
+ for (const argument of argumentBindings(node, taint)) {
590
+ grew = bind(argument.names, argument.source, taint) || grew;
591
+ }
592
+ });
593
+ return grew;
594
+ }
595
+
596
+ /**
597
+ * Every local named function and its parameters.
598
+ * @param {ts.SourceFile} sourceFile - The parsed test file.
599
+ * @returns {Map<string, readonly ts.ParameterDeclaration[]>} By name.
600
+ */
601
+ function localFunctions(sourceFile) {
602
+ /** @type {Map<string, readonly ts.ParameterDeclaration[]>} */
603
+ const functions = new Map();
604
+ walk(sourceFile, node => {
605
+ const fn = namedFunction(node);
606
+ if (fn) functions.set(fn.name, fn.parameters);
607
+ });
608
+ return functions;
609
+ }
610
+
611
+ /**
612
+ * Whether an import is plumbing rather than code under test: a Node built-in,
613
+ * the test runner, a YAML parser, or a helper that lives under `tests/`.
614
+ * @param {string} fileName - Repository-relative importing file.
615
+ * @param {string} specifier - The module specifier.
616
+ * @returns {boolean} True for plumbing.
617
+ */
618
+ function isPlumbing(fileName, specifier) {
619
+ if (PLUMBING_MODULE.test(specifier)) return true;
620
+ if (!specifier.startsWith(".")) return false;
621
+ return path.posix
622
+ .normalize(path.posix.join(path.posix.dirname(fileName), specifier))
623
+ .startsWith("tests/");
624
+ }
625
+
626
+ /**
627
+ * Names imported into the file.
628
+ * @param {ts.SourceFile} sourceFile - The parsed test file.
629
+ * @param {boolean} codeOnly - Only names from modules that are not plumbing.
630
+ * @returns {Set<string>} Imported local names.
631
+ */
632
+ function importedNames(sourceFile, codeOnly) {
633
+ /** @type {Set<string>} */
634
+ const names = new Set();
635
+ for (const statement of sourceFile.statements) {
636
+ if (!ts.isImportDeclaration(statement)) continue;
637
+ const specifier = ts.isStringLiteral(statement.moduleSpecifier)
638
+ ? statement.moduleSpecifier.text
639
+ : "";
640
+ if (codeOnly && isPlumbing(sourceFile.fileName, specifier)) continue;
641
+ const clause = statement.importClause;
642
+ if (clause?.name) names.add(clause.name.text);
643
+ const bindings = clause?.namedBindings;
644
+ if (bindings && ts.isNamedImports(bindings)) {
645
+ for (const element of bindings.elements) names.add(element.name.text);
646
+ }
647
+ }
648
+ return names;
649
+ }
650
+
651
+ /**
652
+ * A type checker over this one file alone (no lib, no imports resolved), used
653
+ * only to map identifiers to the declarations they refer to.
654
+ * @param {ts.SourceFile} sourceFile - The parsed test file.
655
+ * @returns {ts.TypeChecker} The checker.
656
+ */
657
+ function bindingChecker(sourceFile) {
658
+ const options = { noLib: true, noResolve: true, allowJs: true };
659
+ const host = ts.createCompilerHost(options);
660
+ host.getSourceFile = name =>
661
+ name === sourceFile.fileName ? sourceFile : undefined;
662
+ return ts
663
+ .createProgram({ rootNames: [sourceFile.fileName], options, host })
664
+ .getTypeChecker();
665
+ }
666
+
667
+ /**
668
+ * Analyse a file once; the result answers `subjectSource` for any subject.
669
+ * @param {ts.SourceFile} sourceFile - The parsed test file.
670
+ * @returns {Taint} What holds Markdown paths and text.
671
+ */
672
+ export function analyseDocTaint(sourceFile) {
673
+ const checker = bindingChecker(sourceFile);
674
+ /** @type {Taint} */
675
+ const taint = {
676
+ key: identifier =>
677
+ checker.getSymbolAtLocation(identifier) ?? identifier.text,
678
+ md: new Set(),
679
+ roots: new Set(),
680
+ docs: new Set(),
681
+ readers: new Set(),
682
+ imported: importedNames(sourceFile, false),
683
+ codeImports: importedNames(sourceFile, true),
684
+ executors: new Set(),
685
+ pathFns: new Set(),
686
+ exclude: new Set(),
687
+ functions: localFunctions(sourceFile),
688
+ };
689
+ for (let index = 0; index < MAX_PASSES && pass(sourceFile, taint); index++);
690
+ return taint;
691
+ }
692
+
693
+ /**
694
+ * Where an assertion subject's text comes from.
695
+ * @param {ts.Expression | undefined} subject - The `expect(...)` argument.
696
+ * @param {Taint} taint - The file's analysis.
697
+ * @returns {"markdown" | "other" | "unknown"} The subject's source.
698
+ */
699
+ export function subjectSource(subject, taint) {
700
+ if (subject === undefined) return "other";
701
+ if (isDocy(subject, taint)) return "markdown";
702
+ const viaImport = some(
703
+ subject,
704
+ node =>
705
+ ts.isCallExpression(node) && taint.imported.has(calleeName(node) ?? "")
706
+ );
707
+ return viaImport ? "unknown" : "other";
708
+ }