@stateward/core 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/dist/analyses.d.ts +37 -0
  2. package/dist/analyses.js +57 -0
  3. package/dist/backend/program-record.d.ts +7 -0
  4. package/dist/backend/typescript6.d.ts +33 -32
  5. package/dist/backend/typescript6.js +1438 -139
  6. package/dist/baseline.d.ts +1 -1
  7. package/dist/baseline.js +14 -10
  8. package/dist/cache.d.ts +74 -4
  9. package/dist/cache.js +211 -37
  10. package/dist/checker.d.ts +6 -1
  11. package/dist/checker.js +10 -0
  12. package/dist/class-id.d.ts +34 -0
  13. package/dist/class-id.js +93 -0
  14. package/dist/code-text.d.ts +8 -0
  15. package/dist/code-text.js +66 -0
  16. package/dist/config-file.js +7 -5
  17. package/dist/config.d.ts +10 -2
  18. package/dist/config.js +490 -48
  19. package/dist/define.d.ts +8 -2
  20. package/dist/disk-cache.d.ts +2 -0
  21. package/dist/disk-cache.js +77 -11
  22. package/dist/engine.d.ts +33 -1
  23. package/dist/engine.js +344 -24
  24. package/dist/fact-lines.d.ts +31 -0
  25. package/dist/fact-lines.js +197 -0
  26. package/dist/facts-versions.d.ts +27 -0
  27. package/dist/facts-versions.js +99 -0
  28. package/dist/fixes.js +4 -1
  29. package/dist/ignores.d.ts +11 -0
  30. package/dist/ignores.js +74 -27
  31. package/dist/implementation.d.ts +0 -6
  32. package/dist/implementation.js +0 -17
  33. package/dist/index.d.ts +8 -9
  34. package/dist/index.js +10 -7
  35. package/dist/internal.d.ts +7 -4
  36. package/dist/internal.js +5 -3
  37. package/dist/lazy.d.ts +8 -1
  38. package/dist/lazy.js +27 -2
  39. package/dist/ledger.d.ts +2 -2
  40. package/dist/ledger.js +2 -2
  41. package/dist/off-by-default.d.ts +13 -0
  42. package/dist/off-by-default.js +15 -0
  43. package/dist/options.d.ts +21 -5
  44. package/dist/options.js +22 -7
  45. package/dist/package-facts.d.ts +211 -17
  46. package/dist/package-facts.js +520 -38
  47. package/dist/plan.d.ts +1 -1
  48. package/dist/plan.js +7 -2
  49. package/dist/release.d.ts +12 -21
  50. package/dist/release.js +26 -38
  51. package/dist/renamed.d.ts +22 -3
  52. package/dist/renamed.js +50 -16
  53. package/dist/rollout.js +7 -4
  54. package/dist/severity.d.ts +1 -1
  55. package/dist/severity.js +17 -3
  56. package/dist/test-runner.d.ts +18 -0
  57. package/dist/test-runner.js +483 -0
  58. package/dist/testing.d.ts +3 -3
  59. package/dist/testing.js +5 -5
  60. package/dist/timing.d.ts +8 -0
  61. package/dist/timing.js +35 -1
  62. package/dist/types.d.ts +692 -19
  63. package/dist/unread.d.ts +90 -0
  64. package/dist/unread.js +405 -0
  65. package/dist/vocabulary.d.ts +1 -1
  66. package/dist/vocabulary.js +1 -1
  67. package/package.json +4 -2
package/dist/engine.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import path from "node:path";
2
2
  import { readRollout } from "./rollout.js";
3
- import { ruleKey, sha1, summaryOf, summaryText, summaryValue } from "./cache.js";
3
+ import { missingAnalysis, readTarget } from "./analyses.js";
4
+ import { analysisKey, deepFreeze, ruleKey, sha1, summaryOf, summaryText, summaryValue, useKey, } from "./cache.js";
4
5
  import { fileLedger, ledgerKey, ledgerTraits, provesOf, tagRulesOf, withRule } from "./ledger.js";
5
6
  import { defaultOptions } from "./options.js";
6
7
  import { declarationId, fileStated } from "./stated.js";
@@ -8,7 +9,10 @@ import { codeOf, loadRule } from "./lazy.js";
8
9
  import { IGNORE_EXPLAIN, IGNORE_RULE_ID, applyIgnores, fileDirectives } from "./ignores.js";
9
10
  import { addonsWith, appliedOverrides, ruleIsOn, ruleSetting, ruleSettingWith, } from "./severity.js";
10
11
  import { matchingOverrides } from "./overrides.js";
