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
|
|
|
|
|
|
|
|
2019-07-28 01:16:57 +03:00
|
|
|
details : List String
|
|
|
|
details =
|
|
|
|
[ "Since it is not being used, I recommend removing it. It should make the code clearer to read for other people." ]
|
|
|
|
|
|
|
|
|
2019-07-24 19:58:56 +03:00
|
|
|
all : Test
|
|
|
|
all =
|
|
|
|
describe "NoUnusedVariables"
|
|
|
|
[ describe "Top-level variables" topLevelVariablesTests
|
|
|
|
, describe "let..in" letInTests
|
|
|
|
, describe "Top-level variables used inside a let..in" topLevelVariablesUsedInLetInTests
|
|
|
|
, describe "Record updates" recordUpdateTests
|
|
|
|
, describe "Function parameters" functionParameterTests
|
|
|
|
, describe "Imports" importTests
|
|
|
|
, describe "Pattern matching variables" patternMatchingVariablesTests
|
|
|
|
, describe "Defined types" typeTests
|
|
|
|
, describe "Opaque Types" opaqueTypeTests
|
|
|
|
, describe "Operators" operatorTests
|
|
|
|
, describe "Ports" portTests
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
topLevelVariablesTests : List Test
|
|
|
|
topLevelVariablesTests =
|
2018-11-22 21:19:19 +03:00
|
|
|
[ test "should not report exposed top-level variables" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
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" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (b)
|
2018-11-22 21:19:19 +03:00
|
|
|
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" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (b)
|
2019-08-05 01:04:27 +03:00
|
|
|
b = 1
|
|
|
|
a = 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "Top-level variable `a` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "a"
|
|
|
|
}
|
2019-08-05 01:04:27 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (b)
|
|
|
|
b = 1
|
|
|
|
"""
|
|
|
|
]
|
|
|
|
, test "should report unused top-level variables with type annotation" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module SomeModule exposing (b)
|
|
|
|
b = 1
|
|
|
|
a : Int
|
|
|
|
a = 2"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "Top-level variable `a` is not used"
|
2019-08-05 01:04:27 +03:00
|
|
|
, details = details
|
|
|
|
, under = "a"
|
|
|
|
}
|
|
|
|
|> Lint.Test.atExactly { start = { row = 4, column = 1 }, end = { row = 4, column = 2 } }
|
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (b)
|
|
|
|
b = 1
|
|
|
|
"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused top-level variables even if they are annotated" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (b)
|
2018-11-22 21:19:19 +03:00
|
|
|
a: Int
|
2019-08-05 01:04:27 +03:00
|
|
|
a = 1
|
|
|
|
b = 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "Top-level variable `a` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "a"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 3, column = 1 }, end = { row = 3, column = 2 } }
|
2019-08-05 01:04:27 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (b)
|
|
|
|
|
|
|
|
b = 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" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (..)
|
2018-11-22 21:19:19 +03:00
|
|
|
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" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a, b)
|
2018-11-22 21:19:19 +03:00
|
|
|
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" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a, b)
|
2018-11-22 21:19:19 +03:00
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
c = 3"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "Top-level variable `c` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "c"
|
|
|
|
}
|
2019-08-05 01:04:27 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a, b)
|
|
|
|
a = 1
|
|
|
|
b = 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 (port module)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """port module SomeModule exposing (..)
|
2018-11-22 21:19:19 +03:00
|
|
|
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)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """port module SomeModule exposing (a, b)
|
2018-11-22 21:19:19 +03:00
|
|
|
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)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """port module SomeModule exposing (a, b)
|
2018-11-22 21:19:19 +03:00
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
c = 3"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "Top-level variable `c` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "c"
|
|
|
|
}
|
2019-08-05 01:04:27 +03:00
|
|
|
|> Lint.Test.whenFixed """port module SomeModule exposing (a, b)
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should report unused variable even if a homonym from a module is used" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
href = 1
|
|
|
|
a = Html.Styled.Attributes.href"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "Top-level variable `href` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-24 19:58:56 +03:00
|
|
|
, under = "href"
|
|
|
|
}
|
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 1 }, end = { row = 2, column = 5 } }
|
2019-08-05 01:04:27 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
|
|
|
|
a = Html.Styled.Attributes.href"""
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
letInTests : List Test
|
|
|
|
letInTests =
|
|
|
|
[ test "should report unused variables from let declarations" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
a = let b = 1
|
|
|
|
in 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "`let in` variable `b` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "b"
|
|
|
|
}
|
2019-08-09 20:04:12 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
a = 2"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused variables from let even if they are exposed by name" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a, b)
|
2018-11-22 21:19:19 +03:00
|
|
|
a = let b = 1
|
2019-08-09 20:04:12 +03:00
|
|
|
c = 2
|
|
|
|
in c"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "`let in` variable `b` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "b"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } }
|
2019-08-09 20:04:12 +03:00
|
|
|
|> Lint.Test.whenFixed "module SomeModule exposing (a, b)\na = let \n c = 2\n in c"
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2019-08-09 20:04:12 +03:00
|
|
|
, test "should report unused function from let even if they are exposed by name" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a, b)
|
2018-11-22 21:19:19 +03:00
|
|
|
a = let b param = 1
|
|
|
|
in 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "`let in` variable `b` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "b"
|
|
|
|
}
|
2019-07-24 19:58:56 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } }
|
2019-08-09 20:04:12 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a, b)
|
|
|
|
a = 2"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused variables from let even if everything is exposed" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (..)
|
2018-11-22 21:19:19 +03:00
|
|
|
a = let b = 1
|
|
|
|
in 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "`let in` variable `b` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "b"
|
|
|
|
}
|
2019-08-09 20:04:12 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (..)
|
|
|
|
a = 2"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report variables from let declarations that are used in the expression" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
a = let c = 1
|
|
|
|
in c"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
topLevelVariablesUsedInLetInTests : List Test
|
|
|
|
topLevelVariablesUsedInLetInTests =
|
|
|
|
[ test "should not report top-level variables used inside a let expression" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
b = 1
|
|
|
|
a = let c = 1
|
2019-07-24 19:58:56 +03:00
|
|
|
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" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
b = 1
|
|
|
|
a = let c = b
|
2019-07-24 19:58:56 +03:00
|
|
|
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" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
b = 1
|
|
|
|
a = let
|
2019-07-24 19:58:56 +03:00
|
|
|
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-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
recordUpdateTests : List Test
|
|
|
|
recordUpdateTests =
|
|
|
|
[ test "should not report variables used in a record update expression's value to be updated" <|
|
2019-07-02 12:16:03 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-02 12:16:03 +03:00
|
|
|
b = { c = 1 }
|
|
|
|
a = { b | c = 3 }"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report variables used in a record update expression's updates" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-02 12:16:03 +03:00
|
|
|
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" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-02 12:16:03 +03:00
|
|
|
b = { z = 1, c = 2 }
|
|
|
|
c = 1
|
|
|
|
a = { b | c = 3 }"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "Top-level variable `c` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-02 12:16:03 +03:00
|
|
|
, under = "c"
|
|
|
|
}
|
|
|
|
|> Lint.Test.atExactly { start = { row = 3, column = 1 }, end = { row = 3, column = 2 } }
|
2019-08-05 01:04:27 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
b = { z = 1, c = 2 }
|
|
|
|
|
|
|
|
a = { b | c = 3 }"""
|
2019-07-02 12:16:03 +03:00
|
|
|
]
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
functionParameterTests : List Test
|
|
|
|
functionParameterTests =
|
|
|
|
[ test "should not report unused function parameters" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
a n = 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
importTests : List Test
|
|
|
|
importTests =
|
|
|
|
[ test "should report unused imported functions" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (b)
|
2018-11-22 21:19:19 +03:00
|
|
|
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"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "a"
|
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (b)
|
|
|
|
import Foo """
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused imported functions (multiple imports)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (d)
|
2018-11-22 21:19:19 +03:00
|
|
|
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"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-03 14:44:51 +03:00
|
|
|
, under = "C"
|
2019-06-30 13:49:03 +03:00
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (d)
|
|
|
|
import Foo exposing (a, b)"""
|
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"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "a"
|
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (d)
|
|
|
|
import Foo exposing (C, b)"""
|
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"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-03 14:44:51 +03:00
|
|
|
, under = "b"
|
2019-06-30 13:49:03 +03:00
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (d)
|
|
|
|
import Foo exposing (C, a)"""
|
|
|
|
]
|
|
|
|
, test "should report unused imported functions (multiple imports on several lines)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """module SomeModule exposing (d)
|
|
|
|
import Foo
|
|
|
|
exposing
|
|
|
|
( C
|
|
|
|
, a
|
|
|
|
, b
|
|
|
|
)"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
|
|
|
{ message = "Imported type `C` is not used"
|
|
|
|
, details = details
|
|
|
|
, under = "C\n "
|
|
|
|
}
|
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (d)
|
|
|
|
import Foo
|
|
|
|
exposing
|
|
|
|
( a
|
|
|
|
, b
|
|
|
|
)"""
|
|
|
|
, Lint.Test.error
|
|
|
|
{ message = "Imported variable `a` is not used"
|
|
|
|
, details = details
|
|
|
|
, under = "a"
|
|
|
|
}
|
|
|
|
|> Lint.Test.whenFixed
|
|
|
|
("""module SomeModule exposing (d)
|
|
|
|
import Foo
|
|
|
|
exposing
|
|
|
|
( C
|
|
|
|
"""
|
|
|
|
++ " "
|
|
|
|
++ """
|
|
|
|
, b
|
|
|
|
)"""
|
|
|
|
)
|
|
|
|
, Lint.Test.error
|
|
|
|
{ message = "Imported variable `b` is not used"
|
|
|
|
, details = details
|
|
|
|
, under = "b"
|
|
|
|
}
|
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (d)
|
|
|
|
import Foo
|
|
|
|
exposing
|
|
|
|
( C
|
|
|
|
, a
|
|
|
|
)"""
|
2018-11-22 21:19:19 +03:00
|
|
|
]
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should report unused operator import" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import Parser exposing ((</>))"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
|
|
|
{ message = "Imported operator `</>` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-24 19:58:56 +03:00
|
|
|
, under = "(</>)"
|
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
import Parser """
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
, test "should report unused import" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import Html"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
|
|
|
{ message = "Imported module `Html` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-24 19:58:56 +03:00
|
|
|
, under = "Html"
|
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed "module SomeModule exposing (a)\n"
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
, test "should report unused import (multiples segments)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import Html.Styled.Attributes"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
|
|
|
{ message = "Imported module `Html.Styled.Attributes` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-24 19:58:56 +03:00
|
|
|
, under = "Html.Styled.Attributes"
|
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed "module SomeModule exposing (a)\n"
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
, test "should not report import if it exposes all (should be improved by detecting if any exposed value is used)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import Html.Styled.Attributes exposing (..)"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report used import (function access)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import Html.Styled.Attributes
|
|
|
|
a = Html.Styled.Attributes.href"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report unused import if it is aliased" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import Html.Styled.Attributes as Html
|
|
|
|
a = Html.href"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should report unused import alias" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import Html.Styled.Attributes as Html"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
|
|
|
{ message = "Module alias `Html` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-24 19:58:56 +03:00
|
|
|
, under = "Html"
|
|
|
|
}
|
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 34 }, end = { row = 2, column = 38 } }
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
import Html.Styled.Attributes"""
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
2019-07-25 10:14:33 +03:00
|
|
|
, test "should report unused import alias even if it exposes a used type" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-25 10:14:33 +03:00
|
|
|
import Html.Styled.Attributes as Html exposing (Attribute)
|
|
|
|
a : Attribute
|
|
|
|
a = ()"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
|
|
|
{ message = "Module alias `Html` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-25 10:14:33 +03:00
|
|
|
, under = "Html"
|
|
|
|
}
|
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 34 }, end = { row = 2, column = 38 } }
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
import Html.Styled.Attributes exposing (Attribute)
|
|
|
|
a : Attribute
|
|
|
|
a = ()"""
|
2019-07-25 10:14:33 +03:00
|
|
|
]
|
2019-07-25 11:40:23 +03:00
|
|
|
, test "should report unused import alias even if it is named like an exposed type" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-25 11:40:23 +03:00
|
|
|
import Html.Styled as Html exposing (Html)
|
|
|
|
a : Html
|
|
|
|
a = ()"""
|
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
|
|
|
{ message = "Module alias `Html` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-07-25 11:40:23 +03:00
|
|
|
, under = "Html"
|
|
|
|
}
|
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 23 }, end = { row = 2, column = 27 } }
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
import Html.Styled exposing (Html)
|
|
|
|
a : Html
|
|
|
|
a = ()"""
|
2019-07-25 11:40:23 +03:00
|
|
|
]
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report import that exposes a used exposed type" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import B exposing (C(..))
|
|
|
|
a : C
|
|
|
|
a = 1"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report import that exposes an unused exposed type (but whose subtype is potentially used)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import B exposing (C(..))
|
|
|
|
a : D
|
|
|
|
a = 1"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
patternMatchingVariablesTests : List Test
|
|
|
|
patternMatchingVariablesTests =
|
|
|
|
[ test "should not report unused pattern matching parameters" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
a = case thing of
|
|
|
|
Foo b c -> []"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-25 01:19:43 +03:00
|
|
|
, test "should not report unused variable when used as the expression in a case expression" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-25 01:19:43 +03:00
|
|
|
b = 1
|
|
|
|
a =
|
|
|
|
case b of
|
|
|
|
_ -> 2"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report unused type when it is used in a pattern matching pattern" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-25 01:19:43 +03:00
|
|
|
type Bar = Baz
|
|
|
|
|
|
|
|
a =
|
|
|
|
case () of
|
|
|
|
Baz ->
|
|
|
|
[]"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report unused type when it is used in a pattern matching pattern (sub-pattern)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-25 01:19:43 +03:00
|
|
|
type Bar = Baz
|
|
|
|
|
|
|
|
a =
|
|
|
|
case () of
|
|
|
|
Just (Baz range) ->
|
|
|
|
[]"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report unused import when a type from it is used in a pattern matching pattern" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-25 01:19:43 +03:00
|
|
|
import Bar
|
|
|
|
|
|
|
|
a =
|
|
|
|
case () of
|
|
|
|
Just (Bar.Baz range) ->
|
|
|
|
[]"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
|
2018-11-22 21:19:19 +03:00
|
|
|
|
2019-07-24 19:58:56 +03:00
|
|
|
typeTests : List Test
|
|
|
|
typeTests =
|
|
|
|
[ test "should report unused custom type declarations" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-08-19 14:53:35 +03:00
|
|
|
type A = B | C
|
|
|
|
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 = "Type `A` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "A"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 6 }, end = { row = 2, column = 7 } }
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
|
|
|
|
a = 1"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report unused custom type constructors" <|
|
|
|
|
-- This is handled by the `NoUnusedTypeConstructors` rule
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (A)
|
2019-07-24 19:58:56 +03:00
|
|
|
type A = B | C"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
2018-11-22 21:19:19 +03:00
|
|
|
, test "should report unused type aliases declarations" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-08-19 14:53:35 +03:00
|
|
|
type alias A = { a : 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 = "Type `A` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "A"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 12 }, end = { row = 2, column = 13 } }
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
|
|
|
|
a = 1"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report type alias used in a signature" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
type alias A = { a : B }
|
|
|
|
a : A
|
|
|
|
a = {a = 1}"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report type alias used in a signature with multiple arguments" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
type alias A = { a : B }
|
|
|
|
a : String -> A
|
|
|
|
a str = {a = str}"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report custom type used in a signature" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
type A = B | C
|
|
|
|
a : A
|
|
|
|
a = {a = 1}"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report custom type used in a signature with multiple arguments" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
type A = B | C
|
|
|
|
a : String -> A
|
|
|
|
a str = {a = str}"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report parameterized custom type used in a signature" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
type CustomMaybe a = B a | C a
|
|
|
|
a : CustomMaybe D
|
2018-11-22 21:19:19 +03:00
|
|
|
a = []"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report type alias used in a signature with parameterized types (as parameter)" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
type alias A = { a : B }
|
|
|
|
a : List A
|
|
|
|
a = []"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report custom type used in a signature with parameterized types (as parameter)" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
type A = B | C
|
|
|
|
a : List A
|
|
|
|
a = []"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report type alias used in a signature with a record" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
type alias A = { a : B }
|
|
|
|
a : { c: A }
|
|
|
|
a str = {c = str}"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report custom type used in a signature with a record" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
type A = B | C
|
|
|
|
a : { c: A }
|
|
|
|
a str = {c = str}"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report type alias used in a signature with a generic record" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
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-24 19:58:56 +03:00
|
|
|
, test "should not report custom type used in a signature with a generic record" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
type A = B | C
|
|
|
|
a : { r | c: A }
|
|
|
|
a str = {c = str}"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report type alias used in a custom type constructor definition" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (ExposedType)
|
2019-07-24 19:58:56 +03:00
|
|
|
type alias A = { a : B }
|
|
|
|
type ExposedType = Something A
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report custom type used in a custom type constructor definition" <|
|
2019-07-02 12:42:07 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule 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
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report custom type of which a constructor is used" <|
|
2019-07-05 01:33:51 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (b)
|
2019-07-05 01:33:51 +03:00
|
|
|
type A = B | C | D
|
|
|
|
b = B
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report custom type of which a constructor is used even if it was defined afterwards" <|
|
2019-07-05 01:33:51 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (b)
|
2019-07-05 01:33:51 +03:00
|
|
|
b = B
|
|
|
|
type A = B | C | D
|
2019-07-02 12:55:49 +03:00
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report type alias used in type signature inside a let..in" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
type alias A = { a : B }
|
|
|
|
a = let
|
|
|
|
b : A
|
|
|
|
b = 1
|
|
|
|
in
|
|
|
|
b
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report custom type used in type signature inside a let..in" <|
|
2019-07-02 12:55:49 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-02 12:55:49 +03:00
|
|
|
type A = A
|
|
|
|
a = let
|
|
|
|
b : A
|
|
|
|
b = 1
|
|
|
|
in
|
|
|
|
b
|
2019-07-02 12:42:07 +03:00
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report type alias used in a type alias field" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (ExposedType)
|
2019-07-24 19:58:56 +03:00
|
|
|
type alias A = { a : B }
|
|
|
|
type alias ExposedType = { a : A }
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report custom type used in a type alias field" <|
|
2019-07-02 12:42:07 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (ExposedType)
|
2019-07-02 12:42:07 +03:00
|
|
|
type A = B | C
|
|
|
|
type alias ExposedType = { a : A }
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report type alias used in a type alias field's arguments " <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (ExposedType)
|
2019-07-24 19:58:56 +03:00
|
|
|
type alias A = { a : B }
|
|
|
|
type alias ExposedType = { a : Maybe A }
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should not report custom type used in a type alias field's arguments " <|
|
2019-07-02 12:42:07 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (ExposedType)
|
2019-07-02 12:42:07 +03:00
|
|
|
type A = B | C
|
|
|
|
type alias ExposedType = { a : Maybe A }
|
|
|
|
"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-28 15:45:09 +03:00
|
|
|
, test "should not report type alias if it is exposed" <|
|
2019-07-24 19:58:56 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (A)
|
2019-07-24 19:58:56 +03:00
|
|
|
type alias A = { a : B }"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-28 15:45:09 +03:00
|
|
|
, test "should not report custom type if it is exposed" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (A)
|
2018-11-22 21:19:19 +03:00
|
|
|
type A a = B a"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-28 15:45:09 +03:00
|
|
|
, test "should not report custom type if it is exposed with its sub-types" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (A(..))
|
2018-11-22 21:19:19 +03:00
|
|
|
type A = B | C | D"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-28 15:45:09 +03:00
|
|
|
, test "should report unused variable even if it is named like a custom type parameter" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (A)
|
2018-11-22 21:19:19 +03:00
|
|
|
a = 1
|
|
|
|
type A a = B a"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectErrors
|
|
|
|
[ Lint.Test.error
|
2019-08-09 20:04:12 +03:00
|
|
|
{ message = "Top-level variable `a` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "a"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 1 }, end = { row = 2, column = 2 } }
|
2019-08-05 01:04:27 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (A)
|
|
|
|
|
|
|
|
type A a = B a"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2019-07-28 15:45:09 +03:00
|
|
|
, test "should report unused variable even if it is present in a generic record type" <|
|
2018-11-22 21:19:19 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-22 21:19:19 +03:00
|
|
|
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-08-09 20:04:12 +03:00
|
|
|
{ message = "Top-level variable `r` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "r"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 1 }, end = { row = 2, column = 2 } }
|
2019-08-05 01:04:27 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
|
|
|
|
a : { r | c: A }
|
|
|
|
a str = {c = str}"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
opaqueTypeTests : List Test
|
|
|
|
opaqueTypeTests =
|
|
|
|
[ test "should report unused opaque types" <|
|
2018-11-24 19:41:13 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-08-19 14:53:35 +03:00
|
|
|
type A = 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 = "Type `A` is not used"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "A"
|
|
|
|
}
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.atExactly { start = { row = 2, column = 6 }, end = { row = 2, column = 7 } }
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """module SomeModule exposing (a)
|
|
|
|
|
|
|
|
a = 1"""
|
2019-06-30 13:49:03 +03:00
|
|
|
]
|
2018-11-24 19:41:13 +03:00
|
|
|
, test "should not report used opaque types" <|
|
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2018-11-24 19:41:13 +03:00
|
|
|
type A = A Int
|
|
|
|
a : A
|
|
|
|
a = 1"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
operatorTests : List Test
|
|
|
|
operatorTests =
|
|
|
|
[ test "should not report used operator (infix)" <|
|
2018-11-26 20:12:36 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import Parser exposing ((</>))
|
|
|
|
a = 1 </> 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
, test "should not report used operator (prefix)" <|
|
2018-11-26 20:12:36 +03:00
|
|
|
\() ->
|
2019-07-25 12:16:54 +03:00
|
|
|
testRule """module SomeModule exposing (a)
|
2019-07-24 19:58:56 +03:00
|
|
|
import Parser exposing ((</>))
|
|
|
|
a = (</>) 2"""
|
2019-07-02 01:04:45 +03:00
|
|
|
|> Lint.Test.expectNoErrors
|
2019-07-24 19:58:56 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
portTests : List Test
|
|
|
|
portTests =
|
|
|
|
[ test "should not report types that are used in ports" <|
|
2018-11-26 21:09:18 +03:00
|
|
|
\() ->
|
2019-08-19 14:53:35 +03:00
|
|
|
testRule """port module SomeModule exposing (output, input)
|
2018-11-26 21:09:18 +03:00
|
|
|
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
|
2019-08-19 14:53:35 +03:00
|
|
|
, test "should not report used ports" <|
|
2018-11-26 21:09:18 +03:00
|
|
|
\() ->
|
2019-08-19 14:53:35 +03:00
|
|
|
testRule """port module SomeModule exposing (a, subscriptions)
|
2018-11-26 21:09:18 +03:00
|
|
|
import Json.Decode
|
2019-08-19 14:53:35 +03:00
|
|
|
port output : () -> Cmd msg
|
|
|
|
port input : (Json.Decode.Value -> msg) -> Sub msg
|
|
|
|
|
|
|
|
a = output ()
|
|
|
|
subscriptions = input GotInput"""
|
|
|
|
|> Lint.Test.expectNoErrors
|
|
|
|
, test "should report unused ports (ingoing)" <|
|
|
|
|
\() ->
|
|
|
|
testRule """port module SomeModule exposing (a)
|
|
|
|
a = 1
|
|
|
|
port input : (() -> 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)"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "input"
|
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """port module SomeModule exposing (a)
|
|
|
|
a = 1
|
|
|
|
"""
|
2018-11-26 21:09:18 +03:00
|
|
|
]
|
|
|
|
, test "should report unused ports (outgoing)" <|
|
|
|
|
\() ->
|
2019-08-19 14:53:35 +03:00
|
|
|
testRule """port module SomeModule exposing (a)
|
|
|
|
a = 1
|
|
|
|
port output : String -> 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)"
|
2019-07-28 01:16:57 +03:00
|
|
|
, details = details
|
2019-06-30 13:49:03 +03:00
|
|
|
, under = "output"
|
|
|
|
}
|
2019-08-19 14:53:35 +03:00
|
|
|
|> Lint.Test.whenFixed """port module SomeModule exposing (a)
|
|
|
|
a = 1
|
|
|
|
"""
|
2018-11-26 21:09:18 +03:00
|
|
|
]
|
2018-11-22 21:19:19 +03:00
|
|
|
]
|