src/rules.bend
bolt@1.24.0.0 · 208 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 many files at once, as the digests of rules/digest.bend: `coverage`
5# one package's files, `unsafe` every file (`project` below). The directory
6# names the group, and codes.bend says it again for the config.
7# Adding one is adding it here and a row in codes.bend. One rule is not
8# listed here, `noqa` (rules/style/noqa.bend): it reads the other rules'
9# graded findings, so the planner runs it after them (lint/plan.bend). Nor
10# is `setting` (rules/correctness/setting.bend): it reads each bolt.bend that
11# grades the run, not a source, so the planner runs it between the per-file
12# and the project rules.
13# The groups are what a bolt.bend switches at once: correctness (what will
14# fail or blow up), suspicious (what is probably a slip), style, laws, and
15# pedantic (advice that is noisy on idiomatic code: off unless a project
16# asks for it).
17import Base
18import bolt@1.24.0.0/src/finding.bend as F
19import bolt@1.24.0.0/src/codes.bend as Codes
20import bolt@1.24.0.0/src/src.bend as Src
21import bolt@1.24.0.0/src/role.bend as Role
22import bolt@1.24.0.0/src/rules/digest.bend as Digest
23import bolt@1.24.0.0/src/rules/imports.bend as Imports
24import bolt@1.24.0.0/src/lazy/lazy.bend as Lazy
25import bolt@1.24.0.0/src/config.bend as Config
26import bolt@1.24.0.0/src/rules/style/doc.bend as Doc
27import bolt@1.24.0.0/src/rules/suspicious/unused.bend as Unused
28import bolt@1.24.0.0/src/rules/correctness/hole.bend as Hole
29import bolt@1.24.0.0/src/rules/style/space.bend as Space
30import bolt@1.24.0.0/src/rules/style/wrap.bend as Wrap
31import bolt@1.24.0.0/src/rules/style/param.bend as Param
32import bolt@1.24.0.0/src/rules/style/transcong.bend as Transcong
33import bolt@1.24.0.0/src/rules/correctness/pick.bend as Pick
34import bolt@1.24.0.0/src/rules/laws/law.bend as Law
35import bolt@1.24.0.0/src/rules/suspicious/strict.bend as Strict
36import bolt@1.24.0.0/src/rules/suspicious/eager.bend as Eager
37import bolt@1.24.0.0/src/rules/pedantic/tail.bend as Tail
38import bolt@1.24.0.0/src/rules/suspicious/concat.bend as Concat
39import bolt@1.24.0.0/src/rules/suspicious/index.bend as Index
40import bolt@1.24.0.0/src/rules/suspicious/table.bend as Table
41import bolt@1.24.0.0/src/rules/suspicious/hoist.bend as Hoist
42import bolt@1.24.0.0/src/rules/suspicious/ring.bend as Ring
43import bolt@1.24.0.0/src/rules/suspicious/rewalk.bend as Rewalk
44import bolt@1.24.0.0/src/rules/suspicious/unit.bend as Unit
45import bolt@1.24.0.0/src/rules/suspicious/scan.bend as Scan
46import bolt@1.24.0.0/src/rules/suspicious/thunk.bend as Thunk
47import bolt@1.24.0.0/src/rules/suspicious/fromrev.bend as Fromrev
48import bolt@1.24.0.0/src/rules/correctness/put.bend as Put
49import bolt@1.24.0.0/src/rules/correctness/escape.bend as Escape
50import bolt@1.24.0.0/src/rules/correctness/strings.bend as Strings
51import bolt@1.24.0.0/src/rules/correctness/chars.bend as Chars
52import bolt@1.24.0.0/src/rules/correctness/twice.bend as Twice
53import bolt@1.24.0.0/src/rules/correctness/arms.bend as Arms
54import bolt@1.24.0.0/src/rules/correctness/foreign.bend as Foreign
55import bolt@1.24.0.0/src/rules/suspicious/fuel.bend as Fuel
56import bolt@1.24.0.0/src/rules/laws/closed.bend as Closed
57import bolt@1.24.0.0/src/rules/laws/unsafe.bend as Unsafe
58
59# the file a Src was read from
60def on.path(ss: Src.Src) -> String:
61 Src.Src{path, _text, _toks, _tree, _bound, _items} = ss
62 path
63
64# findings put at a path: a per-file rule reports on its own file, so this
65# keeps what each rule gave and makes it hold by construction
66def on.at(+path: String, fs: List<&2, F.Finding>) -> List<&2, F.Finding>:
67 match fs:
68 case Nil{}:
69 Nil{}
70 case Con{F.Finding{_path, line, col, len, rule, msg}, rest}:
71 F.Finding{path, line, col, len, rule, msg} <> on.at(path, rest)
72
73# every per-file rule on a file, told its role (src/role.bend): the rules a
74# law or proof file changes (doc, hole, param, scan, fromrev, closed) read
75# the role, never the path, and the cost rules check nothing in a law or
76# proof file
77def on.rules.role(+rr: Role.Role, +ss: Src.Src) -> List<&2, F.Finding>:
78 List.concat(&2, F.Finding, [
79 Doc.check.role(rr, ss),
80 Unused.check(ss),
81 Hole.check.role(rr, ss),
82 Space.check(ss),
83 Wrap.check(ss),
84 Param.check.role(rr, ss),
85 Role.skip(rr, _u => Pick.check(ss)),
86 Role.skip(rr, _u => Pick.search(ss)),
87 Role.skip(rr, _u => Strict.check(ss)),
88 Role.skip(rr, _u => Eager.check(ss)),
89 Role.skip(rr, _u => Tail.check(ss)),
90 Role.skip(rr, _u => Concat.check(ss)),
91 Role.skip(rr, _u => Concat.cont(ss)),
92 Role.skip(rr, _u => Index.check(ss)),
93 Role.skip(rr, _u => Table.check(ss)),
94 Role.skip(rr, _u => Hoist.check(ss)),
95 Role.skip(rr, _u => Ring.check(ss)),
96 Role.skip(rr, _u => Rewalk.check(ss)),
97 Role.skip(rr, _u => Unit.check(ss)),
98 Scan.check.role(rr, ss),
99 Role.skip(rr, _u => Thunk.check(ss)),
100 Fromrev.check.role(rr, ss),
101 Put.check(ss),
102 Escape.check(ss),
103 Strings.check(ss),
104 Chars.check(ss),
105 Twice.check(ss),
106 Arms.check(ss),
107 Foreign.check(ss),
108 Fuel.check(ss),
109 Closed.check.role(rr, ss),
110 Transcong.check(ss)])
111
112# every rule's findings on a file read once, as each rule gives them, its
113# role read from its name
114def on.rules(+ss: Src.Src) -> List<&2, F.Finding>:
115 on.rules.role(Role.of_path(on.path(ss)), ss)
116
117# every rule's findings on a file read once, each at that file
118def on(+ss: Src.Src) -> List<&2, F.Finding>:
119 on.at(on.path(ss), on.rules(ss))
120
121# every rule's findings on a file read once, told its role, each at that file
122def on.role(+rr: Role.Role, +ss: Src.Src) -> List<&2, F.Finding>:
123 on.at(on.path(ss), on.rules.role(rr, ss))
124
125# every rule's findings on a file
126def all(+path: String, +text: String) -> List<&2, F.Finding>:
127 on(Src.of(path, text))
128
129# Packages (laws/ScopePackageRootsFound.bend). A package root other than `.` is a directory
130# holding a main.bend; a file belongs to the nearest root at or above it, and
131# `coverage` runs on each package's files alone. A directory is
132# named here as Digest.dir names one: normalized, with its slash (`w3c/`),
133# and `` for `.`.
134
135# whether a directory is among the directories
136def has(ds: List<&2, String>, +dd: String) -> Bool:
137 match ds:
138 case Nil{}:
139 False{}
140 case Con{d, rest}:
141 Lazy.or_else(String.eq(d, dd), _u => has(rest, dd))
142
143def ups.go(rs: List<&2, String>) -> List<&2, String>:
144 match rs:
145 case Nil{}:
146 Nil{}
147 case Con{s, +rest}:
148 (String.join(List.reverse(&2, String, s <> rest), "/") ++ "/") <> ups.go(rest)
149
150# a directory and each of its parents but ``, nearest first: `a/b/` is
151# `a/b/` then `a/`
152def ups(dir: String) -> List<&2, String>:
153 ups.go(List.reverse(&2, String, Imports.init(String.split(dir, '/'))))
154
155# the first of the directories that is a root, or `` (the package `.`)
156def owner.go(us: List<&2, String>, +roots: List<&2, String>) -> String:
157 match us:
158 case Nil{}:
159 ""
160 case Con{+u, rest}:
161 Lazy.stop(String, has(roots, u), u, _u => owner.go(rest, roots))
162
163# the package of a file in a directory: the nearest root at or above it
164def owner(+roots: List<&2, String>, dir: String) -> String:
165 owner.go(ups(dir), roots)
166
167# the files of one package, in order
168def members(ds: List<&2, Digest.Digest>, +roots: List<&2, String>, +pkg: String) -> List<&2, Digest.Digest>:
169 match ds:
170 case Nil{}:
171 Nil{}
172 case Con{Digest.Digest{path, norm, +dir, is_laws, is_law_file, exempt, tops, says, deps, unsafes, laws}, rest}:
173 +more = members(rest, roots, pkg)
174 Bool.pick(List<&2, Digest.Digest>, String.eq(owner(roots, dir), pkg),
175 Digest.Digest{path, norm, dir, is_laws, is_law_file, exempt, tops, says, deps, unsafes, laws} <> more, more)
176
177# `coverage` on each package but `.`
178def covers(rs: List<&2, String>, +ds: List<&2, Digest.Digest>, +roots: List<&2, String>) -> List<&2, F.Finding>:
179 match rs:
180 case Nil{}:
181 Nil{}
182 case Con{+dir, rest}:
183 List.append(&2, F.Finding, Law.check(members(ds, roots, dir)), covers(rest, ds, roots))
184
185# every project rule's findings on the files and the package roots but `.`
186# (each a directory as Digest.dir names one): each file is read down to its
187# digest once (rules/digest.bend); `coverage` runs on each package's files,
188# `.` first, and `unsafe` on every file
189def project(+srcs: List<&2, Src.Src>, +rs: List<&2, String>) -> List<&2, F.Finding>:
190 +ds = Digest.all.role(Role.marks(srcs), srcs)
191 List.concat(&2, F.Finding, [Law.check(members(ds, rs, "")), covers(rs, ds, rs), Unsafe.check(ds)])
192
193# the group a rule is in; an unknown slug grades with correctness
194def group(+rule: String) -> String:
195 +named = Codes.group(rule)
196 Bool.pick(String, String.is_empty(named), "correctness", named)
197
198# the findings at the levels a config gives their rules; the ones set off
199# are dropped
200def graded(+cfg: Config.Config, fs: List<&2, F.Finding>) -> List<&2, F.Graded>:
201 match fs:
202 case Nil{}:
203 Nil{}
204 case Con{F.Finding{path, line, col, len, +rule, msg}, rest}:
205 +more = graded(cfg, rest)
206 +l = Config.level(cfg, group(rule), rule)
207 Bool.pick(List<&2, F.Graded>, Config.is_off(l), more,
208 F.Graded{l, F.Finding{path, line, col, len, rule, msg}} <> more)