@stateward/core 1.0.0 → 1.0.1-rc.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.
@@ -625,6 +625,21 @@ shared = isLibraryFile) {
625
625
  },
626
626
  };
627
627
  }
628
+ /**
629
+ * The host a recorded program is replayed against: the disk, with `overlays` in front of it as
630
+ * `hostWithOverlays` serves them (an overlay exists, and its text is what is read), so a replay
631
+ * answers as the build it stands for would have.
632
+ */
633
+ function replayHost(options, overlays) {
634
+ const base = ts.createCompilerHost(options, false);
635
+ if (overlays.size === 0)
636
+ return base;
637
+ return {
638
+ ...base,
639
+ fileExists: (fileName) => overlays.has(fileName) || base.fileExists(fileName),
640
+ readFile: (fileName) => overlays.get(fileName) ?? base.readFile(fileName),
641
+ };
642
+ }
628
643
  function matcher(patterns) {
629
644
  if (patterns.length === 0)
630
645
  return () => false;
@@ -1609,8 +1624,10 @@ export function createBackend({ config, cwd, cache, jsDoc = false, }) {
1609
1624
  if (missingLib !== undefined)
1610
1625
  throw new TypeScriptLibMissingError(missingLib);
1611
1626
  // Only a program built from nothing asks the host every question; one built on an old
1612
- // program, or on unsaved text, is not recorded.
1613
- const queries = cache !== undefined && oldProgram === undefined && overlays.size === 0 && !trustCache
1627
+ // program is not recorded. One built on unsaved text is: its answers are the overlays', and a
1628
+ // replay serves the overlays of its own run the same way (`replayHost`), so the record holds
1629
+ // exactly while the open documents have that text (an editor's next start).
1630
+ const queries = cache !== undefined && oldProgram === undefined && !trustCache
1614
1631
  ? new Map()
1615
1632
  : undefined;
1616
1633
  const plainHost = hostWithOverlays(parsed.options, overlays, sources, trustCache, jsDoc);
@@ -1705,9 +1722,9 @@ export function createBackend({ config, cwd, cache, jsDoc = false, }) {
1705
1722
  * now: its files and fingerprints come from the record, and the program is built only if
1706
1723
  * something asks for a node or a type.
1707
1724
  */
1708
- const recordedUnit = (project, claimed, record) => {
1725
+ const recordedUnit = (project, claimed, record, overlays) => {
1709
1726
  let built;
1710
- const real = () => (built ??= buildUnit(project, claimed, new Map(), false));
1727
+ const real = () => (built ??= buildUnit(project, claimed, overlays, false));
1711
1728
  return {
1712
1729
  tsconfig: project.tsconfigPath,
1713
1730
  files: record.files,
@@ -1738,13 +1755,12 @@ export function createBackend({ config, cwd, cache, jsDoc = false, }) {
1738
1755
  const record = cache?.programs.get(project.tsconfigPath);
1739
1756
  const replayable = record !== undefined &&
1740
1757
  !previous.has(project.tsconfigPath) &&
1741
- normalized.size === 0 &&
1742
1758
  !trustCache &&
1743
1759
  record.inputs ===
1744
1760
  recordInputs(programInputs(project.parsed, claimed), project.parsed.options) &&
1745
- replays(record.queries, ts.createCompilerHost(project.parsed.options, false));
1761
+ replays(record.queries, replayHost(project.parsed.options, normalized));
1746
1762
  return replayable
1747
- ? recordedUnit(project, claimed, record)
1763
+ ? recordedUnit(project, claimed, record, normalized)
1748
1764
  : buildUnit(project, claimed, normalized, trustCache);
1749
1765
  });
1750
1766
  };
package/dist/checker.d.ts CHANGED
@@ -65,8 +65,14 @@ export type Checker = {
65
65
  * The fast pass: runs only the file rules, only on `files` (own files; others are ignored), on
66
66
  * programs that reuse the previous ones. Findings are for those files only; project rules do not
67
67
  * run, so their findings are the last `check`'s.
68
+ *
69
+ * The rules run on each file, unless it is in `reusable` and the checker's cache holds its
70
+ * results under its current dependency fingerprint and reads (as a full check would reuse them):
71
+ * then they are taken from there. The fast pass never writes the cache. Name only files that are
72
+ * likely unchanged (an editor's document just opened): the first such question computes the
73
+ * fingerprints of the whole program, which the next full check reuses.
68
74
  */
69
- checkFiles(files: readonly string[], overlays?: ReadonlyMap<string, string>): CheckResult;
75
+ checkFiles(files: readonly string[], overlays?: ReadonlyMap<string, string>, reusable?: readonly string[]): CheckResult;
70
76
  /** Ids of the active project rules, whose findings only `check` produces. */
71
77
  readonly projectRuleIds: ReadonlySet<string>;
72
78
  /**
package/dist/checker.js CHANGED
@@ -109,12 +109,14 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
109
109
  sliceStarted = performance.now();
110
110
  }
111
111
  },
112
- checkFiles(files, overlays = new Map()) {
112
+ checkFiles(files, overlays = new Map(), reusable = []) {
113
113
  const started = performance.now();
114
114
  const units = backend.reopen(overlays);
115
115
  lastUnits = units;
116
116
  known = undefined;
117
- return runFileRules(config, cwd, units, new Set(files.map(toTypeScriptPath)), started, uncapped === undefined, ignores !== false);
117
+ return runFileRules(config, cwd, units, new Set(files.map(toTypeScriptPath)), started, uncapped === undefined, ignores !== false, cache === false || reusable.length === 0
118
+ ? undefined
119
+ : { cache, files: new Set(reusable.map(toTypeScriptPath)) });
118
120
  },
119
121
  // Read on demand: it consults the rollout file, which a run that ignores rollout
120
122
  // (`plan`, `check --rule`) must not touch.
@@ -202,6 +204,10 @@ export function createChecker({ config, cwd, rules = { kind: "configured" }, cac
202
204
  .sort((a, b) => a.offset - b.offset || (a.label < b.label ? -1 : a.label > b.label ? 1 : 0));
203
205
  },
204
206
  tokens(file) {
207
+ // No plugin colours anything: answered without building or binding a program (an editor
208
+ // asks for tokens as soon as a file opens, before the first check has built one).
209
+ if (!config.plugins.some((plugin) => plugin.editor?.tokens !== undefined))
210
+ return [];
205
211
  const found = lookup(file);
206
212
  if (found === undefined)
207
213
  return [];
package/dist/engine.d.ts CHANGED
@@ -131,12 +131,24 @@ export declare function projectRuleIds(config: Config): ReadonlySet<string>;
131
131
  * rule id, from the summaries of a full check; rules with no reason are left out.
132
132
  */
133
133
  export declare function silentReasons(config: Config, selection: RuleSelection, summaries: ReadonlyMap<string, () => ReadonlyMap<string, Json>>): ReadonlyMap<string, string>;
134
+ /**
135
+ * What a fast pass may take from the cache instead of running the rules: the results the cache
136
+ * holds for each of `files` whose dependency fingerprint and reads are as they were then (the
137
+ * same test as a full pass's). Asking for a fingerprint computes those of the whole program
138
+ * once, so a caller names only the files it expects to be unchanged (an editor's file just opened).
139
+ */
140
+ export type FastReuse = {
141
+ readonly cache: ResultCache;
142
+ readonly files: ReadonlySet<string>;
143
+ };
134
144
  /**
135
145
  * Runs the file rules (not the project rules) over the own files of `units` that are in `files`
136
146
  * and returns findings for those files only. A project rule needs every file's summary, so it
137
- * does not run here. A rule that throws becomes a `failure`, as in `runFilePhase`.
147
+ * does not run here. A rule that throws becomes a `failure`, as in `runFilePhase`. With `reuse`, a
148
+ * rule whose result the cache holds for a reusable file is not run (`FastReuse`): an unchanged
149
+ * file costs no type checking.
138
150
  */
139
- export declare function runFileRules(config: Config, cwd: string, units: readonly ProjectUnit[], files: ReadonlySet<string>, started: number, capped?: boolean, ignores?: boolean): CheckResult;
151
+ export declare function runFileRules(config: Config, cwd: string, units: readonly ProjectUnit[], files: ReadonlySet<string>, started: number, capped?: boolean, ignores?: boolean, reuse?: FastReuse): CheckResult;
140
152
  /**
141
153
  * The markdown behind `stateward explain`, or undefined for an unknown rule id. A lazy rule's
142
154
  * text is in its module, which is loaded for it.
package/dist/engine.js CHANGED
@@ -3,7 +3,7 @@ import { readRollout } from "./rollout.js";
3
3
  import { ruleKey, sha1, summaryOf, summaryValue } from "./cache.js";
4
4
  import { defaultOptions } from "./options.js";
5
5
  import { codeOf, loadRule } from "./lazy.js";
6
- import { IGNORE_EXPLAIN, IGNORE_RULE_ID, applyIgnores, fileDirectives, readFileDirectives, } from "./ignores.js";
6
+ import { IGNORE_EXPLAIN, IGNORE_RULE_ID, applyIgnores, fileDirectives } from "./ignores.js";
7
7
  import { addonsWith, appliedOverrides, ruleIsOn, ruleSetting, ruleSettingWith, } from "./severity.js";
8
8
  import { matchingOverrides } from "./overrides.js";
9
9
  import { defaults } from "./config.js";
@@ -853,12 +853,27 @@ export function silentReasons(config, selection, summaries) {
853
853
  }
854
854
  return reasons;
855
855
  }
856
+ /** The cached results of `file` when its fingerprint and reads hold, else undefined. */
857
+ function holdingResults(unit, file, cache) {
858
+ const entry = cache.files.get(file);
859
+ if (entry === undefined)
860
+ return undefined;
861
+ const fingerprint = unit.fingerprint(file);
862
+ if (fingerprint === undefined || entry.fingerprint !== fingerprint)
863
+ return undefined;
864
+ if (!readsHold(unit, entry.reads))
865
+ return undefined;
866
+ // A copy: the fast pass never writes the cache (what it computes is for these files only).
867
+ return new Map(entry.rules);
868
+ }
856
869
  /**
857
870
  * Runs the file rules (not the project rules) over the own files of `units` that are in `files`
858
871
  * and returns findings for those files only. A project rule needs every file's summary, so it
859
- * does not run here. A rule that throws becomes a `failure`, as in `runFilePhase`.
872
+ * does not run here. A rule that throws becomes a `failure`, as in `runFilePhase`. With `reuse`, a
873
+ * rule whose result the cache holds for a reusable file is not run (`FastReuse`): an unchanged
874
+ * file costs no type checking.
860
875
  */
861
- export function runFileRules(config, cwd, units, files, started, capped = true, ignores = true) {
876
+ export function runFileRules(config, cwd, units, files, started, capped = true, ignores = true, reuse) {
862
877
  // The same rules a full pass runs (rollout included), so the fast pass never shows a rule
863
878
  // the developer has not turned on.
864
879
  const sets = ruleSets(config, { kind: "configured" });
@@ -870,22 +885,33 @@ export function runFileRules(config, cwd, units, files, started, capped = true,
870
885
  for (const file of unit.files) {
871
886
  if (!files.has(file))
872
887
  continue;
873
- unit.bind();
874
- const sourceFile = unit.sourceFile(file);
875
- if (sourceFile === undefined)
876
- continue;
888
+ const results = (reuse?.files.has(file) === true ? holdingResults(unit, file, reuse.cache) : undefined) ??
889
+ new Map();
877
890
  fileCount++;
878
- findings.push(...syntaxFinding(unit, file, cwd), ...importsFinding(unit, file, cwd));
891
+ const syntax = results.get(SYNTAX_RULE_ID)?.findings ?? syntaxFinding(unit, file, cwd);
892
+ const imports = results.get(IMPORTS_RULE_ID)?.findings ?? importsFinding(unit, file, cwd);
893
+ findings.push(...syntax, ...imports);
879
894
  if (ignores) {
880
- const read = readFileDirectives(unit, file);
895
+ const read = fileDirectives(unit, file, results).directives;
881
896
  if (read.length > 0)
882
897
  directives.set(file, read);
883
898
  }
884
899
  const startOfFile = { file, start: 0, end: 0 };
885
- const types = unit.typesFor(file);
900
+ let types;
886
901
  for (const entry of sets.forFile(file)) {
887
902
  if (entry.rule.kind !== "file")
888
903
  continue;
904
+ const result = results.get(entry.key);
905
+ if (result !== undefined) {
906
+ findings.push(...result.findings);
907
+ continue;
908
+ }
909
+ // Rules need parents: bind before the first one runs. A file whose results all come from
910
+ // the cache asks nothing of the program (a recorded one is then never built).
911
+ const sourceFile = boundSourceFile(unit, file);
912
+ if (sourceFile === undefined)
913
+ break;
914
+ types ??= unit.typesFor(file);
889
915
  try {
890
916
  runFileSide(entry.rule, fileContext(entry, sourceFile, types, findings));
891
917
  }
package/dist/ignores.d.ts CHANGED
@@ -44,8 +44,6 @@ export declare function fileDirectives(unit: ProjectUnit, file: string, results:
44
44
  directives: readonly IgnoreDirective[];
45
45
  stored: boolean;
46
46
  };
47
- /** The ignore comments of one file, read now (the fast pass has no cache to keep them in). */
48
- export declare function readFileDirectives(unit: ProjectUnit, file: string): readonly IgnoreDirective[];
49
47
  /** The ids a comment may name, and the ones whose rule ran, for "nothing to ignore" to be a fair judgement. */
50
48
  export type IgnoreScope = {
51
49
  /** Every rule id of the config's plugins, and the ids stateward reports itself. */
package/dist/ignores.js CHANGED
@@ -233,10 +233,6 @@ export function fileDirectives(unit, file, results) {
233
233
  results.set(COMMENTS_KEY, { findings: [], summary: summaryOf(directives) });
234
234
  return { directives, stored: true };
235
235
  }
236
- /** The ignore comments of one file, read now (the fast pass has no cache to keep them in). */
237
- export function readFileDirectives(unit, file) {
238
- return readDirectives(unit, file);
239
- }
240
236
  function distance(a, b) {
241
237
  let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
242
238
  for (let i = 1; i <= a.length; i++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stateward/core",
3
- "version": "1.0.0",
3
+ "version": "1.0.1-rc.1",
4
4
  "description": "The engine of stateward (exclusive states and predictable code paths for TypeScript) and the API for writing your own rules and packs.",
5
5
  "license": "MIT",
6
6
  "repository": {