11
- import { defaults } from "./config.js";
12
+ import { unmatchedFacts } from "./package-facts.js";
13
+ import { factsGate } from "./facts-versions.js";
14
+ import { defaults, packExcludes } from "./config.js";
15
+ import { removedOf, renamesOf } from "./renamed.js";
12
16
  import picomatch from "picomatch";
13
17
  import { isGlob } from "./tsconfigs.js";
14
18
  const RULE_SETTINGS = ["error", "warning", "info", "off"];
@@ -98,28 +102,57 @@ export function missingTypesReason(library) {
98
102
  }
99
103
  /**
100
104
  * What the run cannot check as a whole (`CheckResult.notes`): the rules among `rules` whose
101
- * library's types do not resolve in some project with files, and the projects without
102
- * `strictNullChecks`. Undefined when there is nothing to say.
105
+ * library's types do not resolve in some project with files that import it, and the projects without
106
+ * `strictNullChecks`, and the test-runner configs whose test files could not be read. Undefined
107
+ * when there is nothing to say.
103
108
  */
104
- function notesOf(units, rules) {
109
+ function notesOf(units, rules, facts, gate) {
105
110
  const missing = new Map();
106
111
  const looseNulls = [];
112
+ const unreadTestConfigs = new Set();
107
113
  for (const unit of units) {
108
114
  if (unit.files.length === 0)
109
115
  continue;
110
116
  if (!unit.strictNullChecks)
111
117
  looseNulls.push(unit.tsconfig);
118
+ for (const file of unit.unreadTestConfigs)
119
+ unreadTestConfigs.add(file);
112
120
  for (const entry of rules) {
113
121
  const needs = entry.rule.meta.needsTypes;
114
122
  if (needs === undefined || needs.modules.some((module) => unit.typesResolve(module)))
115
123
  continue;
124
+ // A project that uses none of the library's modules has nothing for the rule to check:
125
+ // installing the types would not change that, so there is nothing to say.
126
+ if (!needs.modules.some((module) => unit.importsModule(module)))
127
+ continue;
116
128
  const ids = missing.get(needs.library) ?? new Set();
117
129
  ids.add(entry.id);
118
130
  missing.set(needs.library, ids);
119
131
  }
120
132
  }
121
- if (missing.size === 0 && looseNulls.length === 0)
133
+ // A line one project's types resolve is matched (0041: keys resolve through the type checker).
134
+ const matched = unmatchedFacts(Object.keys(facts).length === 0
135
+ ? []
136
+ : units.filter((unit) => unit.files.length > 0).map((unit) => unit.factKeys(facts)), facts);
137
+ // The config's facts a project's version leaves out, once each: by the first file of each project.
138
+ const outOfRange = new Set();
139
+ for (const unit of units) {
140
+ const [first] = unit.files;
141
+ if (first === undefined)
142
+ continue;
143
+ for (const left of gate.at(first).withheldFacts) {
144
+ if (left.source === "config")
145
+ outOfRange.add(`${left.reason}; they are not applied`);
146
+ }
147
+ }
148
+ if (missing.size === 0 &&
149
+ looseNulls.length === 0 &&
150
+ unreadTestConfigs.size === 0 &&
151
+ matched.unmatched.length === 0 &&
152
+ matched.notChecked.length === 0 &&
153
+ outOfRange.size === 0) {
122
154
  return undefined;
155
+ }
123
156
  const missingTypes = [...missing]
124
157
  .sort(([a], [b]) => compareText(a, b))
125
158
  .map(([library, ids]) => ({
@@ -127,7 +160,14 @@ function notesOf(units, rules) {
127
160
  reason: missingTypesReason(library),
128
161
  rules: [...ids].sort(compareText),
129
162
  }));
130
- return { missingTypes, looseNulls };
163
+ return {
164
+ missingTypes,
165
+ looseNulls,
166
+ unreadTestConfigs: [...unreadTestConfigs].sort(compareText),
167
+ unmatchedFacts: matched.unmatched,
168
+ factsNotChecked: matched.notChecked,
169
+ factsOutOfRange: [...outOfRange].sort(compareText),
170
+ };
131
171
  }
132
172
  /** The own files of the projects without `strictNullChecks`. */
133
173
  function looseNullFiles(units) {
@@ -158,9 +198,28 @@ function ignoreScope(config, ran) {
158
198
  known,
159
199
  ran: new Set([SYNTAX_RULE_ID, IMPORTS_RULE_ID, ...ran.map((entry) => entry.id)]),
160
200
  options: config.ignores,
201
+ renames: renamesOf(config.plugins),
202
+ removed: removedOf(config.plugins),
203
+ offOptions: offOptionsOf(ran),
161
204
  };
162
205
  }
206
+ /** `IgnoreScope.offOptions`: each rule's convention options that are off in the options it ran with. */
207
+ function offOptionsOf(ran) {
208
+ const found = new Map();
209
+ for (const entry of ran) {
210
+ const off = Object.entries(entry.rule.meta.options ?? {})
211
+ .filter(([name, spec]) => spec.kind === "boolean" && spec.convention === true && entry.options[name] === false)
212
+ .map(([name]) => name);
213
+ if (off.length > 0)
214
+ found.set(entry.id, off);
215
+ }
216
+ return found;
217
+ }
163
218
  const FAILURE_HINT = "report this to the rule's authors; the rest of the check ran normally";
219
+ /** The default `exclude` of a config: core's test patterns, then those its packs add. */
220
+ function defaultExcludes(config) {
221
+ return [...new Set([...defaults.exclude, ...packExcludes(config.plugins)])];
222
+ }
164
223
  /**
165
224
  * The facts every loaded plugin ships, joined in plugin order: per package, per function; a later
166
225
  * plugin's line for the same function wins.
@@ -174,12 +233,61 @@ function shippedFactsOf(plugins) {
174
233
  }
175
234
  return joined;
176
235
  }
236
+ /**
237
+ * The gate of a check's facts: the config's by their `"@versions"`, the packs' by the versions
238
+ * they were verified on (`Plugin.factsVerified`; a later plugin's range for a package wins).
239
+ */
240
+ function gateOf(config) {
241
+ const verified = {};
242
+ for (const plugin of config.plugins)
243
+ Object.assign(verified, plugin.factsVerified ?? {});
244
+ return factsGate({
245
+ facts: config.facts,
246
+ versions: config.factVersions,
247
+ shipped: shippedFactsOf(config.plugins),
248
+ verified,
249
+ configDir: config.configDir,
250
+ });
251
+ }
252
+ /** What the cache key of a rule or analysis holds of the facts. */
253
+ function factsKey(config, gate, shipped) {
254
+ return { config: config.facts, shipped, platform: config.platform, versions: gate.key };
255
+ }
177
256
  function nodeLocation(node) {
178
257
  return { file: node.getSourceFile().fileName, start: node.getStart(), end: node.getEnd() };
179
258
  }
180
259
  function isStrings(value) {
181
260
  return Array.isArray(value) && value.every((item) => typeof item === "string");
182
261
  }
262
+ /** The analyses the rules read that the config's plugins publish, by id. */
263
+ function analysisRuns(config, rules, gate) {
264
+ const readers = new Map();
265
+ for (const entry of rules) {
266
+ if (entry.rule.kind !== "project")
267
+ continue;
268
+ for (const id of entry.rule.reads ?? []) {
269
+ const list = readers.get(id) ?? [];
270
+ list.push(entry.id);
271
+ readers.set(id, list);
272
+ }
273
+ }
274
+ const shippedFacts = shippedFactsOf(config.plugins);
275
+ const runs = [];
276
+ for (const [id, readBy] of [...readers].sort(([a], [b]) => compareText(a, b))) {
277
+ const target = readTarget(config.plugins, id);
278
+ if (target.kind !== "found")
279
+ continue;
280
+ const facts = factsKey(config, gate, shippedFacts);
281
+ runs.push({
282
+ id,
283
+ analysis: target.analysis,
284
+ key: analysisKey({ id, analysis: target.analysis, facts }),
285
+ readers: [...readBy].sort(compareText),
286
+ gate,
287
+ });
288
+ }
289
+ return runs;
290
+ }
183
291
  /** The rule's options with each shared list joined with the other rule's (`RuleInfo.sharedOptions`). */
184
292
  function withSharedOptions(config, plugin, rule, options) {
185
293
  const shared = Object.entries(rule.meta.sharedOptions ?? {});
@@ -195,7 +303,7 @@ function withSharedOptions(config, plugin, rule, options) {
195
303
  return merged;
196
304
  }
197
305
  /** A rule with the settings it runs under, and the key its cached results are kept under. */
198
- function entryOf(config, plugin, name, rule, severity, addons) {
306
+ function entryOf(config, plugin, name, rule, severity, addons, gate) {
199
307
  const id = `${plugin}/${name}`;
200
308
  const options = withSharedOptions(config, plugin, rule, config.ruleOptions[id] ?? defaultOptions(rule.meta.options));
201
309
  const active = {
@@ -208,14 +316,16 @@ function entryOf(config, plugin, name, rule, severity, addons) {
208
316
  options,
209
317
  facts: config.facts,
210
318
  shippedFacts: shippedFactsOf(config.plugins),
319
+ gate,
211
320
  };
212
321
  return {
213
322
  ...active,
214
- key: ruleKey({ ...active, facts: { config: active.facts, shipped: active.shippedFacts } }),
323
+ key: ruleKey({ ...active, facts: factsKey(config, gate, active.shippedFacts) }),
215
324
  };
216
325
  }
217
326
  function ruleSets(config, selection) {
218
327
  const rolledOut = rolloutSet(config, selection);
328
+ const gate = gateOf(config);
219
329
  const anywhere = [];
220
330
  const known = new Map();
221
331
  for (const plugin of config.plugins) {
@@ -233,7 +343,7 @@ function ruleSets(config, selection) {
233
343
  // A rule that only an override turns on has no config severity; project findings take
234
344
  // their own from the file they are in, so this one is never shown.
235
345
  const severity = base === "off" ? rule.meta.defaultSeverity : base;
236
- anywhere.push(entryOf(config, plugin.name, name, codeOf(id, rule), severity, addons));
346
+ anywhere.push(entryOf(config, plugin.name, name, codeOf(id, rule), severity, addons, gate));
237
347
  }
238
348
  }
239
349
  anywhere.sort((a, b) => compareText(a.id, b.id));
@@ -244,7 +354,9 @@ function ruleSets(config, selection) {
244
354
  const settings = new Map();
245
355
  return {
246
356
  anywhere,
357
+ analyses: analysisRuns(config, anywhere, gate),
247
358
  perFile,
359
+ gate,
248
360
  forFile(file) {
249
361
  if (!perFile)
250
362
  return anywhere;
@@ -260,14 +372,14 @@ function ruleSets(config, selection) {
260
372
  if (setting === "off") {
261
373
  if (entry.rule.summarize === undefined)
262
374
  return [];
263
- const quiet = entryOf(config, entry.plugin, entry.name, entry.rule, entry.severity, []);
375
+ const quiet = entryOf(config, entry.plugin, entry.name, entry.rule, entry.severity, [], gate);
264
376
  return [{ ...quiet, key: `${quiet.key}:quiet`, quiet: true }];
265
377
  }
266
378
  const prefix = `${entry.plugin}/`;
267
379
  const addons = addonsWith(config, applied)
268
380
  .filter((one) => one.startsWith(prefix))
269
381
  .map((one) => one.slice(prefix.length));
270
- return [entryOf(config, entry.plugin, entry.name, entry.rule, setting, addons)];
382
+ return [entryOf(config, entry.plugin, entry.name, entry.rule, setting, addons, gate)];
271
383
  });
272
384
  variants.set(key, found);
273
385
  }
@@ -401,7 +513,8 @@ function declarationsAt(against) {
401
513
  return [...new Set(against.map(declarationId))];
402
514
  }
403
515
  function fileContext(entry, sourceFile, types, findings, ledger = [], marks = []) {
404
- const { id, addons, options, facts, shippedFacts } = entry;
516
+ const { id, addons, options } = entry;
517
+ const { facts, shippedFacts, withheldFacts } = entry.gate.at(sourceFile.fileName);
405
518
  return {
406
519
  file: sourceFile.fileName,
407
520
  ledger,
@@ -414,6 +527,7 @@ function fileContext(entry, sourceFile, types, findings, ledger = [], marks = []
414
527
  options,
415
528
  facts,
416
529
  shippedFacts,
530
+ withheldFacts,
417
531
  report(problem) {
418
532
  const finding = problemFinding(entry, {
419
533
  message: problem.message,
@@ -434,7 +548,7 @@ function fileContext(entry, sourceFile, types, findings, ledger = [], marks = []
434
548
  if (finding !== undefined)
435
549
  findings.push(finding);
436
550
  },
437
- unknown({ message, hint, node, against }) {
551
+ unknown({ message, hint, node, against, needs, unnamed }) {
438
552
  const guarantees = declarationsAgainst(against, types);
439
553
  findings.push({
440
554
  kind: "unknown",
@@ -444,14 +558,25 @@ function fileContext(entry, sourceFile, types, findings, ledger = [], marks = []
444
558
  location: nodeLocation(node),
445
559
  ...listedFlag(entry),
446
560
  ...(guarantees.length > 0 ? { guarantees } : {}),
561
+ ...(needs === undefined || needs.length === 0 ? {} : { needs }),
562
+ ...(unnamed === undefined || unnamed.length === 0 ? {} : { unnamed }),
447
563
  });
448
564
  },
449
565
  };
450
566
  }
451
- function projectContext(entry, findings, marks = []) {
567
+ function projectContext(entry, findings, marks = [], analyses = {}, unstated = []) {
452
568
  const { id, options } = entry;
453
569
  return {
454
570
  options,
571
+ analyses,
572
+ unstated({ location, needs, unnamed }) {
573
+ unstated.push({
574
+ ruleId: id,
575
+ location,
576
+ ...(needs === undefined || needs.length === 0 ? {} : { needs }),
577
+ ...(unnamed === undefined || unnamed.length === 0 ? {} : { unnamed }),
578
+ });
579
+ },
455
580
  read(ref, outcome) {
456
581
  marks.push({ ref: ref.id, trait: ref.trait, outcome });
457
582
  },
@@ -471,7 +596,7 @@ function projectContext(entry, findings, marks = []) {
471
596
  if (finding !== undefined)
472
597
  findings.push(finding);
473
598
  },
474
- unknown({ message, hint, location, against }) {
599
+ unknown({ message, hint, location, against, needs, unnamed }) {
475
600
  const guarantees = declarationsAt(against);
476
601
  findings.push({
477
602
  kind: "unknown",
@@ -481,6 +606,8 @@ function projectContext(entry, findings, marks = []) {
481
606
  location,
482
607
  ...listedFlag(entry),
483
608
  ...(guarantees.length > 0 ? { guarantees } : {}),
609
+ ...(needs === undefined || needs.length === 0 ? {} : { needs }),
610
+ ...(unnamed === undefined || unnamed.length === 0 ? {} : { unnamed }),
484
611
  });
485
612
  },
486
613
  };
@@ -545,6 +672,12 @@ function atTheirFiles(sets, entry, found, silenced) {
545
672
  return [{ ...finding, severity: own ?? setting }];
546
673
  });
547
674
  }
675
+ /** A project rule's unstated places, without those in files the config turns the rule off in. */
676
+ function unstatedAtTheirFiles(sets, entry, found) {
677
+ if (!sets.perFile)
678
+ return found;
679
+ return found.filter((one) => sets.settingAt(entry.id, one.location.file) !== "off");
680
+ }
548
681
  /**
549
682
  * Where a project rule's failure is reported: the configured tsconfig when there is one, else the
550
683
  * first project's (a glob names no single file).
@@ -580,6 +713,29 @@ function runOnFile(entry, sourceFile, types, same, ledger) {
580
713
  return { result: { findings }, failed: true };
581
714
  }
582
715
  }
716
+ /**
717
+ * Runs an analysis's `summarize` on one file, as `runOnFile` a rule's: the error when it threw
718
+ * (such a result is not cached, and the analysis fails for this check).
719
+ */
720
+ function runAnalysisOnFile(run, sourceFile, types, same) {
721
+ try {
722
+ const value = run.analysis.summarize({
723
+ file: sourceFile.fileName,
724
+ sourceFile,
725
+ types,
726
+ ...run.gate.at(sourceFile.fileName),
727
+ });
728
+ if (value === undefined)
729
+ return { result: { findings: [] }, failed: false };
730
+ const made = summaryOf(value);
731
+ const summary = same.get(made.hash) ?? made;
732
+ same.set(summary.hash, summary);
733
+ return { result: { findings: [], summary }, failed: false };
734
+ }
735
+ catch (error) {
736
+ return { failed: true, error };
737
+ }
738
+ }
583
739
  /**
584
740
  * The first of the two phases of running the rules (then `runProjectPhase`): every active rule
585
741
  * over the own files of `units`. With `rollout` on and the `configured` selection, this reads the
@@ -623,7 +779,10 @@ nextFiles, cache, ignores, timing) {
623
779
  const sets = ruleSets(config, selection);
624
780
  const rules = sets.anywhere;
625
781
  const testFiles = config.replacesDefaultExclude
626
- ? defaults.exclude.map((pattern) => ({ pattern, matches: picomatch(pattern, { dot: true }) }))
782
+ ? defaultExcludes(config).map((pattern) => ({
783
+ pattern,
784
+ matches: picomatch(pattern, { dot: true }),
785
+ }))
627
786
  : undefined;
628
787
  const testsByPattern = new Map();
629
788
  const findings = [];
@@ -643,6 +802,8 @@ nextFiles, cache, ignores, timing) {
643
802
  summaries.set(entry.id, new Map());
644
803
  }
645
804
  }
805
+ const analysisSummaries = new Map(sets.analyses.map((run) => [run.id, new Map()]));
806
+ const analysisFailures = new Map();
646
807
  for (const unit of units) {
647
808
  fileCount += unit.files.length;
648
809
  for (const file of unit.files) {
@@ -664,8 +825,13 @@ nextFiles, cache, ignores, timing) {
664
825
  ? before
665
826
  : undefined;
666
827
  const reusable = holding?.rules;
667
- // Results of rules that did not run this time stay while the file's inputs are the same.
828
+ // Results of rules that did not run this time stay while the file's inputs are the same,
829
+ // and while a recent run used their rule (`pruneUnused`).
668
830
  const results = new Map(reusable ?? []);
831
+ if (cache !== undefined) {
832
+ useKey(cache, SYNTAX_RULE_ID);
833
+ useKey(cache, IMPORTS_RULE_ID);
834
+ }
669
835
  let syntax = results.get(SYNTAX_RULE_ID);
670
836
  if (syntax === undefined) {
671
837
  syntax = { findings: syntaxFinding(unit, file, cwd) };
@@ -726,6 +892,8 @@ nextFiles, cache, ignores, timing) {
726
892
  cache.changed = true;
727
893
  ledgerRefs.push(...ledger.refs);
728
894
  for (const entry of sets.forFile(file)) {
895
+ if (cache !== undefined)
896
+ useKey(cache, entry.key);
729
897
  let result = reusable?.get(entry.key);
730
898
  if (result === undefined) {
731
899
  const sourceFile = timing === undefined
@@ -762,6 +930,41 @@ nextFiles, cache, ignores, timing) {
762
930
  if (result.summary !== undefined)
763
931
  summaries.get(entry.id)?.set(file, result.summary);
764
932
  }
933
+ for (const run of sets.analyses) {
934
+ if (cache !== undefined)
935
+ useKey(cache, run.key);
936
+ let result = reusable?.get(run.key);
937
+ if (result === undefined) {
938
+ const sourceFile = timing === undefined
939
+ ? boundSourceFile(unit, file)
940
+ : timed(timing, "program", () => boundSourceFile(unit, file));
941
+ if (sourceFile === undefined)
942
+ continue;
943
+ fileTypes ??= unit.typesFor(file);
944
+ const types = fileTypes;
945
+ const ran = timing === undefined
946
+ ? runAnalysisOnFile(run, sourceFile, types, sameSummaries)
947
+ : timing.fileSide(run.id, "project", () => runAnalysisOnFile(run, sourceFile, timing.timedTypes(types), sameSummaries));
948
+ if (cache !== undefined)
949
+ cache.changed = true;
950
+ if (ran.failed) {
951
+ results.delete(run.key);
952
+ if (!analysisFailures.has(run.id)) {
953
+ analysisFailures.set(run.id, { file, error: ran.error });
954
+ }
955
+ yield { phase: "files", done, total, file, rule: run.id };
956
+ continue;
957
+ }
958
+ result = ran.result;
959
+ results.set(run.key, result);
960
+ yield { phase: "files", done, total, file, rule: run.id };
961
+ }
962
+ else {
963
+ timing?.reused(run.id, "project");
964
+ }
965
+ if (result.summary !== undefined)
966
+ analysisSummaries.get(run.id)?.set(file, result.summary);
967
+ }
765
968
  if (fingerprint !== undefined) {
766
969
  nextFiles.set(file, {
767
970
  fingerprint,
@@ -787,6 +990,8 @@ nextFiles, cache, ignores, timing) {
787
990
  sets,
788
991
  findings,
789
992
  summaries,
993
+ analysisSummaries,
994
+ analysisFailures,
790
995
  directives,
791
996
  fileCount,
792
997
  testsByPattern: testFiles === undefined ? undefined : testsByPattern,
@@ -794,7 +999,7 @@ nextFiles, cache, ignores, timing) {
794
999
  projects: units.length > 1
795
1000
  ? units.map((unit) => ({ tsconfig: unit.tsconfig, files: unit.files.length }))
796
1001
  : undefined,
797
- notes: notesOf(units, rules),
1002
+ notes: notesOf(units, rules, config.facts, sets.gate),
798
1003
  looseNulls: looseNullFiles(units),
799
1004
  silenced,
800
1005
  stated,
@@ -821,8 +1026,10 @@ export function* projectPhaseSteps(config, phase, started, keepSummaries, cache,
821
1026
  const { sets, findings, summaries, directives, fileCount, testsByPattern, tsconfigLocation } = phase;
822
1027
  const silenced = [...phase.silenced];
823
1028
  const marks = [...phase.ledger.marks];
1029
+ const unstated = [];
824
1030
  const rules = sets.anywhere;
825
1031
  const total = rules.filter((entry) => entry.rule.kind === "project" && summaries.has(entry.id)).length;
1032
+ const analysisOf = solvedAnalyses(sets.analyses, phase.analysisSummaries, phase.analysisFailures, cache, timing);
826
1033
  let done = 0;
827
1034
  let previous;
828
1035
  for (const entry of rules) {
@@ -846,37 +1053,50 @@ export function* projectPhaseSteps(config, phase, started, keepSummaries, cache,
846
1053
  previous = entry.id;
847
1054
  // In file order, so a rule cannot depend on the order the files were visited.
848
1055
  const sorted = [...fileSummaries].sort(([a], [b]) => compareText(a, b));
849
- const inputs = sha1(sorted.map(([file, summary]) => `${file}\u0000${summary.hash}`).join("\n"));
1056
+ const read = readAnalyses(config, entry, analysisOf, tsconfigLocation);
1057
+ if (read.kind === "not-run") {
1058
+ keepSummaries?.set(entry.id, () => summaryMap(sorted));
1059
+ findings.push(...atTheirFiles(sets, entry, read.findings, silenced));
1060
+ continue;
1061
+ }
1062
+ const inputs = sha1([
1063
+ ...sorted.map(([file, summary]) => `${file}\u0000${summary.hash}`),
1064
+ ...read.fingerprints.map((print) => `\u0001${print}`),
1065
+ ].join("\n"));
850
1066
  const before = cache?.projects.get(entry.key);
851
1067
  if (before?.inputs === inputs) {
852
1068
  findings.push(...atTheirFiles(sets, entry, before.findings, silenced));
1069
+ unstated.push(...unstatedAtTheirFiles(sets, entry, before.unstated ?? []));
853
1070
  marks.push(...withRule(entry.id, before.marks ?? []));
854
1071
  // Built only if a hover asks: a CLI run whose summaries are unchanged never parses them.
855
1072
  keepSummaries?.set(entry.id, () => (before.summaries ??= summaryMap(sorted)));
856
1073
  continue;
857
1074
  }
858
1075
  const own = [];
859
- const read = [];
1076
+ const settled = [];
1077
+ const ownUnstated = [];
860
1078
  const rule = entry.rule;
861
1079
  const runCheck = () => {
862
1080
  const asJson = summaryMap(sorted);
863
1081
  keepSummaries?.set(entry.id, () => asJson);
864
- rule.check(asJson, projectContext(entry, own, read));
1082
+ rule.check(asJson, projectContext(entry, own, settled, read.values(), ownUnstated));
865
1083
  return asJson;
866
1084
  };
867
1085
  try {
868
1086
  const asJson = timing === undefined ? runCheck() : timing.projectSide(entry.id, runCheck);
869
- const settledMarks = sortedMarks(read);
1087
+ const settledMarks = sortedMarks(settled);
870
1088
  marks.push(...withRule(entry.id, settledMarks));
871
1089
  if (cache !== undefined) {
872
1090
  cache.projects.set(entry.key, {
873
1091
  inputs,
874
1092
  findings: own,
1093
+ ...(ownUnstated.length === 0 ? {} : { unstated: ownUnstated }),
875
1094
  ...(settledMarks.length === 0 ? {} : { marks: settledMarks }),
876
1095
  summaries: asJson,
877
1096
  });
878
1097
  cache.changed = true;
879
1098
  }
1099
+ unstated.push(...unstatedAtTheirFiles(sets, entry, ownUnstated));
880
1100
  }
881
1101
  catch (error) {
882
1102
  own.push(failureFinding(entry, tsconfigLocation, error));
@@ -884,6 +1104,9 @@ export function* projectPhaseSteps(config, phase, started, keepSummaries, cache,
884
1104
  }
885
1105
  findings.push(...atTheirFiles(sets, entry, own, silenced));
886
1106
  }
1107
+ unstated.sort((a, b) => compareText(a.location.file, b.location.file) ||
1108
+ a.location.start - b.location.start ||
1109
+ compareText(a.ruleId, b.ruleId));
887
1110
  const covered = applyIgnores(withLooseNullFixes(oneCauseOneMessage(findings, rules), rules, phase.looseNulls), directives, ignoreScope(config, rules));
888
1111
  covered.findings.sort(compareFindings);
889
1112
  const { shown, hidden } = capped
@@ -901,6 +1124,7 @@ export function* projectPhaseSteps(config, phase, started, keepSummaries, cache,
901
1124
  ...withTagRules(phase.ledger.tagRules),
902
1125
  },
903
1126
  ...(phase.notes === undefined ? {} : { notes: phase.notes }),
1127
+ ...(unstated.length === 0 ? {} : { unstated }),
904
1128
  stats: {
905
1129
  files: fileCount,
906
1130
  durationMs: Math.round(performance.now() - started),
@@ -908,7 +1132,7 @@ export function* projectPhaseSteps(config, phase, started, keepSummaries, cache,
908
1132
  ? {}
909
1133
  : {
910
1134
  testFiles: [...testsByPattern.values()].reduce((sum, count) => sum + count, 0),
911
- testFilesByPattern: Object.fromEntries(defaults.exclude.flatMap((pattern) => {
1135
+ testFilesByPattern: Object.fromEntries(defaultExcludes(config).flatMap((pattern) => {
912
1136
  const count = testsByPattern.get(pattern);
913
1137
  return count === undefined ? [] : [[pattern, count]];
914
1138
  })),
@@ -917,6 +1141,102 @@ export function* projectPhaseSteps(config, phase, started, keepSummaries, cache,
917
1141
  },
918
1142
  };
919
1143
  }
1144
+ /**
1145
+ * Each analysis of the run, solved at most once and only when a rule asks: an analysis whose
1146
+ * summaries are as the cache last saw them keeps its fingerprint, and is solved again only when a
1147
+ * rule that reads it must run.
1148
+ */
1149
+ function solvedAnalyses(runs, summaries, failures, cache, timing) {
1150
+ const done = new Map();
1151
+ const byId = new Map(runs.map((run) => [run.id, run]));
1152
+ return (id) => {
1153
+ const known = done.get(id);
1154
+ if (known !== undefined)
1155
+ return known;
1156
+ const run = byId.get(id);
1157
+ if (run === undefined)
1158
+ return undefined;
1159
+ // A file it could not summarize: solved without that file, it would be wrong, not partial.
1160
+ const failed = failures.get(id);
1161
+ if (failed !== undefined) {
1162
+ const solved = { kind: "failed", error: failed.error, file: failed.file };
1163
+ done.set(id, solved);
1164
+ return solved;
1165
+ }
1166
+ const sorted = [...(summaries.get(id) ?? [])].sort(([a], [b]) => compareText(a, b));
1167
+ const inputs = sha1(sorted.map(([file, summary]) => `${file}\u0000${summary.hash}`).join("\n"));
1168
+ const solve = () => {
1169
+ const solveNow = () => deepFreeze(run.analysis.solve(summaryMap(sorted)));
1170
+ return timing === undefined ? solveNow() : timing.projectSide(id, solveNow);
1171
+ };
1172
+ let solved;
1173
+ const before = cache?.analyses.get(run.key);
1174
+ try {
1175
+ if (before?.inputs === inputs) {
1176
+ const entry = before;
1177
+ solved = {
1178
+ kind: "solved",
1179
+ fingerprint: entry.fingerprint,
1180
+ value: () => (entry.value ??= solve()),
1181
+ };
1182
+ }
1183
+ else {
1184
+ const value = solve();
1185
+ const fingerprint = sha1(JSON.stringify(value));
1186
+ cache?.analyses.set(run.key, { inputs, fingerprint, value });
1187
+ if (cache !== undefined)
1188
+ cache.changed = true;
1189
+ solved = { kind: "solved", fingerprint, value: () => value };
1190
+ }
1191
+ }
1192
+ catch (error) {
1193
+ cache?.analyses.delete(run.key);
1194
+ solved = { kind: "failed", error };
1195
+ }
1196
+ done.set(id, solved);
1197
+ return solved;
1198
+ };
1199
+ }
1200
+ /**
1201
+ * What a project rule reads (`ProjectRule.reads`): the fingerprints of its analyses for its cache
1202
+ * inputs and their values for its check, or the findings it reports instead of running: one "not
1203
+ * checked" finding for each analysis the config's plugins do not publish (SW4), or a failure.
1204
+ */
1205
+ function readAnalyses(config, entry, analysisOf, location) {
1206
+ const reads = entry.rule.kind === "project" ? (entry.rule.reads ?? []) : [];
1207
+ const missing = [];
1208
+ const solved = [];
1209
+ for (const id of reads) {
1210
+ const found = analysisOf(id);
1211
+ if (found === undefined) {
1212
+ const target = readTarget(config.plugins, id);
1213
+ if (target.kind === "found")
1214
+ continue;
1215
+ missing.push({
1216
+ kind: "unknown",
1217
+ ruleId: entry.id,
1218
+ ...missingAnalysis(entry.id, id, target),
1219
+ location,
1220
+ ...listedFlag(entry),
1221
+ });
1222
+ }
1223
+ else if (found.kind === "failed") {
1224
+ const at = found.file === undefined ? location : { file: found.file, start: 0, end: 0 };
1225
+ const failure = failureFinding(entry, at, found.error);
1226
+ missing.push({ ...failure, message: `${failure.message} (in analysis ${id})` });
1227
+ }
1228
+ else {
1229
+ solved.push({ id, fingerprint: found.fingerprint, value: found.value });
1230
+ }
1231
+ }
1232
+ if (missing.length > 0)
1233
+ return { kind: "not-run", findings: missing };
1234
+ return {
1235
+ kind: "run",
1236
+ fingerprints: solved.map((one) => `${one.id}\u0000${one.fingerprint}`),
1237
+ values: () => Object.fromEntries(solved.map((one) => [one.id, one.value()])),
1238
+ };
1239
+ }
920
1240
  /**
921
1241
  * One cause, one message (`RuleMeta.yieldsTo`): a finding is left out when a rule it yields to
922
1242
  * reports a problem where the finding's `about` is. `about` itself is dropped from every finding.