src/rules/style/noqa.bend

bolt@1.24.0.0 · 94 lines · 0 definitions

1# rule noqa: a noqa comment (src/noqa.bend) that silences nothing, as
2# ruff's RUF100. A bare one names no code, so it silences nothing; a code
3# it names silences nothing when no graded finding of the file, on the
4# mark's line, has that code, and a code that is no rule's never does. One
5# finding for a bare mark, and one for each such code, at the comment.
6#
7# It reads the graded findings of the whole run, so it is no per-file rule:
8# the planner runs it after every other rule and after the filter, and
9# what it reports no noqa comment silences. A project rule's code
10# (`coverage`, `unsafe`) is judged only when the run is the whole
11# tree (`whole`): over files named, and in the editor, those rules see only
12# part of the laws, so their silence proves nothing.
13import Base
14import bolt@1.24.0.0/src/lazy/lazy.bend as Lazy
15import bolt@1.24.0.0/src/finding.bend as F
16import bolt@1.24.0.0/src/codes.bend as Codes
17import bolt@1.24.0.0/src/noqa.bend as Noqa
18
19# a project rule's code
20def project(+code: String) -> Bool:
21  Lazy.or_else(String.eq(code, Codes.code("coverage")), _u => String.eq(code, Codes.code("unsafe")))
22
23# a code this run can judge: any, over the whole tree; a per-file rule's
24# (or no rule's) otherwise
25def judged(+whole: Bool, +code: String) -> Bool:
26  Lazy.or_else(whole, _u => Bool.not(project(code)))
27
28# some row of the table has the code
29def known.in(rows: List<&2, Codes.Entry>, +code: String) -> Bool:
30  match rows:
31    case Nil{}:
32      False{}
33    case Con{Codes.Entry{_s, id, _grp}, rest}:
34      Lazy.or_else(String.eq(id, code), _u => known.in(rest, code))
35
36# the code is a rule's
37def known(+code: String) -> Bool:
38  known.in(Codes.table(), code)
39
40# a graded finding at the path, on the line, has the code
41def used(gs: List<&2, F.Graded>, +path: String, +line: U32, +code: String) -> Bool:
42  match gs:
43    case Nil{}:
44      False{}
45    case Con{F.Graded{_ll, F.Finding{p, l, _col, _len, rule, _msg}}, rest}:
46      Lazy.or_else(
47        Lazy.and_then(String.eq(p, path),
48          _u => Lazy.and_then(U32.is_eq(l, line), _v => String.eq(Codes.code(rule), code))),
49        _w => used(rest, path, line, code))
50
51# what a bare mark is told
52def say.bare() -> String:
53  "`# noqa` names no code, so it silences nothing; name the codes, e.g. `# noqa: L001`."
54
55# what an unused code is told: a rule's, or no rule's
56def say(+code: String) -> String:
57  Bool.pick(String, known(code), "`# noqa: " ++ code ++ "` silences nothing on this line.",
58    "`# noqa: " ++ code ++ "` is no rule's code, so it silences nothing.")
59
60# one finding for each code judged here that silences nothing on its line
61def check.codes(
62  cs: List<&2, String>,
63  +whole: Bool,
64  +path: String,
65  +line: U32,
66  +col: U32,
67  +len: U32,
68  +gs: List<&2, F.Graded>
69) -> List<&2, F.Finding>:
70  match cs:
71    case Nil{}:
72      Nil{}
73    case Con{+c, rest}:
74      +more = check.codes(rest, whole, path, line, col, len, gs)
75      +bad = Lazy.and_then(judged(whole, c), _u => Bool.not(used(gs, path, line, c)))
76      +msg = say(c)
77      Bool.pick(List<&2, F.Finding>, bad, F.Finding{path, line, col, len, "noqa", msg} <> more, more)
78
79# a mark's findings: one when it is bare, else one per code that silences
80# nothing
81def check.mark(mk: Noqa.Mark, +whole: Bool, +path: String, +gs: List<&2, F.Graded>) -> List<&2, F.Finding>:
82  match mk:
83    case Noqa.Mark{line, col, len, Nil{}}:
84      [F.Finding{path, line, col, len, "noqa", say.bare()}]
85    case Noqa.Mark{line, col, len, Con{c, rest}}:
86      check.codes(c <> rest, whole, path, line, col, len, gs)
87
88# every mark of the file at the path, against the run's graded findings
89def check(mks: List<&2, Noqa.Mark>, +whole: Bool, +path: String, +gs: List<&2, F.Graded>) -> List<&2, F.Finding>:
90  match mks:
91    case Nil{}:
92      Nil{}
93    case Con{mk, rest}:
94      List.append(&2, F.Finding, check.mark(mk, whole, path, gs), check(rest, whole, path, gs))