From 95f4989fa9fc485cc585ad93bf119700dd665cc8 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Mon, 26 Nov 2018 19:16:04 +0100 Subject: [PATCH] Naming --- src/Lint/Rule/NoUnusedVariables.elm | 45 +++++++++++++++++++++-------- tests/NoUnusedVariablesTest.elm | 10 +++---- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/Lint/Rule/NoUnusedVariables.elm b/src/Lint/Rule/NoUnusedVariables.elm index 500316d1..0b239359 100644 --- a/src/Lint/Rule/NoUnusedVariables.elm +++ b/src/Lint/Rule/NoUnusedVariables.elm @@ -49,16 +49,19 @@ rule input = lint input implementation -type Value +type VariableType = Variable | ImportedModule + | ImportedVariable + | ImportedType + | ImportedOperator | ModuleAlias | Type | Port type alias Scope = - { declared : Dict String ( Value, Range ) + { declared : Dict String ( VariableType, Range ) , used : Set String } @@ -74,7 +77,7 @@ emptyScope = Scope Dict.empty Set.empty -error : Value -> Range -> String -> Error +error : VariableType -> Range -> String -> Error error variableType range_ name = Error "NoUnusedVariables" @@ -82,7 +85,7 @@ error variableType range_ name = range_ -variableTypeToString : Value -> String +variableTypeToString : VariableType -> String variableTypeToString value = case value of Variable -> @@ -91,6 +94,15 @@ variableTypeToString value = ImportedModule -> "Imported module" + ImportedVariable -> + "Imported variable" + + ImportedType -> + "Imported type" + + ImportedOperator -> + "Imported operator" + ModuleAlias -> "Module alias" @@ -101,7 +113,7 @@ variableTypeToString value = "Port" -variableTypeWarning : Value -> String +variableTypeWarning : VariableType -> String variableTypeWarning value = case value of Variable -> @@ -110,6 +122,15 @@ variableTypeWarning value = ImportedModule -> "" + ImportedVariable -> + "" + + ImportedType -> + "" + + ImportedOperator -> + "" + ModuleAlias -> "" @@ -202,7 +223,7 @@ visitImport ctx node = Just declaredImports -> ( [] , List.foldl - (\( range, name ) context -> register Variable range name context) + (\( variableType, range, name ) context -> register variableType range name context) ctx (collectFromExposing declaredImports) ) @@ -324,7 +345,7 @@ registerFunction function ctx = register Variable (range declaration.name) (value declaration.name) ctx -collectFromExposing : Exposing -> List ( Range, String ) +collectFromExposing : Exposing -> List ( VariableType, Range, String ) collectFromExposing exposing_ = case exposing_ of All _ -> @@ -335,13 +356,13 @@ collectFromExposing exposing_ = (\node -> case value node of FunctionExpose name -> - Just ( range node, name ) + Just ( ImportedVariable, range node, name ) InfixExpose name -> - Just ( range node, name ) + Just ( ImportedOperator, range node, name ) TypeOrAliasExpose name -> - Just ( range node, name ) + Just ( ImportedType, range node, name ) TypeExpose { name, open } -> case open of @@ -349,7 +370,7 @@ collectFromExposing exposing_ = Nothing Nothing -> - Just ( range node, name ) + Just ( ImportedType, range node, name ) ) list @@ -393,7 +414,7 @@ collectNamesFromTypeAnnotation node = [] -register : Value -> Range -> String -> Context -> Context +register : VariableType -> Range -> String -> Context -> Context register variableType range name ctx = let scopes = diff --git a/tests/NoUnusedVariablesTest.elm b/tests/NoUnusedVariablesTest.elm index 6aac147f..3e2c6f91 100644 --- a/tests/NoUnusedVariablesTest.elm +++ b/tests/NoUnusedVariablesTest.elm @@ -154,15 +154,15 @@ a n = 1""" \() -> testRule """module A exposing (b) import Foo exposing (a)""" - |> expectErrors [ error "Variable `a` is not used" (location 2 22 23) ] + |> expectErrors [ error "Imported variable `a` is not used" (location 2 22 23) ] , test "should report unused imported functions (multiple imports)" <| \() -> testRule """module A exposing (d) import Foo exposing (C, a, b)""" |> expectErrors - [ 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 "Imported type `C` is not used" (location 2 22 23) + , error "Imported variable `a` is not used" (location 2 25 26) + , error "Imported variable `b` is not used" (location 2 28 29) ] -- Needs to be improved, every case should create a new scope stack @@ -254,7 +254,7 @@ a str = {c = str}""" \() -> testRule """module A exposing (a) import Parser exposing (())""" - |> expectErrors [ error "Variable `` is not used" (location 2 25 30) ] + |> expectErrors [ error "Imported operator `` is not used" (location 2 25 30) ] , test "should not report used operator (infix)" <| \() -> testRule """module A exposing (a)