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