@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/define.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import type { EditorContributions, FileRule, Json, NoOptions, OptionSpecs, PackageFactsConfig, PluginOf, ProjectRule, PluginRule } from "./types.js";
1
+ import type { Analyses, EditorContributions, FileRule, Json, NoOptions, OptionSpecs, PackageFactsConfig, PluginAnalysis, PluginOf, ProjectRule, PluginReport, PluginRule, VerifiedFacts } from "./types.js";
2
2
  /**
3
3
  * Identity helpers: they return what they are given, so TypeScript can infer the rule's options
4
4
  * from `meta.options` and type `ctx.options` in `check`. A rule without `meta.options` takes no
5
5
  * options, and a config that gives it some does not compile.
6
6
  */
7
7
  export declare function defineRule<S extends OptionSpecs = NoOptions, G extends string = string>(rule: FileRule<S, G>): FileRule<S, G>;
8
- export declare function defineRule<Summary extends Json, S extends OptionSpecs = NoOptions, G extends string = string>(rule: ProjectRule<Summary, S, G>): ProjectRule<Summary, S, G>;
8
+ export declare function defineRule<Summary extends Json, S extends OptionSpecs = NoOptions, G extends string = string, A extends Analyses = Analyses>(rule: ProjectRule<Summary, S, G, A>): ProjectRule<Summary, S, G, A>;
9
9
  /** Keeps the plugin's name, rule keys and rule groups as literal types, so config rule and group ids can be completed. */
10
10
  export declare function definePlugin<const N extends string, const R extends Readonly<Record<string, PluginRule>>>(plugin: {
11
11
  name: N;
@@ -13,5 +13,11 @@ export declare function definePlugin<const N extends string, const R extends Rea
13
13
  rules: R;
14
14
  editor?: EditorContributions;
15
15
  facts?: PackageFactsConfig;
16
+ factsVerified?: Readonly<Record<string, VerifiedFacts>>;
17
+ reports?: Readonly<Record<string, PluginReport>>;
18
+ analyses?: Readonly<Record<string, PluginAnalysis>>;
19
+ defaultExclude?: readonly string[];
20
+ renamed?: Readonly<Record<string, string>>;
21
+ removed?: readonly string[];
16
22
  }): PluginOf<N, R>;
17
23
  //# sourceMappingURL=define.d.ts.map
@@ -1,4 +1,6 @@
1
1
  import type { ResultCache } from "./cache.js";
