@stateward/core 1.2.1 → 1.2.2

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.
package/dist/cache.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ProgramRecord } from "./backend/program-record.js";
2
- import type { Finding, FixVerdict, Json, Rule, Severity } from "./types.js";
2
+ import type { Finding, FixVerdict, Json, ReadOutcome, Rule, Severity } from "./types.js";
3
3
  /**
4
4
  * The results a checker keeps between runs, by content:
5
5
  *
@@ -16,7 +16,7 @@ import type { Finding, FixVerdict, Json, Rule, Severity } from "./types.js";
16
16
  * server save it to disk between runs as a `CacheSnapshot` (disk-cache.ts).
17
17
  */
18
18
  /** Bumped whenever what is stored, or how a key is computed, changes. */
19
- export declare const CACHE_FORMAT = 7;
19
+ export declare const CACHE_FORMAT = 10;
20
20
  /**
21
21
  * A project rule's summary of one file: the hash of its JSON text, and either that text or, once a
22
22
  * rule or a hover needed the value (a run whose summaries are all unchanged never does), the parsed
@@ -33,10 +33,20 @@ export type CachedSummary = {
33
33
  readonly value: Json;
34
34
  };
35
35
  };
36
- /** What one rule produced on one file. */
36
+ /** A rule's mark on a reference of the ledger (`FileContext.read`), as its results keep it. */
37
+ export type RuleMark = {
38
+ readonly ref: string;
39
+ readonly trait: string;
40
+ readonly outcome: ReadOutcome;
41
+ };
42
+ /**
43
+ * What one rule produced on one file: its findings, its summary, and its marks on the references of
44
+ * the ledger (absent when it made none).
45
+ */
37
46
  export type RuleResult = {
38
47
  findings: readonly Finding[];
39
48
  summary?: CachedSummary;
49
+ marks?: readonly RuleMark[];
40
50
  };
41
51
  /**
42
52
  * A summary as JSON stores it (a Map becomes `{}`, `undefined` members disappear): a project rule
@@ -66,6 +76,8 @@ export type ProjectEntry = {
66
76
  /** Hash of the rule's summaries, by file. */
67
77
  inputs: string;
68
78
  findings: readonly Finding[];
79
+ /** The rule's marks on the references its file sides handed on (`ProjectContext.read`). */
80
+ marks?: readonly RuleMark[];
69
81
  /**
70
82
  * The summaries as the rule received them, handed to hovers (the same object while unchanged);
71
83
  * undefined until asked for when the entry came from disk.
@@ -116,6 +128,7 @@ export type CacheSnapshot = {
116
128
  findings: readonly Finding[];
117
129
  summary?: string;
118
130
  hash?: string;
131
+ marks?: readonly RuleMark[];
119
132
  }>;
120
133
  /** `FileEntry.reads`. */
121
134
  reads: Record<string, string>;
