src/rules.bend

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