2
+ /** The stateward version (core's): a new release reads nothing an older one wrote. */
3
+ export declare function statewardVersion(): string;
2
4
  /**
3
5
  * The results of earlier runs on disk, read and written by the CLI (`stateward check`) and the
4
6
  * language server alike: one JSON file per project and implementation in the cache folder, keyed
@@ -1,11 +1,21 @@
1
+ import { createRequire } from "node:module";
1
2
  import { createHash, randomBytes } from "node:crypto";
2
- import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, renameSync, rmSync, rmdirSync, unlinkSync, writeFileSync, } from "node:fs";
3
+ import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, statSync, renameSync, rmSync, rmdirSync, unlinkSync, writeSync, } from "node:fs";
3
4
  import { homedir } from "node:os";
4
5
  import path from "node:path";
5
- import { emptyCache, parseSnapshot, toSnapshot } from "./cache.js";
6
+ import { END_TEXT, emptyCache, parseSnapshot, pruneUnused, snapshotChunks } from "./cache.js";
6
7
  import { cacheEntries, dropLeastRecentlyUsed, limitBytes, markUsed } from "./disk-limits.js";
7
8
  import { implementationId } from "./implementation.js";
8
- import { statewardVersion } from "./package-cache.js";
9
+ let version;
10
+ /** The stateward version (core's): a new release reads nothing an older one wrote. */
11
+ export function statewardVersion() {
12
+ if (version === undefined) {
13
+ const read = createRequire(import.meta.url)("../package.json");
14
+ const field = typeof read === "object" && read !== null ? Reflect.get(read, "version") : undefined;
15
+ version = typeof field === "string" ? field : "unknown";
16
+ }
17
+ return version;
18
+ }
9
19
  /**
10
20
  * The results of earlier runs on disk, read and written by the CLI (`stateward check`) and the
11
21
  * language server alike: one JSON file per project and implementation in the cache folder, keyed
@@ -115,7 +125,7 @@ io = {}) {
115
125
  const key = createHash("sha1").update(`${project}\n${implementation}`).digest("hex").slice(0, 16);
116
126
  const file = path.join(dir, `check-${key}.json`);
117
127
  const version = statewardVersion();
118
- const cache = readCache(file, { version, implementation, project });
128
+ const cache = readCache(file, { version, implementation, project }, limit);
119
129
  // Used now: when the folder is over its limit, the files used longest ago go first.
120
130
  if (cache.files.size > 0)
121
131
  markUsed(file);
@@ -127,12 +137,17 @@ io = {}) {
127
137
  const temporary = `${file}.${String(process.pid)}.${randomBytes(4).toString("hex")}.tmp`;
128
138
  try {
129
139
  mkdirSync(dir, { recursive: true });
130
- writeFileSync(temporary, JSON.stringify({
131
- stateward: version,
132
- implementation,
133
- project,
134
- snapshot: toSnapshot(cache),
135
- }));
140
+ pruneUnused(cache);
141
+ const head = JSON.stringify({ stateward: version, implementation, project });
142
+ const written = writeCapped(temporary, `${head.slice(0, -1)},"snapshot":`, snapshotChunks(cache), "}", limit);
143
+ if (!written) {
144
+ // Over the limit before any file's results: the old file would be stale, and a cold
145
+ // run costs time, never findings.
146
+ rmSync(temporary, { force: true });
147
+ rmSync(file, { force: true });
148
+ cache.changed = false;
149
+ return undefined;
150
+ }
136
151
  renameWithRetry(io.rename ?? renameSync, temporary, file, io.wait ?? sleep);
137
152
  // Saved: a later save (after fixes were tried) writes only what changes after this one.
138
153
  cache.changed = false;
@@ -146,9 +161,60 @@ io = {}) {
146
161
  },
147
162
  };
148
163
  }
149
- function readCache(file, expected) {
164
+ /**
165
+ * Writes `head`, the chunks and `tail` to `file`, one chunk at a time, so no string holds the
166
+ * whole text. A file's results that would take the file past `limit` bytes are left out, with
167
+ * every file's after them: they are checked again on the next run. False, with nothing useful
168
+ * written, when the parts other than the files' results alone pass the limit.
169
+ */
170
+ function writeCapped(file, head, chunks, tail, limit) {
171
+ const fd = openSync(file, "w");
172
+ try {
173
+ let bytes = 0;
174
+ const write = (text) => {
175
+ bytes += writeSync(fd, text);
176
+ };
177
+ // Room is kept for the closing text, so the file stays whole and within the limit.
178
+ const tailBytes = Buffer.byteLength(tail) + Buffer.byteLength(END_TEXT);
179
+ if (Buffer.byteLength(head) + tailBytes > limit)
180
+ return false;
181
+ write(head);
182
+ let entries = 0;
183
+ let full = false;
184
+ for (const chunk of chunks) {
185
+ if (chunk.kind === "end") {
186
+ write(chunk.text);
187
+ continue;
188
+ }
189
+ if (chunk.kind === "part") {
190
+ if (bytes + Buffer.byteLength(chunk.text) + tailBytes > limit)
191
+ return false;
192
+ write(chunk.text);
193
+ continue;
194
+ }
195
+ if (full)
196
+ continue;
197
+ const text = entries === 0 ? chunk.text : `,${chunk.text}`;
198
+ if (bytes + Buffer.byteLength(text) + tailBytes > limit) {
199
+ full = true;
200
+ continue;
201
+ }
202
+ write(text);
203
+ entries++;
204
+ }
205
+ write(tail);
206
+ return true;
207
+ }
208
+ finally {
209
+ closeSync(fd);
210
+ }
211
+ }
212
+ function readCache(file, expected, limit) {
150
213
  let parsed;
151
214
  try {
215
+ // A file past the limit (an older stateward wrote it) is not read: it may not fit in a string.
216
+ if (statSync(file).size > limit)
217
+ return emptyCache();
152
218
  parsed = JSON.parse(readFileSync(file, "utf8"));
153
219
  }
154
220
  catch {
package/dist/engine.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import type { CachedSummary, ResultCache } from "./cache.js";
2
2
  import type { IgnoreDirective } from "./ignores.js";
3
+ import type { FactsGate } from "./facts-versions.js";
3
4
  import type { ProjectUnit } from "./backend/typescript6.js";
4
5
  import type { TimingRecorder } from "./timing.js";
5
- import type { PackageFactsConfig, CheckNotes, CheckResult, Config, Finding, HiddenFindings, Json, Ledger, Location, Options, Rule, RuleSelection, RuleSetting, Severity, StatedDeclaration } from "./types.js";
6
+ import type { PackageFactsConfig, CheckNotes, CheckResult, Config, Finding, HiddenFindings, Json, Ledger, Location, Options, PluginAnalysis, Rule, RuleSelection, RuleSetting, Severity, StatedDeclaration } from "./types.js";
6
7
  /** More findings of one rule in one file than this are counted, not listed. */
7
8
  export declare const MAX_FINDINGS_PER_RULE_PER_FILE = 20;
8
9
  /**
@@ -32,6 +33,8 @@ type ActiveRule = {
32
33
  facts: PackageFactsConfig;
33
34
  /** The facts the loaded plugins ship (`Plugin.facts`): part of its cache key too. */
34
35
  shippedFacts: PackageFactsConfig;
36
+ /** Both sets as they hold for a file's project versions (`"@versions"`, `factsVerified`). */
37
+ gate: FactsGate;
35
38
  };
36
39
  /**
37
40
  * The rules a run uses, with the settings of the files they run on. A rule that is on in the
@@ -41,13 +44,30 @@ type ActiveRule = {
41
44
  */
42
45
  type RuleSets = {
43
46
  anywhere: readonly RuleEntry[];
47
+ /** The analyses the rules of `anywhere` read and the config's plugins publish, by id. */
48
+ analyses: readonly AnalysisRun[];
44
49
  /** What to run on `file`: the project rules (for their summaries) and the file rules that are on there. */
45
50
  forFile(file: string): readonly RuleEntry[];
46
51
  /** Whether findings need to be judged by the file they are in. */
47
52
  perFile: boolean;
53
+ /** The facts of each file, as they hold for its project's versions. */
54
+ gate: FactsGate;
48
55
  /** The setting of a rule at a file, "off" when it is not on there. */
49
56
  settingAt(ruleId: string, file: string): RuleSetting;
50
57
  };
58
+ /**
59
+ * An analysis a rule that runs reads (`ProjectRule.reads`): run once per check, on every file,
60
+ * whether or not its publisher's rules are on. `key` is its cache key (`analysisKey`); `readers`
61
+ * are the rules that read it, sorted (the first is named on a failure of its file side).
62
+ */
63
+ type AnalysisRun = {
64
+ id: string;
65
+ analysis: PluginAnalysis;
66
+ key: string;
67
+ readers: readonly string[];
68
+ /** As `FileContext.facts`, `shippedFacts` and `withheldFacts`, for each file. */
69
+ gate: FactsGate;
70
+ };
51
71
  export declare function compareFindings(a: Finding, b: Finding): number;
52
72
  /**
53
73
  * A rule with its cache key. `quiet`: the config turns the rule off for this file, but the rule
@@ -58,6 +78,11 @@ type RuleEntry = ActiveRule & {
58
78
  key: string;
59
79
  quiet?: true;
60
80
  };
81
+ /** Where an analysis's file side first threw in this check, and what. */
82
+ type AnalysisFailure = {
83
+ readonly file: string;
84
+ readonly error: unknown;
85
+ };
61
86
  /**
62
87
  * What the rules' file sides leave for their project sides: findings, summaries and what the
63
88
  * result's statistics need, and nothing of the programs, so a caller that needs no more of them
@@ -67,6 +92,13 @@ export type FilePhase = {
67
92
  readonly sets: RuleSets;
68
93
  readonly findings: Finding[];
69
94
  readonly summaries: ReadonlyMap<string, ReadonlyMap<string, CachedSummary>>;
95
+ /** The summaries of each analysis the rules read (`RuleSets.analyses`), by id, then by file. */
96
+ readonly analysisSummaries: ReadonlyMap<string, ReadonlyMap<string, CachedSummary>>;
97
+ /**
98
+ * The analyses whose `summarize` threw on a file, by id: incomplete, so the rules that read
99
+ * them do not run and each reports the failure.
100
+ */
101
+ readonly analysisFailures: ReadonlyMap<string, AnalysisFailure>;
70
102
  readonly directives: ReadonlyMap<string, readonly IgnoreDirective[]>;
71
103
  readonly fileCount: number;
72
104
  /** Test files checked by pattern, when the config's `exclude` replaces the default list. */