@@ -123,6 +136,7 @@ export type CacheSnapshot = {
123
136
  projects: Record<string, {
124
137
  inputs: string;
125
138
  findings: readonly Finding[];
139
+ marks?: readonly RuleMark[];
126
140
  }>;
127
141
  programs: Record<string, ProgramRecord>;
128
142
  /** `ResultCache.verdicts`: `null` for a fix that compiles, else TypeScript's message. */
package/dist/cache.js CHANGED
@@ -16,7 +16,7 @@ import { isQueryKind } from "./backend/program-record.js";
16
16
  * server save it to disk between runs as a `CacheSnapshot` (disk-cache.ts).
17
17
  */
18
18
  /** Bumped whenever what is stored, or how a key is computed, changes. */
19
- export const CACHE_FORMAT = 7;
19
+ export const CACHE_FORMAT = 10;
20
20
  /** Frozen all the way down: a cached summary is shared between runs, so no rule may change it. */
21
21
  function deepFreeze(value) {
22
22
  if (typeof value === "object" && value !== null) {
@@ -97,14 +97,13 @@ export function toSnapshot(cache) {
97
97
  for (const [file, entry] of [...cache.files].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
98
98
  const rules = {};
99
99
  for (const [key, result] of entry.rules) {
100
- rules[key] =
101
- result.summary === undefined
102
- ? { findings: result.findings }
103
- : {
104
- findings: result.findings,
105
- summary: summaryText(result.summary),
106
- hash: result.summary.hash,
107
- };
100
+ rules[key] = {
101
+ findings: result.findings,
102
+ ...(result.summary === undefined
103
+ ? {}
104
+ : { summary: summaryText(result.summary), hash: result.summary.hash }),
105
+ ...(result.marks === undefined ? {} : { marks: result.marks }),
106
+ };
108
107
  }
109
108
  files[file] = {
110
109
  fingerprint: entry.fingerprint,
@@ -114,7 +113,11 @@ export function toSnapshot(cache) {
114
113
  }
115
114
  const projects = {};
116
115
  for (const [key, entry] of cache.projects) {
117
- projects[key] = { inputs: entry.inputs, findings: entry.findings };
116
+ projects[key] = {
117
+ inputs: entry.inputs,
118
+ findings: entry.findings,
119
+ ...(entry.marks === undefined ? {} : { marks: entry.marks }),
120
+ };
118
121
  }
119
122
  return {
120
123
  format: CACHE_FORMAT,
@@ -234,6 +237,45 @@ function stringRecord(value) {
234
237
  }
235
238
  return record;
236
239
  }
240
+ function parseOutcome(value) {
241
+ if (!isFields(value))
242
+ return undefined;
243
+ const { kind } = value;
244
+ if (kind === "decided" || kind === "irrelevant")
245
+ return { kind };
246
+ if (kind === "handed-on" && typeof value.to === "string")
247
+ return { kind, to: value.to };
248
+ if (kind === "unchecked" && typeof value.reason === "string") {
249
+ return { kind, reason: value.reason };
250
+ }
251
+ return undefined;
252
+ }
253
+ function parseMark(value) {
254
+ if (!isFields(value) || typeof value.ref !== "string" || typeof value.trait !== "string") {
255
+ return undefined;
256
+ }
257
+ const outcome = parseOutcome(value.outcome);
258
+ return outcome && { ref: value.ref, trait: value.trait, outcome };
259
+ }
260
+ /** A result's marks: absent stays absent; anything that is not a list of marks is undefined. */
261
+ function parseMarks(value) {
262
+ if (value === undefined)
263
+ return {};
264
+ const marks = parseList(value, parseMark);
265
+ return marks && { marks };
266
+ }
267
+ /** A JSON list of strings; undefined when it is anything else. */
268
+ function parseStrings(value) {
269
+ if (!Array.isArray(value))
270
+ return undefined;
271
+ const found = [];
272
+ for (const item of value) {
273
+ if (typeof item !== "string")
274
+ return undefined;
275
+ found.push(item);
276
+ }
277
+ return found;
278
+ }
237
279
  export function parseFinding(value) {
238
280
  if (!isFields(value))
239
281
  return undefined;
@@ -247,10 +289,19 @@ export function parseFinding(value) {
247
289
  }
248
290
  if (kind === "failure")
249
291
  return { kind, ruleId, message, hint, location };
292
+ const guarantees = parseStrings(value.guarantees);
293
+ if (value.guarantees !== undefined && guarantees === undefined)
294
+ return undefined;
250
295
  if (kind === "unknown") {
251
- return value.listed === true
252
- ? { kind, ruleId, message, hint, location, listed: true }
253
- : { kind, ruleId, message, hint, location };
296
+ return {
297
+ kind,
298
+ ruleId,
299
+ message,
300
+ hint,
301
+ location,
302
+ ...(value.listed === true ? { listed: true } : {}),
303
+ ...(guarantees === undefined ? {} : { guarantees }),
304
+ };
254
305
  }
255
306
  if (kind !== "problem" || !isSeverity(value.severity))
256
307
  return undefined;
@@ -275,6 +326,8 @@ export function parseFinding(value) {
275
326
  fixes,
276
327
  ...(findingKind === undefined ? {} : { findingKind }),
277
328
  ...(about === undefined ? {} : { about }),
329
+ ...(value.against === true ? { against: true } : {}),
330
+ ...(guarantees === undefined ? {} : { guarantees }),
278
331
  };
279
332
  }
280
333
  /**
@@ -319,11 +372,14 @@ export function parseSnapshot(value) {
319
372
  if (findings === undefined)
320
373
  return emptyCache();
321
374
  const { summary, hash } = result;
375
+ const marks = parseMarks(result.marks);
376
+ if (marks === undefined)
377
+ return emptyCache();
322
378
  if (summary === undefined && hash === undefined) {
323
- rules.set(key, { findings });
379
+ rules.set(key, { findings, ...marks });
324
380
  }
325
381
  else if (typeof summary === "string" && typeof hash === "string") {
326
- rules.set(key, { findings, summary: summaryFromText(summary, hash) });
382
+ rules.set(key, { findings, summary: summaryFromText(summary, hash), ...marks });
327
383
  }
328
384
  else {
329
385
  return emptyCache();
@@ -339,10 +395,11 @@ export function parseSnapshot(value) {
339
395
  if (!isFields(entry) || typeof entry.inputs !== "string")
340
396
  return emptyCache();
341
397
  const findings = parseList(entry.findings, parseFinding);
342
- if (findings === undefined)
398
+ const marks = parseMarks(entry.marks);
399
+ if (findings === undefined || marks === undefined)
343
400
  return emptyCache();
344
401
  // The summaries are rebuilt from the file entries when the rule runs.
345
- cache.projects.set(key, { inputs: entry.inputs, findings });
402
+ cache.projects.set(key, { inputs: entry.inputs, findings, ...marks });
346
403
  }
347
404
  return cache;
348
405
  }
package/dist/checker.d.ts CHANGED
@@ -124,6 +124,11 @@ export type Checker = {
124
124
  * of that check has the file. `check --fix` edits this text, never a newer one on disk.
125
125
  */
126
126
  analysedText(file: string): string | undefined;
127
+ /**
128
+ * Whether the config turns `ruleId` off for `file`, by the rule's setting or an `overrides`
129
+ * entry: the rule never looked at the guarantee's places there (`proveGuarantees`).
130
+ */
131
+ ruleOffAt(ruleId: string, file: string): boolean;
127
132
  };
128
133
  /**
129
134
  * Editor requests are answered from the programs of the last check. A plugin that throws
package/dist/checker.js CHANGED
@@ -2,7 +2,9 @@ import { createBackend, toTypeScriptPath } from "./backend/typescript6.js";
2
2
  import { emptyCache } from "./cache.js";
3
3
  import { reviewFixes, verifyFixSet } from "./fixes.js";
4
4
  import { legacyMarkerRewrite } from "./guarantee-markers.js";
5
- import { drain, filePhaseRun, needsJsDoc, projectPhaseSteps, projectRuleIds, runFileRules, silentReasons, } from "./engine.js";
5
+ import { ledgerGuarantees } from "./ledger.js";
6
+ import { unlistedGuarantees } from "./stated.js";
7
+ import { drain, filePhaseRun, needsJsDoc, projectPhaseSteps, projectRuleIds, ruleOffAtFn, runFileRules, silentReasons, } from "./engine.js";
6
8
  /** The default slice: short against the editor's 200 ms budget, long against a pause's cost. */
7
9
  export const SLICE_MS = 20;
8
10
  function fileTotal(units) {
@@ -32,9 +34,16 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
32
34
  cache: cache === false ? undefined : cache,
33
35
  jsDoc: needsJsDoc(config),
34
36
  });
37
+ let offAt;
35
38
  /** The units of the last run; indexed (and bound) on the first editor request. */
36
39
  let lastUnits;
37
40
  let known;
41
+ /** The declarations that state a guarantee, of the last full check's files. */
42
+ let stated = [];
43
+ /** The references of the last full check's ledger. */
44
+ let ledgerRefs = [];
45
+ /** The rule that proves each tag trait, in the last full check. */
46
+ let tagRules = {};
38
47
  /** The project rules' summaries of the last full check. */
39
48
  let summaries = new Map();
40
49
  /** Where verdicts are kept when the checker keeps no cache. */
@@ -76,6 +85,9 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
76
85
  const kept = new Map();
77
86
  const result = yield* projectPhaseSteps(config, phase, started, kept, cache === false ? undefined : cache, uncapped === undefined, timing);
78
87
  summaries = kept;
88
+ stated = phase.stated;
89
+ ledgerRefs = result.ledger?.refs ?? [];
90
+ tagRules = result.ledger?.tagRules ?? {};
79
91
  return result;
80
92
  }
81
93
  return { steps: steps(), abandon: () => filePhase?.abandon() };
@@ -147,9 +159,13 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
147
159
  }) ?? []);
