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). 5 import Base 6 import bend-lint-api@0.2.2.0/linter/lint.bend as Lint 7 import bend-lint-api@0.2.2.0/shared/lists.bend as Lists 8 import bend-lint-api@0.2.2.0/shared/strings.bend as Strings 9 10 # The fix that deletes the import's line. 11 def 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 20 def 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 37 def 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 48 def run(+here: Lint.Here) -> Lint.Ask(List<&2, Lint.Finding>): 49 Lint.done(findings(Lint.Here.declarations(here), Lint.Here.text(here), [])) 50 51 def rule() -> Lint.Rule: 52 Lint.Rule{Lint.Needs{False{}, True{}}, [], Lint.OnFile{run}}