src/lint/plan.bend

bolt@1.24.0.0 · 577 lines · 0 definitions

1# src/lint/plan: `bolt` (the lint) as a pure planner. It reads a World
2# (src/lint/world.bend) and returns either the questions it still needs
3# answered (`wants`) or the Plan: the lines to print and the exit status
4# (`plan`). The interpreter (src/lint.bend) loops on `wants`, answering each
5# question by IO, until nothing is wanted, then prints `plan`'s lines and
6# exits with its status. The design is docs/rfc/bolt-lint-planner.md.
7#
8# With no files named, the planner walks the tree under `.` as a fold over the
9# World's listings: a directory not listed yet reads as empty and is asked
10# for, so each round of the loop lists one more level of the tree. Once every
11# directory the walk reads is listed, it asks for the text of each source and
12# of every bolt.bend candidate of their directories, each once.
13# A directory's candidates are taken once it is resolved against the World's
14# working directory (Config.home), so a relative path is graded by the same
15# bolt.bend wherever bolt runs from.
16# `wants` never parses a file; `plan` parses and lints once, and reads the
17# settings of each bolt.bend that grades a directory for `setting` (C011). Once every rule
18# has run and each finding is graded, the noqa comments (src/noqa.bend)
19# silence what they name, and then the `noqa` rule reports the comments that
20# silenced nothing.
21import Base
22import bolt@1.24.0.0/src/lazy/lazy.bend as Lazy
23import bolt@1.24.0.0/src/lint/world.bend as W
24import bolt@1.24.0.0/src/finding.bend as F
25import bolt@1.24.0.0/src/src.bend as Src
26import bolt@1.24.0.0/src/role.bend as Role
27import bolt@1.24.0.0/src/rules.bend as Rules
28import bolt@1.24.0.0/src/rules/imports.bend as Imports
29import bolt@1.24.0.0/src/config.bend as Config
30import bolt@1.24.0.0/src/noqa.bend as Noqa
31import bolt@1.24.0.0/src/rules/style/noqa.bend as NoqaRule
32import bolt@1.24.0.0/src/rules/correctness/setting.bend as Setting
33
34# what `bolt` prints, in order, and the status it exits with
35type Plan is Data:
36  Plan{lines: List<&2, String>, exit: U32}
37
38# the lines a plan prints
39def lines.of(pl: Plan) -> List<&2, String>:
40  Plan{lines, _exit} = pl
41  lines
42
43# the status a plan exits with
44def exit.of(pl: Plan) -> U32:
45  Plan{_lines, exit} = pl
46  exit
47
48# ---------------------------------------------------------------------------
49# the walk: every `.bend` file under `.`, sorted. A hidden name (`.git`,
50# `.gate`, `.claude`) and `node_modules` are not descended into. The walk keeps
51# a queue of directories rather than nesting, as Bend has no mutual recursion:
52# one def shrinks one argument, the fuel, which bounds how many directories are
53# read.
54
55# a directory the walk never descends into
56def walk.skipped(+name: String) -> Bool:
57  Bool.or(String.starts_with(name, "."), String.eq(name, "node_modules"))
58
59# an entry's name without the "/" that marks it a directory
60def walk.bare(+name: String) -> String:
61  String.take(name, Nat.sub(String.length(name), 1n))
62
63# a name directly under a directory, as a path
64def walk.under(+dir: String, +name: String) -> String:
65  dir ++ "/" ++ name
66
67# the sub-directories of a listing worth descending into, as paths
68def walk.dirs(es: List<&2, String>, +dir: String) -> List<&2, String>:
69  match es:
70    case Nil{}:
71      Nil{}
72    case Con{+e, rest}:
73      +more = walk.dirs(rest, dir)
74      +keep = Bool.and(String.ends_with(e, "/"), Bool.not(walk.skipped(walk.bare(e))))
75      Bool.pick(List<&2, String>, keep, walk.under(dir, walk.bare(e)) <> more, more)
76
77# the `.bend` files of a listing, as paths (a directory's name ends in "/")
78def walk.bends(es: List<&2, String>, +dir: String) -> List<&2, String>:
79  match es:
80    case Nil{}:
81      Nil{}
82    case Con{+e, rest}:
83      +more = walk.bends(rest, dir)
84      Bool.pick(List<&2, String>, String.ends_with(e, ".bend"), walk.under(dir, e) <> more, more)
85
86# a walk once it stops: the `.bend` files it found, and every directory it
87# read, the last one read first
88type Walk is Data:
89  Walk{found: List<&2, String>, read: List<&2, String>}
90
91# the files a walk found
92def walk.found(wk: Walk) -> List<&2, String>:
93  Walk{found, _read} = wk
94  found
95
96# the directories a walk read, in the order it read them
97def walk.read(wk: Walk) -> List<&2, String>:
98  Walk{_found, read} = wk
99  List.reverse(&2, String, read)
100
101# every `.bend` file under the directories still queued, the fuel bounding how
102# many directories are read, each read through the World's listing. What is
103# found is put in front of what came before, not after it, so the walk stays
104# linear; `files` sorts at the end regardless.
105def walk(
106  fuel: Nat,
107  +world: W.World,
108  queue: List<&2, String>,
109  acc: List<&2, String>,
110  seen: List<&2, String>
111) -> Walk:
112  match fuel:
113    case 0n:
114      Walk{acc, seen}
115    case 1n+f:
116      match queue:
117        case Nil{}:
118          Walk{acc, seen}
119        case Con{+dir, rest}:
120          +es = W.listing(world, dir)
121          walk(f, world, List.append(&2, String, rest, walk.dirs(es, dir)),
122            List.append(&2, String, walk.bends(es, dir), acc), dir <> seen)
123
124# how many directories the walk reads at most; past it the walk stops,
125# silently. A literal, not `U32.to_nat(100000)`: the checker keeps a literal
126# whole and steps it one at a time, so laws/ScopeWalksWholeTree.bend's law can name the bound,
127# where it would expand the call in unary and overflow. The compiler emits
128# the same `U32.to_nat` for either.
129def limit() -> Nat:
130  100000n
131
132# the walk of the tree under `.`, reading at most `limit()` directories
133def tree(+world: W.World) -> Walk:
134  walk(limit(), world, ["."], [], [])
135
136# ---------------------------------------------------------------------------
137# the files of the run
138
139# the files named, or every `.bend` file the walk finds when none were
140def files.named(paths: List<&2, String>, +world: W.World) -> List<&2, String>:
141  match paths:
142    case Nil{}:
143      +found = walk.found(tree(world))
144      List.sort(~String, ~String.is_le, found)
145    case Con{p, rest}:
146      p <> rest
147
148# the files the lint runs over
149def files(+world: W.World) -> List<&2, String>:
150  files.named(W.paths.of(world), world)
151
152# a bolt.bend is configuration, not source
153def is_config(+pp: String) -> Bool:
154  Bool.or(String.eq(pp, "bolt.bend"), String.ends_with(pp, "/bolt.bend"))
155
156# the paths that are source
157def sources(ps: List<&2, String>) -> List<&2, String>:
158  match ps:
159    case Nil{}:
160      Nil{}
161    case Con{+p, rest}:
162      +more = sources(rest)
163      Bool.pick(List<&2, String>, is_config(p), more, p <> more)
164
165# the source files of the run, in file-list order
166def inputs(+world: W.World) -> List<&2, String>:
167  sources(files(world))
168
169# ---------------------------------------------------------------------------
170# what the plan reads
171
172# whether a path is among the paths
173def seen.has(ps: List<&2, String>, +pp: String) -> Bool:
174  match ps:
175    case Nil{}:
176      False{}
177    case Con{p, rest}:
178      Lazy.or_else(String.eq(p, pp), _u => seen.has(rest, pp))
179
180# each path once, where it first appears
181def uniq(ps: List<&2, String>, +seen: List<&2, String>) -> List<&2, String>:
182  match ps:
183    case Nil{}:
184      Nil{}
185    case Con{+p, rest}:
186      +dup = seen.has(seen, p)
187      +more = uniq(rest, p <> seen)
188      Bool.pick(List<&2, String>, dup, more, p <> more)
189
190# ---------------------------------------------------------------------------
191# the packages (laws/ScopePackageRootsFound.bend): every directory the walk reads but `.` whose
192# listing names a main.bend, in the order the walk reads them; none when files
193# were named
194
195# the directories the walk read that hold a main.bend, `.` left out
196def roots.of(ds: List<&2, String>, +world: W.World) -> List<&2, String>:
197  match ds:
198    case Nil{}:
199      Nil{}
200    case Con{+d, rest}:
201      +more = roots.of(rest, world)
202      +root = Bool.and(Bool.not(String.eq(d, ".")), seen.has(W.listing(world, d), "main.bend"))
203      Bool.pick(List<&2, String>, root, d <> more, more)
204
205# the package roots but `.`, as the walk names them (`./w3c`)
206def roots.named(paths: List<&2, String>, +world: W.World) -> List<&2, String>:
207  match paths:
208    case Nil{}:
209      roots.of(walk.read(tree(world)), world)
210    case Con{_p, _rest}:
211      Nil{}
212
213# the package roots but `.`
214def roots(+world: W.World) -> List<&2, String>:
215  roots.named(W.paths.of(world), world)
216
217# each package root as the project rules take it: its directory as a digest
218# names one (`w3c/`)
219def packages(rs: List<&2, String>) -> List<&2, String>:
220  match rs:
221    case Nil{}:
222      Nil{}
223    case Con{r, rest}:
224      Imports.dir_of(r ++ "/main.bend") <> packages(rest)
225
226# the directory of each path
227def dirs.of(ps: List<&2, String>) -> List<&2, String>:
228  match ps:
229    case Nil{}:
230      Nil{}
231    case Con{p, rest}:
232      Config.dir_of(p) <> dirs.of(rest)
233
234# the directory of every source, each once: every finding's path is a
235# source's, so these are the directories grading needs
236def conf.dirs(+ps: List<&2, String>) -> List<&2, String>:
237  uniq(dirs.of(ps), [])
238
239# the bolt.bend candidates of a directory as a path names it, nearest first:
240# the directory resolved against the working directory, then each parent up to
241# the root
242def conf.chain(+world: W.World, dir: String) -> List<&2, String>:
243  Config.chain(Config.home(W.cwd.of(world), dir))
244
245# the bolt.bend candidates of each directory, nearest first
246def conf.cands(ds: List<&2, String>, +world: W.World) -> List<&2, String>:
247  match ds:
248    case Nil{}:
249      Nil{}
250    case Con{d, rest}:
251      List.append(&2, String, conf.chain(world, d), conf.cands(rest, world))
252
253# the listing of each directory, as a question
254def asks.entries(ds: List<&2, String>) -> List<&2, W.Ask>:
255  match ds:
256    case Nil{}:
257      Nil{}
258    case Con{d, rest}:
259      W.Entries{d} <> asks.entries(rest)
260
261# the text of each path, as a question
262def asks.texts(ps: List<&2, String>) -> List<&2, W.Ask>:
263  match ps:
264    case Nil{}:
265      Nil{}
266    case Con{p, rest}:
267      W.Contents{p} <> asks.texts(rest)
268
269# the directories the walk lists: every one it reads, none when files were
270# named
271def lists.named(paths: List<&2, String>, +world: W.World) -> List<&2, W.Ask>:
272  match paths:
273    case Nil{}:
274      asks.entries(walk.read(tree(world)))
275    case Con{_p, _rest}:
276      Nil{}
277
278# the listings the plan reads
279def lists(+world: W.World) -> List<&2, W.Ask>:
280  lists.named(W.paths.of(world), world)
281
282# the texts the plan reads: each source and every bolt.bend candidate of their
283# directories, each once
284def texts(+world: W.World) -> List<&2, W.Ask>:
285  +ps = inputs(world)
286  asks.texts(uniq(List.concat(&2, String, [ps, conf.cands(conf.dirs(ps), world)]),
287    []))
288
289# every question the plan reads the World through: the listings, then the
290# texts
291def looks(+world: W.World) -> List<&2, W.Ask>:
292  List.append(&2, W.Ask, lists(world), texts(world))
293
294# the questions a World does not answer yet
295def unanswered(asks: List<&2, W.Ask>, +world: W.World) -> List<&2, W.Ask>:
296  match asks:
297    case Nil{}:
298      Nil{}
299    case Con{+a, rest}:
300      +more = unanswered(rest, world)
301      Bool.pick(List<&2, W.Ask>, W.answered(world, a), more, a <> more)
302
303# the listings still open, or, once there are none, the texts still open
304def wants.pick(ls: List<&2, W.Ask>, +world: W.World) -> List<&2, W.Ask>:
305  match ls:
306    case Nil{}:
307      unanswered(texts(world), world)
308    case Con{a, rest}:
309      a <> rest
310
311# the questions a World still leaves open. The file list is known only once
312# every directory the walk reads is listed, so the texts wait for the walk.
313def wants(+world: W.World) -> List<&2, W.Ask>:
314  wants.pick(unanswered(lists(world), world), world)
315
316# ---------------------------------------------------------------------------
317# the findings
318
319# a source path and the text the World holds for it
320type Got is Data:
321  Got{path: String, text: Maybe<&2, String>}
322
323# each source with what the World answers for it
324def gots(ps: List<&2, String>, +world: W.World) -> List<&2, Got>:
325  match ps:
326    case Nil{}:
327      Nil{}
328    case Con{+p, rest}:
329      Got{p, W.text(world, p)} <> gots(rest, world)
330
331# one finding for each source that could not be read
332def unread(gs: List<&2, Got>) -> List<&2, F.Finding>:
333  match gs:
334    case Nil{}:
335      Nil{}
336    case Con{Got{+path, None{}}, rest}:
337      F.Finding{path, 0, 0, 0, "read", "Cannot read this file."} <> unread(rest)
338    case Con{Got{_path, Some{_text}}, rest}:
339      unread(rest)
340
341# every source that was read, parsed once for every rule
342def srcs_of(gs: List<&2, Got>) -> List<&2, Src.Src>:
343  match gs:
344    case Nil{}:
345      Nil{}
346    case Con{Got{_path, None{}}, rest}:
347      srcs_of(rest)
348    case Con{Got{path, Some{text}}, rest}:
349      Src.of(path, text) <> srcs_of(rest)
350
351# every source's findings from the per-file rules, each told its role among
352# the marks (src/role.bend)
353def per_file.role(+ms: List<&2, Role.Mark>, ss: List<&2, Src.Src>) -> List<&2, F.Finding>:
354  match ss:
355    case Nil{}:
356      Nil{}
357    case Con{s, rest}:
358      Src.Src{+path, text, toks, tree, bound, items} = s
359      List.concat(&2, F.Finding, [Rules.on.role(Role.of(ms, path), Src.Src{path, text, toks, tree, bound, items}),
360        per_file.role(ms, rest)])
361
362# every source's findings from the per-file rules: a file a LAWS.bend or a
363# PROOF.bend imports is a law or proof file to them as those are
364def per_file(+ss: List<&2, Src.Src>) -> List<&2, F.Finding>:
365  per_file.role(Role.marks(ss), ss)
366
367# the path of the first candidate the World holds a text for, as a list of
368# one; none when it holds none
369def conf.first(gs: List<&2, Got>) -> List<&2, String>:
370  match gs:
371    case Nil{}:
372      Nil{}
373    case Con{Got{path, Some{_text}}, _rest}:
374      [path]
375    case Con{Got{_path, None{}}, rest}:
376      conf.first(rest)
377
378# the bolt.bend that grades each directory, in directory order: the first of
379# its candidates the World read, as conf.near reads it; none for a directory
380# graded by the defaults
381def conf.used(ds: List<&2, String>, +world: W.World) -> List<&2, String>:
382  match ds:
383    case Nil{}:
384      Nil{}
385    case Con{d, rest}:
386      List.append(&2, String, conf.first(gots(conf.chain(world, d), world)), conf.used(rest, world))
387
388# every bolt.bend grading reads, each once, in the order its directories come
389def graders(+world: W.World) -> List<&2, String>:
390  uniq(conf.used(conf.dirs(inputs(world)), world), [])
391
392# the `setting` rule's findings on each bolt.bend the World read, in order
393def settings.at(gs: List<&2, Got>) -> List<&2, F.Finding>:
394  match gs:
395    case Nil{}:
396      Nil{}
397    case Con{Got{+path, Some{text}}, rest}:
398      List.append(&2, F.Finding, Setting.check(path, text), settings.at(rest))
399    case Con{Got{_path, None{}}, rest}:
400      settings.at(rest)
401
402# the `setting` rule's findings: each unknown setting of every bolt.bend
403# grading reads (src/rules/correctness/setting.bend)
404def settings(+world: W.World) -> List<&2, F.Finding>:
405  settings.at(gots(graders(world), world))
406
407# every finding of every rule, ungraded, from the sources and what the World
408# answers for them: the read failures, the per-file findings of each source,
409# then `setting`'s over each bolt.bend grading reads, then the project rules'
410# over every source and the other packages
411def found.at(+world: W.World, +gs: List<&2, Got>, +ss: List<&2, Src.Src>) -> List<&2, F.Finding>:
412  List.concat(&2, F.Finding, [unread(gs), per_file(ss), settings(world),
413    Rules.project(ss, packages(roots(world)))])
414
415# every finding of every rule, ungraded
416def found(+world: W.World) -> List<&2, F.Finding>:
417  +gs = gots(inputs(world), world)
418  found.at(world, gs, srcs_of(gs))
419
420# ---------------------------------------------------------------------------
421# grading
422
423# a directory and the config its findings are graded by
424type Conf is Data:
425  Conf{dir: String, config: Config.Config}
426
427# what the World holds at each path
428def texts.at(ps: List<&2, String>, +world: W.World) -> List<&2, Maybe<&2, String>>:
429  match ps:
430    case Nil{}:
431      Nil{}
432    case Con{p, rest}:
433      W.text(world, p) <> texts.at(rest, world)
434
435# a directory's config: the nearest bolt.bend the World could read among its
436# candidates, or the defaults
437def conf.near(+world: W.World, dir: String) -> Config.Config:
438  Config.nearest(texts.at(conf.chain(world, dir), world))
439
440# each directory's config
441def confs.at(ds: List<&2, String>, +world: W.World) -> List<&2, Conf>:
442  match ds:
443    case Nil{}:
444      Nil{}
445    case Con{+d, rest}:
446      Conf{d, conf.near(world, d)} <> confs.at(rest, world)
447
448# the config of every directory grading needs
449def confs(+world: W.World) -> List<&2, Conf>:
450  confs.at(conf.dirs(inputs(world)), world)
451
452# the config of a directory, from the table; one the table does not hold is
453# worked out the same way (every finding's directory is in the table, so this
454# never happens in a run)
455def conf.find(cs: List<&2, Conf>, +world: W.World, +dir: String) -> Config.Config:
456  match cs:
457    case Nil{}:
458      conf.near(world, dir)
459    case Con{Conf{d, cfg}, rest}:
460      Lazy.stop(Config.Config, String.eq(d, dir), cfg, _u => conf.find(rest, world, dir))
461
462# the config a finding at a path is graded by: its directory's
463def config.at(+cs: List<&2, Conf>, +world: W.World, path: String) -> Config.Config:
464  conf.find(cs, world, Config.dir_of(path))
465
466# every finding graded by the config of its directory, in order, the ones at
467# off dropped
468def grade(+cs: List<&2, Conf>, +world: W.World, fs: List<&2, F.Finding>) -> List<&2, F.Graded>:
469  match fs:
470    case Nil{}:
471      Nil{}
472    case Con{F.Finding{+path, line, col, len, rule, msg}, rest}:
473      cfg = config.at(cs, world, path)
474      List.append(&2, F.Graded, Rules.graded(cfg, [F.Finding{path, line, col, len, rule, msg}]), grade(cs, world, rest))
475
476# ---------------------------------------------------------------------------
477# silencing, then the `noqa` rule
478
479# the `noqa` rule's findings on each file's marks, against the graded
480# findings, each put at its file as a per-file rule's are
481def noqa(ms: List<&2, Noqa.Marks>, +whole: Bool, +gs: List<&2, F.Graded>) -> List<&2, F.Finding>:
482  match ms:
483    case Nil{}:
484      Nil{}
485    case Con{Noqa.Marks{+path, mks}, rest}:
486      List.append(&2, F.Finding, Rules.on.at(path, NoqaRule.check(mks, whole, path, gs)), noqa(rest, whole, gs))
487
488# the graded findings no mark silences, in order, then the `noqa` rule's,
489# graded by the grader given: nothing silences what `noqa` reports
490def after(
491  +ms: List<&2, Noqa.Marks>,
492  +whole: Bool,
493  +gs: List<&2, F.Graded>,
494  grader: List<&2, F.Finding> -> List<&2, F.Graded>
495) -> List<&2, F.Graded>:
496  List.append(&2, F.Graded, Noqa.keep(ms, gs), grader(noqa(ms, whole, gs)))
497
498# every finding of the run, graded, then silenced by the marks of the
499# sources, then `noqa`'s own, which judges a project rule's code only when
500# the run is the whole tree. The sources are parsed once for both.
501def final(+world: W.World) -> List<&2, F.Graded>:
502  +gs = gots(inputs(world), world)
503  +ss = srcs_of(gs)
504  +cs = confs(world)
505  after(Noqa.of_srcs(ss), List.is_empty(&2, String, W.paths.of(world)), grade(cs, world, found.at(world, gs, ss)),
506    fs => grade(cs, world, fs))
507
508# ---------------------------------------------------------------------------
509# the plan
510
511# one more at error, or the count as it was
512def errors.at(ll: Config.Level, more: U32) -> U32:
513  match ll:
514    case Config.Off{}:
515      more
516    case Config.Warn{}:
517      more
518    case Config.Error{}:
519      U32.inc(more)
520
521# how many are errors
522def errors(gs: List<&2, F.Graded>) -> U32:
523  match gs:
524    case Nil{}:
525      0
526    case Con{F.Graded{ll, _ff}, rest}:
527      errors.at(ll, errors(rest))
528
529# one more below error, or the count as it was
530def warnings.at(ll: Config.Level, more: U32) -> U32:
531  match ll:
532    case Config.Off{}:
533      U32.inc(more)
534    case Config.Warn{}:
535      U32.inc(more)
536    case Config.Error{}:
537      more
538
539# how many are not errors
540def warnings(gs: List<&2, F.Graded>) -> U32:
541  match gs:
542    case Nil{}:
543      0
544    case Con{F.Graded{ll, _ff}, rest}:
545      warnings.at(ll, warnings(rest))
546
547# whether any is an error
548def has_error(gs: List<&2, F.Graded>) -> Bool:
549  match gs:
550    case Nil{}:
551      False{}
552    case Con{F.Graded{ll, _ff}, rest}:
553      +more = has_error(rest)
554      Bool.or(Config.is_error(ll), more)
555
556# `1 error`, `2 errors`
557def count(+nn: U32, +what: String) -> String:
558  U32.show(nn) ++ " " ++ what ++ Bool.pick(String, U32.is_eq(nn, 1), "", "s")
559
560# `clean`, or the counts
561def summary(+gs: List<&2, F.Graded>) -> String:
562  Lazy.stop(String, List.is_empty(&2, F.Graded, gs), "clean",
563    _u => count(errors(gs), "error") ++ ", " ++ count(warnings(gs), "warning"))
564
565# every graded finding as a line
566def show_all(gs: List<&2, F.Graded>) -> List<&2, String>:
567  match gs:
568    case Nil{}:
569      Nil{}
570    case Con{g, rest}:
571      F.show_graded(g) <> show_all(rest)
572
573# `bolt` over a World: every graded finding no mark silences as a line, then
574# `noqa`'s, then the summary of those, and exit 1 exactly when one is an error
575def plan(+world: W.World) -> Plan:
576  +gs = final(world)
577  Plan{List.append(&2, String, show_all(gs), [summary(gs)]), Bool.pick(U32, has_error(gs), 1, 0)}