bend-lint 0.3.0 → 0.3.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/README.md CHANGED
@@ -131,8 +131,8 @@ A rule is a file in `bend-lint-rules/`. It imports bend-lint's rule API from Ben
131
131
 
132
132
  ```
133
133
  import Base
134
- import bend-lint-api@0.3.0.0/linter/api/impl.bend as LintImpl
135
- import bend-lint-api@0.3.0.0/linter/api/spec.bend as Lint
134
+ import bend-lint-api@0.3.2.0/linter/api/impl.bend as LintImpl
135
+ import bend-lint-api@0.3.2.0/linter/api/spec.bend as Lint
136
136
 
137
137
  def rule() -> Lint.Rule:
138
138
  ...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bend-lint",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Diagnostics for Bend 2 from rules outside the language: source text, and the checker's types",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -14,8 +14,8 @@
14
14
  "engines": {
15
15
  "bun": "1.4.4"
16
16
  },
17
- "bendRelease": "bendlang/bend/v2.0.39",
18
- "bendWindows": "MattCozendey/bend/d38038df4c456f5c8421f3630787f6de91d6c724",
17
+ "bendRelease": "bendlang/bend/v2.0.40",
18
+ "bendWindows": "MattCozendey/bend/cbb3959663552a2ff1f437c5b2f10c945651f816",
19
19
  "scripts": {
20
20
  "postinstall": "bun ./src/linter/seam.ts postinstall"
21
21
  }
@@ -1,8 +1,8 @@
1
1
  # The program seam.ts writes from bend-lint.json calls `run` with its rules: `Cli.run(~[Cli.Rule{"x", X.rule()}])`.
2
2
  import Base
3
- import bend-lint-api@0.3.0.0/shared.bend as Shared
4
- import bend-lint-api@0.3.0.0/linter/api/impl.bend as LintImpl
5
- import bend-lint-api@0.3.0.0/linter/api/spec.bend as Lint
3
+ import bend-lint-api@0.3.2.0/shared.bend as Shared
4
+ import bend-lint-api@0.3.2.0/linter/api/impl.bend as LintImpl
5
+ import bend-lint-api@0.3.2.0/linter/api/spec.bend as Lint
6
6
  import ./directives/impl.bend as DirectivesImpl
7
7
  import ./directives/spec.bend as Directives
8
8
  import ./shared/files/impl.bend as FilesImpl
@@ -1,7 +1,7 @@
1
1
  import Base
2
- import bend-lint-api@0.3.0.0/shared.bend as Shared
3
- import bend-lint-api@0.3.0.0/linter/api/impl.bend as LintImpl
4
- import bend-lint-api@0.3.0.0/linter/api/spec.bend as Lint
2
+ import bend-lint-api@0.3.2.0/shared.bend as Shared
3
+ import bend-lint-api@0.3.2.0/linter/api/impl.bend as LintImpl
4
+ import bend-lint-api@0.3.2.0/linter/api/spec.bend as Lint
5
5
  import ../shared/text/impl.bend as TextImpl
6
6
  import ../shared/text/spec.bend as Text
7
7
  import ./spec.bend as Spec
@@ -1,6 +1,6 @@
1
1
  # Directives: the `# bend-lint:` comments that disable or expect findings.
2
2
  import Base
3
- import bend-lint-api@0.3.0.0/linter/api/spec.bend as Lint
3
+ import bend-lint-api@0.3.2.0/linter/api/spec.bend as Lint
4
4
 
5
5
  # A comment: where it is (code points), its line (from 0), whether code comes before it on its line, and its text
6
6
  # after the #.
@@ -169,7 +169,8 @@ type Note = { role: "t" | "b"; type: Typed; quantity: Quant };
169
169
 
170
170
  // A file the check parsed: its path, its text on disk and as its parser read it (with its import
171
171
  // lines blank), its namespace and its import aliases (in order), the parser that read its
