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/suspicious/empty.bend as Empty 45 import ./rules/correctness/put.bend as Put 46 import ./rules/correctness/escape.bend as Escape 47 import ./rules/perf/strings.bend as Strings 48 import ./rules/perf/chars.bend as Chars 49 import ./rules/correctness/twice.bend as Twice 50 import ./rules/correctness/arms.bend as Arms 51 import ./rules/pedantic/foreign.bend as Foreign 52 import ./rules/pedantic/fuel.bend as Fuel 53 import ./rules/laws/closed.bend as Closed 54 55 # the file a Src was read from 56 def 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 62 def 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 per-file rule on a file, told its role (src/role.bend): the rules a 70 # law or proof file changes (doc, hole, param, fromrev, prefix, empty, closed) 71 # read the role, never the path, and the cost rules check nothing in a law 72 # or proof file 73 def on.rules.role(+rr: Role.Role, +ss: Src.Src) -> List<&2, F.Finding>: 74 List.concat(&2, F.Finding, [ 75 Doc.check.role(rr, ss), 76 Unused.check(ss), 77 Hole.check.role(rr, ss), 78 Space.check(ss), 79 Wrap.check(ss), 80 Param.check.role(rr, ss), 81 Role.skip(rr, _u => Pick.check(ss)), 82 Role.skip(rr, _u => Pick.search(ss)), 83 Role.skip(rr, _u => Strict.check(ss)), 84 Role.skip(rr, _u => Eager.check(ss)), 85 Role.skip(rr, _u => Tail.check(ss)), 86 Role.skip(rr, _u => Concat.check(ss)), 87 Role.skip(rr, _u => Concat.cont(ss)), 88 Role.skip(rr, _u => Index.check(ss)), 89 Role.skip(rr, _u => Rewalk.check(ss)), 90 Role.skip(rr, _u => Thunk.check(ss)), 91 Fromrev.check.role(rr, ss), 92 Prefix.check.role(rr, ss), 93 Empty.check.role(rr, ss), 94 Put.check(ss), 95 Escape.check(ss), 96 Strings.check(ss), 97 Chars.check(ss), 98 Twice.check(ss), 99 Arms.check(ss), 100 Foreign.check(ss), 101 Fuel.check(ss), 102 Closed.check.role(rr, ss), 103 Transcong.check(ss)]) 104 105 # every rule's findings on a file read once, told its role, each at that file 106 def on.role(+rr: Role.Role, +ss: Src.Src) -> List<&2, F.Finding>: 107 on.at(on.path(ss), on.rules.role(rr, ss)) 108 109 # Packages (laws/ScopePackageRootsFound.bend). A package root other than `.` is a directory 110 # holding a main.bend; a file belongs to the nearest root at or above it, and 111 # `coverage` runs on each package's files alone. A directory is 112 # named here as Digest.dir names one: normalized, with its slash (`w3c/`), 113 # and `` for `.`. 114 115 # whether a directory is among the directories 116 def has(ds: List<&2, String>, +dd: String) -> Bool: 117 match ds: 118 case Nil{}: 119 False{} 120 case Con{d, rest}: 121 Lazy.or_else(String.eq(d, dd), _u => has(rest, dd)) 122 123 def ups.go(rs: List<&2, String>) -> List<&2, String>: 124 match rs: 125 case Nil{}: 126 Nil{} 127 case Con{s, +rest}: 128 (String.join(List.reverse(&2, String, s <> rest), "/") ++ "/") <> ups.go(rest) 129 130 # a directory and each of its parents but ``, nearest first: `a/b/` is 131 # `a/b/` then `a/` 132 def ups(dir: String) -> List<&2, String>: 133 ups.go(List.reverse(&2, String, Imports.init(String.split(dir, '/')))) 134 135 # the first of the directories that is a root, or `` (the package `.`) 136 def owner.go(us: List<&2, String>, +roots: List<&2, String>) -> String: 137 match us: 138 case Nil{}: 139 "" 140 case Con{+u, rest}: 141 Lazy.stop(String, has(roots, u), u, _u => owner.go(rest, roots)) 142 143 # the package of a file in a directory: the nearest root at or above it 144 def owner(+roots: List<&2, String>, dir: String) -> String: 145 owner.go(ups(dir), roots) 146 147 # the files of one package, in order 148 def members(ds: List<&2, Digest.Digest>, +roots: List<&2, String>, +pkg: String) -> List<&2, Digest.Digest>: 149 match ds: 150 case Nil{}: 151 Nil{} 152 case Con{Digest.Digest{path, norm, +dir, is_laws, is_law_file, exempt, tops, says, deps, laws}, rest}: 153 +more = members(rest, roots, pkg) 154 Bool.pick(List<&2, Digest.Digest>, String.eq(owner(roots, dir), pkg), 155 Digest.Digest{path, norm, dir, is_laws, is_law_file, exempt, tops, says, deps, laws} <> more, more) 156 157 # `coverage` on each package but `.` 158 def covers(rs: List<&2, String>, +ds: List<&2, Digest.Digest>, +roots: List<&2, String>) -> List<&2, F.Finding>: 159 match rs: 160 case Nil{}: 161 Nil{} 162 case Con{+dir, rest}: 163 List.append(&2, F.Finding, Law.check(members(ds, roots, dir)), covers(rest, ds, roots)) 164 165 # every project rule's findings on the files and the package roots but `.` 166 # (each a directory as Digest.dir names one): each file is read down to its 167 # digest once (rules/digest.bend); `coverage` runs on each package's files, 168 # `.` first 169 def project(+srcs: List<&2, Src.Src>, +rs: List<&2, String>) -> List<&2, F.Finding>: 170 +ds = Digest.all.role(Role.marks(srcs), srcs) 171 List.concat(&2, F.Finding, [Law.check(members(ds, rs, "")), covers(rs, ds, rs)]) 172 173 # the group a rule is in; an unknown slug grades with correctness 174 def group(+rule: String) -> String: 175 +named = Codes.group(rule) 176 Bool.pick(String, String.is_empty(named), "correctness", named) 177 178 # the findings at the levels a config gives their rules; the ones set off 179 # are dropped 180 def graded(+cfg: Config.Config, fs: List<&2, F.Finding>) -> List<&2, F.Graded>: 181 match fs: 182 case Nil{}: 183 Nil{} 184 case Con{F.Finding{path, line, col, len, +rule, msg}, rest}: 185 +more = graded(cfg, rest) 186 +l = Config.level(cfg, group(rule), rule) 187 Bool.pick(List<&2, F.Graded>, Config.is_off(l), more, 188 F.Graded{l, F.Finding{path, line, col, len, rule, msg}} <> more)