NoUnusedVariables: Report unused imported variables

This commit is contained in:
Jeroen Engels 2017-01-28 16:01:58 +01:00
parent da3f260ece
commit c371244a4c
2 changed files with 43 additions and 15 deletions

View File

@ -41,7 +41,7 @@ implementation =
createError : String -> Error
createError name =
Error "NoUnusedVariables" ("Variable `" ++ name ++ "` was not used`")
Error "NoUnusedVariables" ("Variable `" ++ name ++ "` is not used")
addUsedToStack : List Scope -> List String -> List Scope
@ -160,6 +160,23 @@ statementFn ctx node =
Enter (ModuleDeclaration names AllExport) ->
( [], { ctx | exportsEverything = True } )
Enter (ImportStatement module_ alias_ (Just (SubsetExport imported))) ->
let
variables =
List.foldl
(\var res ->
case var of
FunctionExport name ->
name :: res
_ ->
res
)
[]
imported
in
( [], { ctx | scopes = addFoundToStack ctx.scopes variables } )
Enter (ModuleDeclaration names exportType) ->
( []
, { ctx

View File

@ -23,14 +23,14 @@ tests =
, test "should report unused top-level variables" <|
\() ->
rule "a = 1"
|> Expect.equal [ error "Variable `a` was not used`" ]
|> Expect.equal [ error "Variable `a` is not used" ]
, test "should report unused top-level variables even if they are annotated" <|
\() ->
rule """
a: Int
a = 1
"""
|> Expect.equal [ error "Variable `a` was not used`" ]
|> Expect.equal [ error "Variable `a` is not used" ]
, test "should not report unused top-level variables if everything is exposed" <|
\() ->
rule """module A exposing (..)
@ -52,28 +52,28 @@ tests =
b = 2
c = 3
"""
|> Expect.equal [ error "Variable `c` was not used`" ]
|> Expect.equal [ error "Variable `c` is not used" ]
, test "should report unused variables from let declarations" <|
\() ->
rule """module A exposing (a)
a = let b = 1
in 2
"""
|> Expect.equal [ error "Variable `b` was not used`" ]
|> Expect.equal [ error "Variable `b` is not used" ]
, test "should report unused variables from let even if they are exposed by name" <|
\() ->
rule """module A exposing (a, b)
a = let b = 1
in 2
"""
|> Expect.equal [ error "Variable `b` was not used`" ]
|> Expect.equal [ error "Variable `b` is not used" ]
, test "should report unused variables from let even if everything is exposed" <|
\() ->
rule """module A exposing (..)
a = let b = 1
in 2
"""
|> Expect.equal [ error "Variable `b` was not used`" ]
|> Expect.equal [ error "Variable `b` is not used" ]
, test "should not report top-level variables used inside a let body" <|
\() ->
rule """module A exposing (a)
@ -117,6 +117,17 @@ tests =
a n = 1
"""
|> Expect.equal []
, test "should report unused imported functions" <|
\() ->
rule "import Foo exposing (a)"
|> Expect.equal [ error "Variable `a` is not used" ]
, test "should report unused imported functions (multiple imports)" <|
\() ->
rule "import Foo exposing (a, b, C)"
|> Expect.equal
[ error "Variable `a` is not used"
, error "Variable `b` is not used"
]
-- Needs to be improved, every case should create a new scope stack
-- Right now, every parameter is considered used, which is not great
, test "should not report unused pattern matching parameters" <|
@ -126,7 +137,7 @@ tests =
Foo b c -> []
"""
|> Expect.equal []
-- What to do with types needs to be precised when I understand Type exports better (wrt sub-types)
-- What to do with types needs to be determined when I understand Type exports better (wrt sub-types)
, test "should report unused type declarations" <|
\() ->
rule """
@ -138,7 +149,7 @@ tests =
-- \() ->
-- rule """module A exposing (a, b)
-- """
-- |> Expect.equal [ error "Variable `a` was not used`" ]
-- |> Expect.equal [ error "Variable `a` is not used" ]
-- , test "should not report unused type declarations that are exposed by name" <|
-- \() ->
-- rule """module A exposing (a, b)
@ -146,7 +157,7 @@ tests =
-- b = 2
-- c = 3
-- """
-- |> Expect.equal [ error "Variable `a` was not used`" ]
-- |> Expect.equal [ error "Variable `a` is not used" ]
-- , test "should report unused named imports `import A exposing (a)`" <|
-- \() ->
-- rule """module A exposing (a, b)
@ -154,7 +165,7 @@ tests =
-- b = 2
-- c = 3
-- """
-- |> Expect.equal [ error "Variable `a` was not used`" ]
-- |> Expect.equal [ error "Variable `a` is not used" ]
-- , test "should not report used named imports `import A exposing (a)`" <|
-- \() ->
-- rule """module A exposing (a, b)
@ -162,7 +173,7 @@ tests =
-- b = 2
-- c = 3
-- """
-- |> Expect.equal [ error "Variable `a` was not used`" ]
-- |> Expect.equal [ error "Variable `a` is not used" ]
-- , test "should not report unused union imports `import A exposing (B(..))`" <|
-- \() ->
-- rule """module A exposing (a, b)
@ -170,7 +181,7 @@ tests =
-- b = 2
-- c = 3
-- """
-- |> Expect.equal [ error "Variable `a` was not used`" ]
-- |> Expect.equal [ error "Variable `a` is not used" ]
-- , test "should report unused union imports `import A exposing (B(B))`" <|
-- \() ->
-- rule """module A exposing (a, b)
@ -178,7 +189,7 @@ tests =
-- b = 2
-- c = 3
-- """
-- |> Expect.equal [ error "Variable `a` was not used`" ]
-- |> Expect.equal [ error "Variable `a` is not used" ]
-- , test "should not report unused union imports `import A exposing (B(B))` (with more nesting?)" <|
-- \() ->
-- rule """module A exposing (a, b)
@ -186,7 +197,7 @@ tests =
-- b = 2
-- c = 3
-- """
-- |> Expect.equal [ error "Variable `a` was not used`" ]
-- |> Expect.equal [ error "Variable `a` is not used" ]
]