172
- // declarations, the loads of its imports, and the calls that parsed its declarations.
172
+ // declarations, the loads of its imports, the calls that parsed its declarations, and where its
173
+ // declarations start in the book's order.
173
174
  type Parsed = {
174
175
  path: string;
175
176
  text: string;
@@ -179,8 +180,13 @@ type Parsed = {
179
180
  parser: unknown;
180
181
  loads: Frame[];
181
182
  frames: Frame[];
183
+ start: number;
182
184
  };
183
185
 
186
+ // A file a check parsed, with its key (the checker's version, the file's path and text, and its
187
+ // imports' keys) and its closure (itself, the files it imports, and Base, as "").
188
+ type Keyed = { file: Parsed; key: string; closure: Set<string> };
189
+
184
190
  // Where the spans a file's parser made are on disk: the parser, the text on disk, and, for an
185
191
  // offset the parser read, where it is on disk in UTF-16 units; and, for one of those, in code
186
192
  // points.
@@ -479,6 +485,10 @@ const FILES = new Map<unknown, number>();
479
485
  const ANSWERED: Answered[] = [];
480
486
  const NUMBERS = new Map<string, number>();
481
487
 
488
+ // The records of a file that no fact is on (see `getCheck`), by its key and namespace: each check
489
+ // that parses the file so gives the same.
490
+ const RECORDS = new Map<string, string[]>();
491
+
482
492
  const STALE = 7 * 24 * 60 * 60 * 1000;
483
493
 
484
494
  // Touching an executable makes a virus scanner check it again before it runs (about 0.2 s on
@@ -995,6 +1005,7 @@ const getParsedFiles = (frames: Frame[], out: Parsed[]): Parsed[] => {
995
1005
  parser: book.kids.find((f) => isFile(f.args[0]))?.args[0],
996
1006
  loads,
997
1007
  frames: book.kids,
1008
+ start: load.out as number,
998
1009
  });
999
1010
  }
1000
1011
  });
@@ -1324,7 +1335,7 @@ const getDeclarationTrees = (b: Builder, [head, ...frames]: Frame[]): Tree[] =>
1324
1335
  : [getDeclarationAt(b, "Definition", name, getDeclarationParts(b, head.kids))];
1325
1336
  }
1326
1337
  const syntax: Syntax =
1327
- head.name === "parse_fresh" && head.args[2] === "ctrs"
1338
+ head.name === "parse_fresh" && (head.args[2] === "ctrs" || head.args[2] === b.book.ctrs)
1328
1339
  ? "Constructor"
1329
1340
  : b.book.tlds[head.out as string]?.$ === "ADT"
1330
1341
  ? "Datatype"
