no_unused/imports.bend
bend-lint-rules@0.2.1.0 · 52 lines · 0 definitions
1# no_unused/imports: an import that nothing in its file uses.
2# Fix: deletes the import's line.
3# Heuristics: a use is a name written through the import's alias, or, for Base, a name that only Base
4# declares (Imported in lint.bend).
5import Base
6import bend-lint-api@0.2.1.0/linter/lint.bend as Lint
7import bend-lint-api@0.2.1.0/shared/lists.bend as Lists
8import bend-lint-api@0.2.1.0/shared/strings.bend as Strings
9
10# The fix that deletes the import's line.
11def deleted(+text: String, path: String, at: U32) -> List<&2, Lint.Fix>:
12 +beg = Strings.line_start(text, at)
13 [
14 Lint.Fix{
15 "Remove the import",
16 [Lint.Replace{Lint.Span{path, beg, Strings.line_end(text, beg)}, ""}]
17 }
18 ]
19
20def unused(text: String, d: Lint.Declaration) -> List<&2, Lint.Finding>:
21 match d:
22 case Lint.Declaration{
23 Lint.Node{Lint.Import{t}, name, Some{Lint.Span{+path, +beg, end}}, k, f},
24 Nil{}
25 }:
26 [
27 Lint.Finding{
28 Lint.Warning{},
29 Lint.Span{path, beg, end},
30 "Remove the import " ++ name ++ ": nothing uses it.",
31 deleted(text, path, beg)
32 }
33 ]
34 case _:
35 []
36
37def findings(
38 declarations: List<&2, Lint.Declaration>,
39 +text: String,
40 out: List<&2, Lint.Finding>
41) -> List<&2, Lint.Finding>:
42 match declarations:
43 case Nil{}:
44 List.reverse(&2, Lint.Finding, out)
45 case Con{d, rest}:
46 findings(rest, text, Lists.onto(&2, Lint.Finding, unused(text, d), out))
47
48def run(+here: Lint.Here) -> Lint.Ask(List<&2, Lint.Finding>):
49 Lint.done(findings(Lint.Here.declarations(here), Lint.Here.text(here), []))
50
51def rule() -> Lint.Rule:
52 Lint.Rule{Lint.Needs{False{}, True{}}, [], Lint.OnFile{run}}