Backport elm-review-unused

This commit is contained in:
Jeroen Engels 2020-12-05 12:40:40 +01:00
parent dd1ffc5266
commit c6ca7ec9fe
4 changed files with 51 additions and 5 deletions

View File

@ -68,7 +68,7 @@ Also, if you like comparing custom types in the way described above, you might p
You can try this rule out by running the following command: You can try this rule out by running the following command:
```bash ```bash
elm - review --template jfmengels/elm-review-unused/example --rules NoUnused.CustomTypeConstructorArgs elm-review --template jfmengels/elm-review-unused/example --rules NoUnused.CustomTypeConstructorArgs
``` ```
-} -}
@ -89,7 +89,8 @@ rule =
type alias ProjectContext = type alias ProjectContext =
{ exposedModules : Set ModuleName { exposedModules : Set ModuleName
, customTypeArgs : , customTypeArgs :
Dict ModuleName Dict
ModuleName
{ moduleKey : Rule.ModuleKey { moduleKey : Rule.ModuleKey
, args : Dict String (List Range) , args : Dict String (List Range)
} }
@ -328,7 +329,8 @@ expressionVisitor node context =
let let
usedArguments : List ( ( ModuleName, String ), Set Int ) usedArguments : List ( ( ModuleName, String ), Set Int )
usedArguments = usedArguments =
List.concatMap (Tuple.first >> collectUsedCustomTypeArgs context.lookupTable) cases cases
|> List.concatMap (Tuple.first >> collectUsedCustomTypeArgs context.lookupTable)
in in
( [], { context | usedArguments = registerUsedPatterns usedArguments context.usedArguments } ) ( [], { context | usedArguments = registerUsedPatterns usedArguments context.usedArguments } )

View File

@ -594,4 +594,46 @@ foo = [ (Foo, A), (Bar, B) ]
] ]
|> Review.Test.runOnModulesWithProjectData project (rule []) |> Review.Test.runOnModulesWithProjectData project (rule [])
|> Review.Test.expectNoErrors |> Review.Test.expectNoErrors
, test "should not report imported type constructors when they are shadowed by a local type alias that does not create a function (#5)" <|
\() ->
[ """module A exposing (x)
import B exposing (Foo(..))
type alias Foo = B.Foo -- this "shadowing" is causing the bug, removing the alias removes the false positive
x = Foo 1 -- usage of the custom type constructor!
"""
, """module B exposing (Foo(..))
type Foo = Foo Int
"""
]
|> Review.Test.runOnModulesWithProjectData project (rule [])
|> Review.Test.expectNoErrors
, test "should report imported type constructors even when they are shadowed by a local type alias that creates a function (#5)" <|
\() ->
[ """module A exposing (x)
import B exposing (Foo(..))
type alias Foo = { n : Int }
x = Foo 1 -- Not a usage of B.Foo in this case!
"""
, """module B exposing (Foo(..))
type Foo = Foo Int
"""
]
|> Review.Test.runOnModulesWithProjectData project (rule [])
|> Review.Test.expectErrorsForModules
[ ( "B"
, [ Review.Test.error
{ message = "Type constructor `Foo` is not used."
, details = details
, under = "Foo"
}
|> Review.Test.atExactly { start = { row = 2, column = 12 }, end = { row = 2, column = 15 } }
]
)
]
] ]

View File

@ -81,7 +81,8 @@ moduleVisitor schema =
type alias ProjectContext = type alias ProjectContext =
{ projectType : ProjectType { projectType : ProjectType
, modules : , modules :
Dict ModuleName Dict
ModuleName
{ moduleKey : Rule.ModuleKey { moduleKey : Rule.ModuleKey
, exposed : Dict String ExposedElement , exposed : Dict String ExposedElement
} }

View File

@ -74,7 +74,8 @@ moduleVisitor schema =
type alias ProjectContext = type alias ProjectContext =
{ modules : { modules :
Dict ModuleName Dict
ModuleName
{ moduleKey : Rule.ModuleKey { moduleKey : Rule.ModuleKey
, moduleNameLocation : Range , moduleNameLocation : Range
} }