src/rules.bend

bolt@1.19.0.0 · 132 lines · 0 definitions

1# src/rules: every rule, and its group. A rule is a module under
2# rules/<group>/ with `check(src) -> List<Finding>`, run over each file read
3# once (src/src.bend); a project rule has `check(ds) -> List<Finding>` and
4# sees every file at once, as the digests of rules/digest.bend. The directory
5# names the group, and codes.bend says it again for the config.
6# Adding one is adding it here and a row in codes.bend. One rule is not
7# listed here, `noqa` (rules/style/noqa.bend): it reads the other rules'
8# graded findings, so the planner runs it after them (lint/plan.bend). Nor
9# is `setting` (rules/correctness/setting.bend): it reads each bolt.bend that
10# grades the run, not a source, so the planner runs it between the per-file
11# and the project rules.
12# The groups are what a bolt.bend switches at once: correctness (what will
13# fail or blow up), suspicious (what is probably a slip), style, laws, and
14# pedantic (advice that is noisy on idiomatic code: off unless a project
15# asks for it).
16import Base
17import bolt@1.19.0.0/src/finding.bend as F
18import bolt@1.19.0.0/src/codes.bend as Codes
19import bolt@1.19.0.0/src/src.bend as Src
20import bolt@1.19.0.0/src/rules/digest.bend as Digest
21import bolt@1.19.0.0/src/config.bend as Config
22import bolt@1.19.0.0/src/rules/style/doc.bend as Doc
23import bolt@1.19.0.0/src/rules/suspicious/unused.bend as Unused
24import bolt@1.19.0.0/src/rules/correctness/hole.bend as Hole
25import bolt@1.19.0.0/src/rules/style/space.bend as Space
26import bolt@1.19.0.0/src/rules/style/wrap.bend as Wrap
27import bolt@1.19.0.0/src/rules/style/param.bend as Param
28import bolt@1.19.0.0/src/rules/correctness/pick.bend as Pick
29import bolt@1.19.0.0/src/rules/laws/law.bend as Law
30import bolt@1.19.0.0/src/rules/suspicious/strict.bend as Strict
31import bolt@1.19.0.0/src/rules/suspicious/eager.bend as Eager
32import bolt@1.19.0.0/src/rules/pedantic/tail.bend as Tail
33import bolt@1.19.0.0/src/rules/suspicious/concat.bend as Concat
34import bolt@1.19.0.0/src/rules/suspicious/index.bend as Index
35import bolt@1.19.0.0/src/rules/suspicious/table.bend as Table
36import bolt@1.19.0.0/src/rules/suspicious/hoist.bend as Hoist
37import bolt@1.19.0.0/src/rules/suspicious/ring.bend as Ring
38import bolt@1.19.0.0/src/rules/suspicious/rewalk.bend as Rewalk
39import bolt@1.19.0.0/src/rules/suspicious/unit.bend as Unit
40import bolt@1.19.0.0/src/rules/suspicious/scan.bend as Scan
41import bolt@1.19.0.0/src/rules/suspicious/thunk.bend as Thunk
42import bolt@1.19.0.0/src/rules/suspicious/fromrev.bend as Fromrev
43import bolt@1.19.0.0/src/rules/correctness/put.bend as Put
44import bolt@1.19.0.0/src/rules/correctness/escape.bend as Escape
45import bolt@1.19.0.0/src/rules/correctness/strings.bend as Strings
46import bolt@1.19.0.0/src/rules/correctness/chars.bend as Chars
47import bolt@1.19.0.0/src/rules/correctness/twice.bend as Twice
48import bolt@1.19.0.0/src/rules/correctness/arms.bend as Arms
49import bolt@1.19.0.0/src/rules/correctness/foreign.bend as Foreign
50import bolt@1.19.0.0/src/rules/suspicious/fuel.bend as Fuel
51import bolt@1.19.0.0/src/rules/laws/closed.bend as Closed
52import bolt@1.19.0.0/src/rules/laws/trace.bend as Trace
53import bolt@1.19.0.0/src/rules/laws/unsafe.bend as Unsafe
54
55# the file a Src was read from
56def on.path(ss: Src.Src) -> String:
57  Src.Src{path, _text, _toks, _tree, _bound, _items} = ss
58  path
59
60# findings put at a path: a per-file rule reports on its own file, so this
61# keeps what each rule gave and makes it hold by construction
62def on.at(+path: String, fs: List<&2, F.Finding>) -> List<&2, F.Finding>:
63  match fs:
64    case Nil{}:
65      Nil{}
66    case Con{F.Finding{_path, line, col, len, rule, msg}, rest}:
67      F.Finding{path, line, col, len, rule, msg} <> on.at(path, rest)
68
69# every rule's findings on a file read once, as each rule gives them
70def on.rules(+ss: Src.Src) -> List<&2, F.Finding>:
71  List.concat(&2, F.Finding, [
72    Doc.check(ss),
73    Unused.check(ss),
74    Hole.check(ss),
75    Space.check(ss),
76    Wrap.check(ss),
77    Param.check(ss),
78    Pick.check(ss),
79    Strict.check(ss),
80    Eager.check(ss),
81    Tail.check(ss),
82    Concat.check(ss),
83    Index.check(ss),
84    Table.check(ss),
85    Hoist.check(ss),
86    Ring.check(ss),
87    Rewalk.check(ss),
88    Unit.check(ss),
89    Scan.check(ss),
90    Thunk.check(ss),
91    Fromrev.check(ss),
92    Put.check(ss),
93    Escape.check(ss),
94    Strings.check(ss),
95    Chars.check(ss),
96    Twice.check(ss),
97    Arms.check(ss),
98    Foreign.check(ss),
99    Fuel.check(ss),
100    Closed.check(ss)])
101
102# every rule's findings on a file read once, each at that file
103def on(+ss: Src.Src) -> List<&2, F.Finding>:
104  on.at(on.path(ss), on.rules(ss))
105
106# every rule's findings on a file
107def all(+path: String, +text: String) -> List<&2, F.Finding>:
108  on(Src.of(path, text))
109
110# every project rule's findings on the files and SPEC.md's text (None when
111# there is none): each file is read down to its digest once
112# (rules/digest.bend), and the rules run over those
113def project(srcs: List<&2, Src.Src>, whole: Bool, spec: Maybe<&2, String>) -> List<&2, F.Finding>:
114  +ds = Digest.all(srcs)
115  List.concat(&2, F.Finding, [Law.check(ds), Unsafe.check(ds), Trace.check(whole, spec, ds)])
116
117# the group a rule is in; an unknown slug grades with correctness
118def group(+rule: String) -> String:
119  +named = Codes.group(rule)
120  Bool.pick(String, String.is_empty(named), "correctness", named)
121
122# the findings at the levels a config gives their rules; the ones set off
123# are dropped
124def graded(+cfg: Config.Config, fs: List<&2, F.Finding>) -> List<&2, F.Graded>:
125  match fs:
126    case Nil{}:
127      Nil{}
128    case Con{F.Finding{path, line, col, len, +rule, msg}, rest}:
129      +more = graded(cfg, rest)
130      +l = Config.level(cfg, group(rule), rule)
131      Bool.pick(List<&2, F.Graded>, Config.is_off(l), more,
132        F.Graded{l, F.Finding{path, line, col, len, rule, msg}} <> more)