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 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). 13 import Base 14 import ./finding.bend as F 15 import ./codes.bend as Codes 16 import ./src.bend as Src 17 import ./rules/digest.bend as Digest 18 import ./config.bend as Config 19 import ./rules/style/doc.bend as Doc 20 import ./rules/suspicious/unused.bend as Unused 21 import ./rules/correctness/hole.bend as Hole 22 import ./rules/style/space.bend as Space 23 import ./rules/style/wrap.bend as Wrap 24 import ./rules/style/param.bend as Param 25 import ./rules/correctness/pick.bend as Pick 26 import ./rules/laws/law.bend as Law 27 import ./rules/suspicious/strict.bend as Strict 28 import ./rules/suspicious/eager.bend as Eager 29 import ./rules/pedantic/tail.bend as Tail 30 import ./rules/suspicious/concat.bend as Concat 31 import ./rules/suspicious/index.bend as Index 32 import ./rules/suspicious/table.bend as Table 33 import ./rules/suspicious/hoist.bend as Hoist 34 import ./rules/suspicious/ring.bend as Ring 35 import ./rules/suspicious/rewalk.bend as Rewalk 36 import ./rules/suspicious/unit.bend as Unit 37 import ./rules/correctness/put.bend as Put 38 import ./rules/correctness/escape.bend as Escape 39 import ./rules/correctness/strings.bend as Strings 40 import ./rules/correctness/chars.bend as Chars 41 import ./rules/correctness/twice.bend as Twice 42 import ./rules/correctness/arms.bend as Arms 43 import ./rules/correctness/foreign.bend as Foreign 44 import ./rules/suspicious/fuel.bend as Fuel 45 import ./rules/laws/closed.bend as Closed 46 import ./rules/laws/trace.bend as Trace 47 import ./rules/laws/unsafe.bend as Unsafe 48 49 # the file a Src was read from 50 def 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 56 def 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 64 def 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 94 def 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 98 def 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 104 def 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 109 def 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 115 def 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)