This commit is contained in:
Jeroen Engels 2018-11-26 18:12:36 +01:00
parent c7816e002b
commit 245b22455d
2 changed files with 46 additions and 51 deletions

View File

@ -58,7 +58,6 @@ type alias Scope =
type alias Context = type alias Context =
{ scopes : Nonempty Scope { scopes : Nonempty Scope
, exposesEverything : Bool , exposesEverything : Bool
, imports : Dict String Range
} }
@ -76,7 +75,6 @@ initialContext : Context
initialContext = initialContext =
{ scopes = Nonempty.fromElement emptyScope { scopes = Nonempty.fromElement emptyScope
, exposesEverything = False , exposesEverything = False
, imports = Dict.empty
} }
@ -132,38 +130,27 @@ visitImport ctx node =
node node
|> value |> value
|> .exposingList |> .exposingList
declaredImports =
exposed
|> Maybe.map (value >> collectFromExposing)
|> Maybe.withDefault []
moduleName =
Maybe.withDefault (value node |> .moduleName) (value node |> .moduleAlias)
in in
case Maybe.map value exposed of case Maybe.map value exposed of
Just (All _) -> Nothing ->
-- Do not attempt to report an import that exposes all let
( [], ctx ) moduleName =
Maybe.withDefault (value node |> .moduleName) (value node |> .moduleAlias)
in
( []
, register
(range moduleName)
(value moduleName |> getModuleName)
ctx
)
_ -> Just declaredImports ->
if List.isEmpty declaredImports then ( []
-- Only register the module name , List.foldl
( [] (\( range, name ) context -> register range name context)
, register ctx
(range moduleName) (collectFromExposing declaredImports)
(value moduleName |> getModuleName) )
ctx
)
else
-- Only register the exposed variables
( []
, List.foldl
(\( range, name ) context -> register range name context)
ctx
declaredImports
)
visitExpression : Context -> Direction -> Node Expression -> ( List Error, Context ) visitExpression : Context -> Direction -> Node Expression -> ( List Error, Context )
@ -254,7 +241,8 @@ visitEnd ctx =
else else
ctx.scopes ctx.scopes
|> Nonempty.head |> Nonempty.head
|> makeReportRoot ctx.imports |> makeReport
|> Tuple.first
in in
( errors, ctx ) ( errors, ctx )
@ -284,8 +272,16 @@ collectFromExposing exposing_ =
InfixExpose name -> InfixExpose name ->
Just ( range node, name ) Just ( range node, name )
_ -> TypeOrAliasExpose name ->
Nothing Just ( range node, name )
TypeExpose { name, open } ->
case open of
Just openRange ->
Nothing
Nothing ->
Just ( range node, name )
) )
list list
@ -371,22 +367,6 @@ makeReport { declared, used } =
( errors, nonUsedVars ) ( errors, nonUsedVars )
makeReportRoot : Dict String Range -> Scope -> List Error
makeReportRoot imports { declared, used } =
let
nonUsedVariablesErrors =
Dict.filter (\key _ -> not <| Set.member key used) declared
|> Dict.toList
|> List.map (\( key, range ) -> error range key)
nonUsedImportErrors =
Dict.filter (\key _ -> not <| Set.member key used) imports
|> Dict.toList
|> List.map (\( key, range ) -> error range key)
in
nonUsedImportErrors ++ nonUsedVariablesErrors
mapNonemptyHead : (a -> a) -> Nonempty a -> Nonempty a mapNonemptyHead : (a -> a) -> Nonempty a -> Nonempty a
mapNonemptyHead fn nonempty = mapNonemptyHead fn nonempty =
let let

View File

@ -160,7 +160,8 @@ import Foo exposing (a)"""
testRule """module A exposing (d) testRule """module A exposing (d)
import Foo exposing (C, a, b)""" import Foo exposing (C, a, b)"""
|> expectErrors |> expectErrors
[ error "Variable `a` is not used" (location 2 25 26) [ error "Variable `C` is not used" (location 2 22 23)
, error "Variable `a` is not used" (location 2 25 26)
, error "Variable `b` is not used" (location 2 28 29) , error "Variable `b` is not used" (location 2 28 29)
] ]
@ -316,6 +317,20 @@ a = Html.href"""
testRule """module A exposing (a) testRule """module A exposing (a)
import Html.Styled.Attributes as Html""" import Html.Styled.Attributes as Html"""
|> expectErrors [ error "Variable `Html` is not used" (location 2 34 38) ] |> expectErrors [ error "Variable `Html` is not used" (location 2 34 38) ]
, test "should not report import that exposes a used exposed type" <|
\() ->
testRule """module A exposing (a)
import B exposing (C(..))
a : C
a = 1"""
|> expectErrors []
, 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"""
|> expectErrors []
] ]