@@ -1509,42 +1520,51 @@ const isSameType = (Bend: Bend, held: Held, a: Typed, b: Typed): boolean => {
1509
1520
  // entry in the scope of its body has, unless it is a parameter its signature declared (a law's fill
1510
1521
  // takes the law's); it is used as often as its judgment closed it. Of a node's facts that the
1511
1522
  // checker counts the same (same role, quantity and type), the first stays: pattern compilation
1512
- // checks one term in several branches.
1523
+ // checks one term in several branches. A file's nodes come from `getTrees` (by its parser) once a
1524
+ // fact is read at a span of the file.
1513
1525
  const putFacts = (
1514
1526
  Bend: Bend,
1515
1527
  held: Held,
1516
1528
  own: string[],
1517
1529
  judgments: Map<LTerm, Judgment>,
1518
1530
  wanted: ReadonlySet<string | undefined> | undefined,
1519
- tops: Tree[],
1531
+ getTrees: (file: unknown) => Tree[],
1520
1532
  ): void => {
1521
1533
  const { binders } = held;
1522
1534
  const origins = new Map<string, Tree[]>();
1523
- for (const top of tops) {
1524
- const mine = getPreorder([top], []);
1525
- const declarers = new Map<number, Tree>();
1526
- for (const o of mine) {
1527
- if (o.binder !== undefined && BINDS.has(o.syntax)) {
1528
- declarers.set(o.binder, o);
1529
- }
1535
+ const indexed = new Set<unknown>();
1536
+ const getNodesAt = (s: Span | undefined): Tree[] => {
1537
+ if (s === undefined) {
1538
+ return [];
1530
1539
  }
1531
- for (const o of mine) {
1532
- if (o.syntax === "Variable" && o.binder !== undefined) {
1533
- o.declarer = declarers.get(o.binder);
1534
- }
1535
- if (o.origin !== undefined) {
1536
- const key = getSpanKey(o.origin);
1537
- const known = origins.get(key);
1538
- if (known === undefined) {
1539
- origins.set(key, [o]);
1540
- } else {
1541
- known.push(o);
1540
+ if (!indexed.has(s.file)) {
1541
+ indexed.add(s.file);
1542
+ for (const top of getTrees(s.file)) {
1543
+ const mine = getPreorder([top], []);
1544
+ const declarers = new Map<number, Tree>();
1545
+ for (const o of mine) {
1546
+ if (o.binder !== undefined && BINDS.has(o.syntax)) {
1547
+ declarers.set(o.binder, o);
1548
+ }
1549
+ }
1550
+ for (const o of mine) {
1551
+ if (o.syntax === "Variable" && o.binder !== undefined) {
1552
+ o.declarer = declarers.get(o.binder);
1553
+ }
1554
+ if (o.origin !== undefined) {
1555
+ const key = getSpanKey(o.origin);
1556
+ const known = origins.get(key);
1557
+ if (known === undefined) {
1558
+ origins.set(key, [o]);
1559
+ } else {
1560
+ known.push(o);
1561
+ }
1562
+ }
1542
1563
  }
1543
1564
  }
1544
1565
  }
1545
- }
1546
- const getNodesAt = (s: Span | undefined): Tree[] =>
1547
- s === undefined ? [] : (origins.get(getSpanKey(s)) ?? []);
1566
+ return origins.get(getSpanKey(s)) ?? [];
1567
+ };
1548
1568
  const getBinderNode = (s: Span | undefined, k: string): Tree | undefined =>
1549
1569
  getNodesAt(s).find((o) => BINDS.has(o.syntax) && o.name === k);
1550
1570
  const getEntry = (scope: Scope | undefined, level: number): Ann | undefined =>
@@ -1776,6 +1796,70 @@ const getUntemplated = (x: unknown, bok: Book): unknown => {
1776
1796
  );
1777
1797
  };
1778
1798
 
1799
+ // The mark, in the cache, that a passed check checked the file of `key` as if alone (see
1800
+ // `getValidity`).
1801
+ const getValidPath = (key: string): string => nodePath.join(CHECKS_DIR, key + ".valid");
1802
+
1803
+ // The keys of `files`, in the order Bend parsed them (a file's imports first), for the checker's
1804
+ // `version`.
1805
+ const getKeyed = (version: string, files: Parsed[]): Keyed[] => {
1806
+ const known = new Map<string, Keyed>();
1807
+ return files.map((file) => {
1808
+ const loads = file.loads.flatMap((l) => known.get(l.args[1] as string) ?? []);
1809
+ const one = {
1810
+ file,
1811
+ key: getSha256Hex(
1812
+ JSON.stringify([version, file.path, file.text, ...loads.map((l) => l.key)]),
1813
+ ),
1814
+ closure: new Set(["", file.path, ...loads.flatMap((l) => [...l.closure])]),
1815
+ };
1816
+ known.set(file.path, one);
1817
+ return one;
1818
+ });
1819
+ };
1820
+
1821
+ // What Bend's check of the files `keyed` need not check again, by the book's `order` of
1822
+ // declarations, Base's before `from`. A file is checked as if alone when its closure holds the file
1823
+ // of each fill of a law (a name declared again) before it whose law its closure holds: the checker
1824
+ // unfolds a law from its fill on. `own` gives the key of each such file; `start` the first
1825
+ // declaration of the first file that the check must check: one not checked as if alone, or with no
1826
+ // mark of its key, or whose facts are `wanted` (by its namespace). A use of a mark keeps it from
1827
+ // prune.
1828
+ const getValidity = (
1829
+ keyed: Keyed[],
1830
+ order: string[],
1831
+ from: number,
1832
+ wanted: (ns: string) => boolean,
1833
+ ): { start: number; own: string[] } => {
1834
+ const getOwner = (i: number): string =>
1835
+ keyed.filter((k) => k.file.start <= i).at(-1)?.file.path ?? "";
1836
+ const first = new Map<string, number>();
1837
+ const fills = order.flatMap((k, i) => {
1838
+ const at = first.get(k);
1839
+ if (at === undefined) {
1840
+ first.set(k, i);
1841
+ }
1842
+ return at === undefined || i < from ? [] : [{ at: i, law: getOwner(at), filler: getOwner(i) }];
1843
+ });
1844
+ const isOwn = ({ file, closure }: Keyed): boolean =>
1845
+ fills.every(
1846
+ ({ at, law, filler }) => at >= file.start || closure.has(filler) || !closure.has(law),
1847
+ );
1848
+ return {
1849
+ start:
1850
+ keyed.find(
1851
+ (one) =>
1852
+ !isOwn(one) ||
1853
+ wanted(one.file.ns) ||
1854
+ tryGet(() => {
1855
+ nodeFs.utimesSync(getValidPath(one.key), new Date(), new Date());
1856
+ return true;
1857
+ }) !== true,
1858
+ )?.file.start ?? order.length,
1859
+ own: keyed.filter(isOwn).map((one) => one.key),
1860
+ };
1861
+ };
1862
+
1779
1863
  // The path Bend loads a file at, as `seen` keeps it: real, with `/`; a file with an overlay, at its
1780
1864
  // key.
1781
1865
  const getLoadPath = (path: string): string =>
@@ -1787,7 +1871,8 @@ const getLoadPath = (path: string): string =>
1787
1871
  // and the check's number, here 0 (see `getNumberedAnswer`), or `failed` and each file it read;
1788
1872
  // then each file it parsed, if it loaded them: its path, namespace and text, then its nodes (see
1789
1873
  // `getTreeRecords`). A passed check gives the facts of the files at `facts.paths` (all, if none).
1790
- // The hooks are global, so checks must not overlap.
1874
+ // Bend checks only the declarations that `getValidity` does not skip. The hooks are global, so
1875
+ // checks must not overlap.
1791
1876
  const getCheck = async (checker: Checker, path: string, facts: Wanted): Promise<Made> => {
1792
1877
  const start = performance.now();
1793
1878
  const { Bend, base } = await checker.getLoaded();
@@ -1795,19 +1880,31 @@ const getCheck = async (checker: Checker, path: string, facts: Wanted): Promise<
1795
1880
  const seen = new Map<string, string | null>([[Bend.BASE_BEND, ""]]);
1796
1881
  const root: Frame = { name: "check", args: [], out: undefined, end: -1, kids: [] };
1797
1882
  const judgments = new Map<LTerm, Judgment>();
1798
- let loaded = false;
1883
+ let keyed: Keyed[] = [];
1884
+ let wanted: Set<string | undefined> | undefined;
1799
1885
  let passed = false;
1800
1886
  READS.clear();
1801
1887
  FILES.clear();
1802
1888
  traceParsing(root);
1803
1889
  try {
1804
1890
  await Bend.book_load(book, getLoadPath(path), "", seen);
1805
- loaded = true;
1891
+ keyed = getKeyed(checker.version, getParsedFiles(root.kids, []));
1892
+ wanted =
1893
+ facts === undefined || facts.paths.length === 0
1894
+ ? undefined
1895
+ : new Set(facts.paths.map((p) => seen.get(getLoadPath(p)) ?? undefined));
1806
1896
  if (facts !== undefined) {
1807
1897
  traceJudgments(Bend, judgments);
1808
1898
  }
1809
- Bend.book_valid(book, base.order.length);
1899
+ const validity = getValidity(
1900
+ keyed,
1901
+ book.order,
1902
+ base.order.length,
1903
+ (ns) => facts !== undefined && (wanted === undefined || wanted.has(ns)),
1904
+ );
1905
+ Bend.book_valid(book, validity.start);
1810
1906
  passed = true;
1907
+ validity.own.forEach((key) => tryGet(() => writeNewFileAtomically(getValidPath(key), "")));
1811
1908
  } catch (e) {
1812
1909
  // A check that fails gives no facts; `bend <file> --check-only` says why. What bend.ts throws
1813
1910
  // for the file is an Err; anything else is a fault of the checker or of this glue, not of the
@@ -1818,27 +1915,38 @@ const getCheck = async (checker: Checker, path: string, facts: Wanted): Promise<
1818
1915
  } finally {
1819
1916
  untrace();
1820
1917
  }
1821
- const files = loaded ? getParsedFiles(root.kids, []) : [];
1822
- const forest = files.map((file) => getFileTrees(file, book));
1823
1918
  const held: Held = { bok: book, types: [], binders: new Map() };
1919
+ // The trees of the files that facts are on, by their parsers: facts build them as they need them.
1920
+ const forest = new Map<unknown, Tree[]>();
1824
1921
  if (passed && facts !== undefined) {
1825
1922
  putFacts(
1826
1923
  Bend,
1827
1924
  held,
1828
1925
  [...new Set(book.order.slice(base.order.length))],
1829
1926
  judgments,
1830
- facts.paths.length === 0
1831
- ? undefined
1832
- : new Set(facts.paths.map((p) => seen.get(getLoadPath(p)) ?? undefined)),
1833
- forest.flat(),
1927
+ wanted,
1928
+ (parser) => {
1929
+ const file = keyed.find((k) => k.file.parser === parser)?.file;
1930
+ const trees = forest.get(parser) ?? (file === undefined ? [] : getFileTrees(file, book));
1931
+ forest.set(parser, trees);
1932
+ return trees;
1933
+ },
1834
1934
  );
1835
1935
  }
1836
- const program = files.flatMap((file, n) =>
1837
- forest[n].reduce(
1936
+ const getRecords = (file: Parsed, trees: Tree[]): string[] =>
1937
+ trees.reduce(
1838
1938
  (out, t) => getTreeRecords(t, held, out),
1839
1939
  [getRecord(["file", file.path, file.ns, file.text])],
1840
- ),
1841
- );
1940
+ );
1941
+ const program = keyed.flatMap(({ file, key }) => {
1942
+ const trees = forest.get(file.parser);
1943
+ if (trees !== undefined) {
1944
+ return getRecords(file, trees);
1945
+ }
1946
+ const kept = RECORDS.get(key + " " + file.ns) ?? getRecords(file, getFileTrees(file, book));
1947
+ RECORDS.set(key + " " + file.ns, kept);
1948
+ return kept;
1949
+ });
1842
1950
  const read = [...READS];
1843
1951
  COUNTS.checkerMs += performance.now() - start;
1844
1952
  return passed
@@ -2076,16 +2184,19 @@ const getBuiltProgram = async (source: string, dir: URL, waits: boolean): Promis
2076
2184
  // The answer to `check <path> <facts|syntax> <paths...>`: from the cache, if each file the check
2077
2185
  // read is unchanged, else from Bend, and a passed check goes to the cache (whole or not at all; a
2078
2186
  // cache that cannot be written is skipped). A passed check is numbered as before in this server,
2079
- // or anew. What it holds is large, and only a question reads it, so it holds nothing.
2187
+ // or anew. A check that Bend made holds what it holds until `forget`, for the questions of its
2188
+ // rules; one from the cache holds nothing.
2080
2189
  const getCheckAnswer = async (checker: Checker, path: string, facts: Wanted): Promise<string> => {
2081
2190
  const file = getCachePath(checker, path, facts);
2082
2191
  let entry = getCachedEntry(file);
2192
+ let held: Held | undefined;
2083
2193
  COUNTS[entry === undefined ? "checked" : "cached"] += 1;
2084
2194
  if (entry === undefined) {
2085
2195
  const made = await getCheck(checker, path, facts);
2086
2196
  if (made.held === undefined) {
2087
2197
  return made.answer;
2088
2198
  }
2199
+ held = made.held;
2089
2200
  entry = {
2090
2201
  files: made.read.map(([p, text]) => [p, getSha256Hex(text)]),
2091
2202
  answer: made.answer,
@@ -2099,6 +2210,7 @@ const getCheckAnswer = async (checker: Checker, path: string, facts: Wanted): Pr
2099
2210
  known !== undefined && ANSWERED[known - 1].digest === entry.digest
2100
2211
  ? known
2101
2212
  : ANSWERED.push({ file, path, facts, digest: entry.digest, types: entry.types });
2213
+ ANSWERED[number - 1].held ??= held;
2102
2214
  NUMBERS.set(file, number);
2103
2215
  return getNumberedAnswer(entry.answer, number);
2104
2216
  };
@@ -1,5 +1,5 @@
1
1
  import Base
2
- import bend-lint-api@0.3.0.0/shared.bend as Shared
2
+ import bend-lint-api@0.3.2.0/shared.bend as Shared
3
3
  import ./spec.bend as Spec
4
4
 
5
5
  # Types
@@ -1,7 +1,7 @@
1
1
  import Base
2
- import bend-lint-api@0.3.0.0/shared.bend as Shared
3
- import bend-lint-api@0.3.0.0/linter/api/impl.bend as LintImpl
4
- import bend-lint-api@0.3.0.0/linter/api/spec.bend as Lint
2
+ import bend-lint-api@0.3.2.0/shared.bend as Shared
3
+ import bend-lint-api@0.3.2.0/linter/api/impl.bend as LintImpl
4
+ import bend-lint-api@0.3.2.0/linter/api/spec.bend as Lint
5
5
  import ../files/impl.bend as FilesImpl
6
6
  import ../files/spec.bend as Files
7
7
  import ../text/impl.bend as TextImpl
@@ -1,6 +1,6 @@
1
1
  # The fixes a run makes with `--fix-dangerously`: no fix is proven to keep the program until Bend's checker is Bend.
2
2
  import Base
3
- import bend-lint-api@0.3.0.0/linter/api/spec.bend as Lint
3
+ import bend-lint-api@0.3.2.0/linter/api/spec.bend as Lint
4
4
  import ../text/spec.bend as Text
5
5
 
6
6
  # The text of each of `sources`, by its path, onto `out`.
@@ -1,5 +1,5 @@
1
1
  import Base
2
- import bend-lint-api@0.3.0.0/shared.bend as Shared
2
+ import bend-lint-api@0.3.2.0/shared.bend as Shared
3
3
  import ./spec.bend as Spec
4
4
 
5
5
  # Types
@@ -1,5 +1,5 @@
1
1
  import Base
2
- import bend-lint-api@0.3.0.0/shared.bend as Shared
2
+ import bend-lint-api@0.3.2.0/shared.bend as Shared
3
3
  import ./spec.bend as Spec
4
4
 
5
5
  # By place: a balanced (AVL) tree, by start, then end; `height` counts its levels. As spans never touch, their ends