148
160
  },
149
161
  guarantees() {
150
- return config.plugins.flatMap((plugin) => plugin.editor?.guarantees?.({
162
+ const listed = config.plugins.flatMap((plugin) => plugin.editor?.guarantees?.({
151
163
  projectSummaries: (ruleId) => summaries.get(ruleId)?.(),
152
164
  }) ?? []);
165
+ // A stated declaration no plugin lists still reads "not checked", never missing; so does a
166
+ // declaration the ledger has references to (a function stated by its return type).
167
+ const known = [...listed, ...unlistedGuarantees(stated, listed)];
168
+ return [...known, ...ledgerGuarantees(ledgerRefs, known, tagRules)];
153
169
  },
154
170
  reviewFixes(findings) {
155
171
  const units = (lastUnits ??= backend.open(new Map()));
@@ -158,6 +174,10 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
158
174
  verifyFixSet(forms) {
159
175
  return verifyFixSet(forms, (lastUnits ??= backend.open(new Map())));
160
176
  },
177
+ ruleOffAt(ruleId, file) {
178
+ offAt ??= ruleOffAtFn(config);
179
+ return offAt(ruleId, toTypeScriptPath(file));
180
+ },
161
181
  analysedText(file) {
162
182
  const name = toTypeScriptPath(file);
163
183
  for (const unit of lastUnits ?? []) {
package/dist/config.js CHANGED
@@ -215,6 +215,50 @@ function parseNeedsTypes(value) {
215
215
  return "invalid";
216
216
  return { library, modules: [...modules] };
217
217
  }
218
+ /** `RuleProves.flows` as given: undefined when left out, "invalid" when not one. */
219
+ function parseFlows(value) {
220
+ if (value === undefined)
221
+ return undefined;
222
+ if (!isRecord(value))
223
+ return "invalid";
224
+ const { produced, decided, escaped } = value;
225
+ const lists = { produced, decided, escaped };
226
+ if (Object.values(lists).some((list) => list !== undefined && !isStringArray(list))) {
227
+ return "invalid";
228
+ }
229
+ if (Object.keys(value).some((key) => !(key in lists)))
230
+ return "invalid";
231
+ return {
232
+ ...(isStringArray(produced) ? { produced: [...produced] } : {}),
233
+ ...(isStringArray(decided) ? { decided: [...decided] } : {}),
234
+ ...(isStringArray(escaped) ? { escaped: [...escaped] } : {}),
235
+ };
236
+ }
237
+ /** `RuleInfo.proves` as given: undefined when left out, "invalid" when not a `RuleProves`. */
238
+ function parseProves(value) {
239
+ if (value === undefined)
240
+ return undefined;
241
+ if (!isRecord(value))
242
+ return "invalid";
243
+ const { traits, returns, made, holders, tags } = value;
244
+ const lists = { traits, returns, made, holders, tags };
245
+ if (Object.values(lists).some((list) => list !== undefined && !isStringArray(list))) {
246
+ return "invalid";
247
+ }
248
+ if (Object.keys(value).some((key) => !(key in lists) && key !== "flows"))
249
+ return "invalid";
250
+ const flows = parseFlows(value.flows);
251
+ if (flows === "invalid")
252
+ return "invalid";
253
+ return {
254
+ ...(isStringArray(traits) ? { traits: [...traits] } : {}),
255
+ ...(isStringArray(returns) ? { returns: [...returns] } : {}),
256
+ ...(isStringArray(made) ? { made: [...made] } : {}),
257
+ ...(isStringArray(holders) ? { holders: [...holders] } : {}),
258
+ ...(flows === undefined ? {} : { flows }),
259
+ ...(isStringArray(tags) ? { tags: [...tags] } : {}),
260
+ };
261
+ }
218
262
  /** The metadata a pack declares up front (`RuleInfo`): everything but the `explain` text. */
219
263
  function parseInfo(value, where, errors) {
220
264
  const found = parseInfoFields(value, where, errors);
@@ -248,7 +292,13 @@ function parseInfo(value, where, errors) {
248
292
  ...sharing,
249
293
  needsTypes,
250
294
  };
251
- const info = fixNeedsStrictNulls === true ? { ...typed, fixNeedsStrictNulls: true } : typed;
295
+ const proves = parseProves(value.proves);
296
+ if (proves === "invalid") {
297
+ errors.push(`${where}.meta.proves must be { traits?, returns?, made?, holders?, flows?: { produced?, decided?, escaped? }, tags? } (each a list of traits or tag names) when present`);
298
+ return undefined;
299
+ }
300
+ const strict = fixNeedsStrictNulls === true ? { ...typed, fixNeedsStrictNulls: true } : typed;
301
+ const info = proves === undefined ? strict : { ...strict, proves };
252
302
  if (value.options === undefined)
253
303
  return info;
254
304
  const options = parseOptionSpecs(value.options, where, errors);
package/dist/engine.d.ts CHANGED
@@ -2,7 +2,7 @@ import type { CachedSummary, ResultCache } from "./cache.js";
2
2
  import type { IgnoreDirective } from "./ignores.js";
3
3
  import type { ProjectUnit } from "./backend/typescript6.js";
4
4
  import type { TimingRecorder } from "./timing.js";
5
- import type { PackageFactsConfig, CheckNotes, CheckResult, Config, Finding, HiddenFindings, Json, Location, Options, Rule, RuleSelection, RuleSetting, Severity } from "./types.js";
5
+ import type { PackageFactsConfig, CheckNotes, CheckResult, Config, Finding, HiddenFindings, Json, Ledger, Location, Options, Rule, RuleSelection, RuleSetting, Severity, StatedDeclaration } from "./types.js";
6
6
  /** More findings of one rule in one file than this are counted, not listed. */
7
7
  export declare const MAX_FINDINGS_PER_RULE_PER_FILE = 20;
8
8
  /**
@@ -49,8 +49,14 @@ type RuleSets = {
49
49
  settingAt(ruleId: string, file: string): RuleSetting;
50
50
  };
51
51
  export declare function compareFindings(a: Finding, b: Finding): number;
52
+ /**
53
+ * A rule with its cache key. `quiet`: the config turns the rule off for this file, but the rule
54
+ * keeps a summary of it (a guarantee's places), so a guarantee is never "unused" only because the
55
+ * rule is off there: it runs, and its findings are dropped.
56
+ */
52
57
  type RuleEntry = ActiveRule & {
53
58
  key: string;
59
+ quiet?: true;
54
60
  };
55
61
  /**
56
62
  * What the rules' file sides leave for their project sides: findings, summaries and what the
@@ -76,6 +82,12 @@ export type FilePhase = {
76
82
  readonly notes: CheckNotes | undefined;
77
83
  /** The own files of the projects without `strictNullChecks`. */
78
84
  readonly looseNulls: ReadonlySet<string>;
85
+ /** The linked findings of a rule the config turned off where they lie (`CheckResult.silenced`). */
86
+ readonly silenced: readonly Finding[];
87
+ /** The declarations that state a guarantee in their written type, for every own file. */
88
+ readonly stated: readonly StatedDeclaration[];
89
+ /** The reference ledger of every own file, and the marks the rules' file sides made on it. */
90
+ readonly ledger: Ledger;
79
91
  };
80
92
  /**
81
93
  * The first of the two phases of running the rules (then `runProjectPhase`): every active rule
@@ -130,6 +142,12 @@ export declare function limitFindings(sorted: readonly Finding[]): {
130
142
  hidden: HiddenFindings[];
131
143
  };
132
144
  /** Ids of the rules that `selection` runs somewhere (not off everywhere; the rollout applied for `configured`). */
145
+ /**
146
+ * Whether the config turns a rule off for a file (the rule off, or an `overrides` entry that
147
+ * does): `stateward guarantees` reads it for the places a guarantee covers, where the rule never
148
+ * looked. Built once; every rule counts, whatever the rollout.
149
+ */
150
+ export declare function ruleOffAtFn(config: Config): (ruleId: string, file: string) => boolean;
133
151
  export declare function activeRuleIds(config: Config, selection: RuleSelection): ReadonlySet<string>;
134
152
  /**
135
153
  * Whether a rule that the config does not switch off reads JSDoc (`RuleMeta.needsJsDoc`), so files