2018-11-22 21:19:19 +03:00
|
|
|
module NoUnusedVariablesTest exposing (all)
|
|
|
|
|
|
|
|
import Lint.Rule.NoUnusedVariables exposing (rule)
|
2019-07-02 01:04:45 +03:00
|
|
|
import Lint.Test exposing (LintResult)
|
2019-06-26 14:47:00 +03:00
|
|
|
import Test exposing (Test, describe, test)
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
testRule : String -> LintResult
|
|
|
|
testRule =
|
2019-07-02 01:04:45 +03:00
|
|
|
Lint.Test.run rule
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
tests : List Test
|
|
|
|
tests =
|
|
|
|
[ test "should not report exposed top-level variables" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
a = 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report used top-level variables" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (b)
|
|
|
|
a n = 1
|
|
|
|
b = a 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused top-level variables" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (b)
|
|
|
|
a = 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `a` is not used"
|
|
|
|
, under = "a"
|
|
|
|
}
|
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused top-level variables even if they are annotated" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (b)
|
|
|
|
a: Int
|
|
|
|
a = 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `a` is not used"
|
|
|
|
, under = "a"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 3, column = 1 }, end = { row = 3, column = 2 } }
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report unused top-level variables if everything is exposed" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (..)
|
|
|
|
a n = 1
|
|
|
|
b = a 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report unused top-level variables that are exposed by name" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a, b)
|
|
|
|
a = 1
|
|
|
|
b = 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report unused top-level variables that are exposed by name, but report others" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a, b)
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
c = 3"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `c` is not used"
|
|
|
|
, under = "c"
|
|
|
|
}
|
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report unused top-level variables if everything is exposed (port module)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """port module A exposing (..)
|
|
|
|
a n = 1
|
|
|
|
b = a 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report unused top-level variables that are exposed by name (port module)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """port module A exposing (a, b)
|
|
|
|
a = 1
|
|
|
|
b = 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report unused top-level variables that are exposed by name, but report others (port module)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """port module A exposing (a, b)
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
c = 3"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `c` is not used"
|
|
|
|
, under = "c"
|
|
|
|
}
|
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused variables from let declarations" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
a = let b = 1
|
|
|
|
in 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `b` is not used"
|
|
|
|
, under = "b"
|
|
|
|
}
|
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused variables from let even if they are exposed by name" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a, b)
|
|
|
|
a = let b = 1
|
|
|
|
in 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `b` is not used"
|
|
|
|
, under = "b"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } }
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused functions from let even if they are exposed by name" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
a = let b param = 1
|
|
|
|
in 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `b` is not used"
|
|
|
|
, under = "b"
|
|
|
|
}
|
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused variables from let even if everything is exposed" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (..)
|
|
|
|
a = let b = 1
|
|
|
|
in 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `b` is not used"
|
|
|
|
, under = "b"
|
|
|
|
}
|
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report top-level variables used inside a let expression" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
b = 1
|
|
|
|
a = let c = 1
|
|
|
|
in b + c"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report top-level variables used inside let declarations" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
b = 1
|
|
|
|
a = let c = b
|
|
|
|
in c"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report top-level variables used in nested lets" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
b = 1
|
|
|
|
a = let
|
|
|
|
c = b
|
|
|
|
d = let
|
|
|
|
e = 1
|
|
|
|
in
|
|
|
|
b + c + e
|
|
|
|
in
|
|
|
|
d"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-02 12:16:03 +03:00
|
|
|
, test "should not report variables used in a record update expression's value to be updated" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
b = { c = 1 }
|
|
|
|
a = { b | c = 3 }"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report variables used in a record update expression's updates" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
b = { y = 1, z = 1 }
|
|
|
|
d = 3
|
|
|
|
e = 3
|
|
|
|
a = { b | y = d, z = e }"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should report variables even if they appear as keys of a record update expression's updates" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
b = { z = 1, c = 2 }
|
|
|
|
c = 1
|
|
|
|
a = { b | c = 3 }"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
|
|
|
{ message = "Variable `c` is not used"
|
|
|
|
, under = "c"
|
|
|
|
}
|
|
|
|
|> Lint.Test.atExactly { start = { row = 3, column = 1 }, end = { row = 3, column = 2 } }
|
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report variables from let declarations that are used in the expression" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
a = let c = 1
|
|
|
|
in c"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report unused function parameters" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
a n = 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused imported functions" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (b)
|
|
|
|
import Foo exposing (a)"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Imported variable `a` is not used"
|
|
|
|
, under = "a"
|
|
|
|
}
|
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused imported functions (multiple imports)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (d)
|
|
|
|
import Foo exposing (C, a, b)"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-07-03 14:44:51 +03:00
|
|
|
{ message = "Imported type `C` is not used"
|
|
|
|
, under = "C"
|
2019-06-30 13:49:03 +03:00
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
, Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Imported variable `a` is not used"
|
|
|
|
, under = "a"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
, Lint.Test.error
|
2019-07-03 14:44:51 +03:00
|
|
|
{ message = "Imported variable `b` is not used"
|
|
|
|
, under = "b"
|
2019-06-30 13:49:03 +03:00
|
|
|
}
|
2018-11-22 21:19:19 +03:00
|
|
|
]
|
|
|
|
, test "should not report unused pattern matching parameters" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
a = case thing of
|
|
|
|
Foo b c -> []"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
|
|
|
|
-- Should B and C be reported if they are not used? Probably.
|
|
|
|
, test "should report unused custom type declarations" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type A = B | C"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Type `A` is not used"
|
|
|
|
, under = "A"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 6 }, end = { row = 2, column = 7 } }
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused type aliases declarations" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type alias A = { a : B }"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Type `A` is not used"
|
|
|
|
, under = "A"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 12 }, end = { row = 2, column = 13 } }
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report type used in a signature" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type alias A = { a : B }
|
|
|
|
a : A
|
|
|
|
a = {a = 1}"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report type used in a signature with multiple arguments" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type alias A = { a : B }
|
|
|
|
a : String -> A
|
|
|
|
a str = {a = str}"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report type used in a signature with parameterized types (as generic type)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type alias A = { a : B }
|
|
|
|
a : A B
|
|
|
|
a = []"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report type used in a signature with parameterized types (as parameter)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type alias A = { a : B }
|
|
|
|
a : List A
|
|
|
|
a = []"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report type used in a signature with a record" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type alias A = { a : B }
|
|
|
|
a : { c: A }
|
|
|
|
a str = {c = str}"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report type used in a signature with a generic record" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type alias A = { a : B }
|
|
|
|
a : { r | c: A }
|
|
|
|
a str = {c = str}"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-02 12:42:07 +03:00
|
|
|
, test "should not report type used in a custom type constructor definition" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (ExposedType)
|
2019-07-02 12:55:49 +03:00
|
|
|
type A = B
|
2019-07-02 12:42:07 +03:00
|
|
|
|
|
|
|
type ExposedType = Something A
|
2019-07-05 01:33:51 +03:00
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report a type of which a constructor is used" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (b)
|
|
|
|
type A = B | C | D
|
|
|
|
|
|
|
|
b = B
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report a type of which a constructor is used even if it was defined afterwards" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (b)
|
|
|
|
b = B
|
|
|
|
|
|
|
|
type A = B | C | D
|
2019-07-02 12:55:49 +03:00
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report type used in type signature inside a let-in" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type A = A
|
|
|
|
|
|
|
|
a = let
|
|
|
|
b : A
|
|
|
|
b = 1
|
|
|
|
in
|
|
|
|
b
|
2019-07-02 12:42:07 +03:00
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report type used in a type alias field" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (ExposedType)
|
|
|
|
type A = B | C
|
|
|
|
|
|
|
|
type alias ExposedType = { a : A }
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report type used in a type alias field's arguments " <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (ExposedType)
|
|
|
|
type A = B | C
|
|
|
|
|
|
|
|
type alias ExposedType = { a : Maybe A }
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report type if it's exposed" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (A)
|
|
|
|
type A a = B a"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report custom type if it's exposed with its sub-types" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (A(..))
|
|
|
|
type A = B | C | D"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused variable even if it's present in a generic type" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (A)
|
|
|
|
a = 1
|
|
|
|
type A a = B a"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `a` is not used"
|
|
|
|
, under = "a"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 1 }, end = { row = 2, column = 2 } }
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused variable even if it's present in a generic record type" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
r = 1
|
|
|
|
a : { r | c: A }
|
|
|
|
a str = {c = str}"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `r` is not used"
|
|
|
|
, under = "r"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 1 }, end = { row = 2, column = 2 } }
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused operator import" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Parser exposing ((</>))"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Imported operator `</>` is not used"
|
|
|
|
, under = "(</>)"
|
|
|
|
}
|
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report used operator (infix)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Parser exposing ((</>))
|
|
|
|
a = 1 </> 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should not report used operator (prefix)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Parser exposing ((</>))
|
|
|
|
a = (</>) 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-24 19:41:13 +03:00
|
|
|
, test "should report unused opaque types" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type A = A Int"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Type `A` is not used"
|
|
|
|
, under = "A"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 6 }, end = { row = 2, column = 7 } }
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-24 19:41:13 +03:00
|
|
|
, test "should not report used opaque types" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
type A = A Int
|
|
|
|
a : A
|
|
|
|
a = 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-26 13:22:43 +03:00
|
|
|
, test "should report unused import" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Html"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Imported module `Html` is not used"
|
|
|
|
, under = "Html"
|
|
|
|
}
|
|
|
|
]
|
2018-11-26 13:22:43 +03:00
|
|
|
, test "should report unused import (multiples segments)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Html.Styled.Attributes"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Imported module `Html.Styled.Attributes` is not used"
|
|
|
|
, under = "Html.Styled.Attributes"
|
|
|
|
}
|
|
|
|
]
|
2018-11-26 13:22:43 +03:00
|
|
|
, test "should not report import if it exposes all (should be improved by detecting if any exposed value is used)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Html.Styled.Attributes exposing (..)"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-26 13:22:43 +03:00
|
|
|
, test "should report unused variable even if a homonym from a module is used" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
href = 1
|
|
|
|
a = Html.Styled.Attributes.href"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Variable `href` is not used"
|
|
|
|
, under = "href"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 1 }, end = { row = 2, column = 5 } }
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-26 13:22:43 +03:00
|
|
|
, test "should not report used import (function access)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Html.Styled.Attributes
|
|
|
|
a = Html.Styled.Attributes.href"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-26 13:22:43 +03:00
|
|
|
, test "should not report unused import if it is aliased" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Html.Styled.Attributes as Html
|
|
|
|
a = Html.href"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-26 13:22:43 +03:00
|
|
|
, test "should report unused import alias" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Html.Styled.Attributes as Html"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Module alias `Html` is not used"
|
|
|
|
, under = "Html"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 34 }, end = { row = 2, column = 38 } }
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-26 20:12:36 +03:00
|
|
|
, test "should not report import that exposes a used exposed type" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import B exposing (C(..))
|
|
|
|
a : C
|
|
|
|
a = 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-26 20:12:36 +03:00
|
|
|
, test "should not report import that exposes an unused exposed type (but whose subtype is potentially used)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import B exposing (C(..))
|
|
|
|
a : D
|
|
|
|
a = 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-26 21:09:18 +03:00
|
|
|
, test "should not report types that are used in ports" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (output, input)
|
|
|
|
import Json.Decode
|
|
|
|
import Json.Encode
|
|
|
|
port output : Json.Encode.Value -> Cmd msg
|
|
|
|
port input : (Json.Decode.Value -> msg) -> Sub msg"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-26 21:09:18 +03:00
|
|
|
, test "should report unused ports (ingoing)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Json.Decode
|
|
|
|
port input : (Json.Decode.Value -> msg) -> Sub msg"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Port `input` is not used (Warning: Removing this port may break your application if it is used in the JS code)"
|
|
|
|
, under = "input"
|
|
|
|
}
|
2018-11-26 21:09:18 +03:00
|
|
|
]
|
|
|
|
, test "should report unused ports (outgoing)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module A exposing (a)
|
|
|
|
import Json.Encode
|
|
|
|
port output : Json.Encode.Value -> Cmd msg"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-06-30 13:49:03 +03:00
|
|
|
{ message = "Port `output` is not used (Warning: Removing this port may break your application if it is used in the JS code)"
|
|
|
|
, under = "output"
|
|
|
|
}
|
2018-11-26 21:09:18 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
all : Test
|
|
|
|
all =
|
|
|
|
describe "NoUnusedVariables" tests
|