From 9923ec0368e901903b1a2c26f1e6095933cabb1c Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Mon, 24 Jun 2019 00:32:27 +0200 Subject: [PATCH] Use new Rule API in existing rules --- src/Lint/Rule/DefaultPatternPosition.elm | 15 ++--- src/Lint/Rule/NoDebug.elm | 13 +--- src/Lint/Rule/NoExtraBooleanComparison.elm | 13 +--- src/Lint/Rule/NoImportingEverything.elm | 13 +--- src/Lint/Rule/NoUnusedVariables.elm | 74 ++++++++++------------ tests/NoUnusedVariablesTest.elm | 4 +- 6 files changed, 50 insertions(+), 82 deletions(-) diff --git a/src/Lint/Rule/DefaultPatternPosition.elm b/src/Lint/Rule/DefaultPatternPosition.elm index e3633043..6c230403 100644 --- a/src/Lint/Rule/DefaultPatternPosition.elm +++ b/src/Lint/Rule/DefaultPatternPosition.elm @@ -58,9 +58,8 @@ module Lint.Rule.DefaultPatternPosition exposing (rule, Configuration, PatternPo import Elm.Syntax.Expression exposing (Expression(..)) import Elm.Syntax.Node as Node exposing (Node) import Elm.Syntax.Pattern exposing (Pattern(..)) -import Lint exposing (Rule, lint) import Lint.Error as Error exposing (Error) -import Lint.Rule as Rule +import Lint.Rule2 as Rule exposing (Rule) import List.Extra exposing (findIndex) import Regex @@ -83,15 +82,9 @@ type alias Configuration = -} rule : Configuration -> Rule rule config = - Lint.createRule - "DefaultPatternPosition" - (lint (implementation config)) - - -implementation : Configuration -> Rule.Implementation () -implementation configuration = - Rule.createSimple - |> Rule.withSimpleExpressionVisitor (expressionVisitor configuration) + Rule.newRuleSchema "DefaultPatternPosition" + |> Rule.withSimpleExpressionVisitor (expressionVisitor config) + |> Rule.fromSchema error : Node a -> String -> Error diff --git a/src/Lint/Rule/NoDebug.elm b/src/Lint/Rule/NoDebug.elm index 906e8876..8b252b81 100644 --- a/src/Lint/Rule/NoDebug.elm +++ b/src/Lint/Rule/NoDebug.elm @@ -32,9 +32,8 @@ module Lint.Rule.NoDebug exposing (rule) import Elm.Syntax.Expression exposing (Expression(..)) import Elm.Syntax.Node exposing (Node, range, value) -import Lint exposing (Rule, lint) import Lint.Error as Error exposing (Error) -import Lint.Rule as Rule +import Lint.Rule2 as Rule exposing (Rule) {-| Forbid the use of `Debug` before it goes into production. @@ -46,15 +45,9 @@ import Lint.Rule as Rule -} rule : Rule rule = - Lint.createRule - "NoDebug" - (lint implementation) - - -implementation : Rule.Implementation () -implementation = - Rule.createSimple + Rule.newRuleSchema "NoDebug" |> Rule.withSimpleExpressionVisitor expressionVisitor + |> Rule.fromSchema error : Node a -> Error diff --git a/src/Lint/Rule/NoExtraBooleanComparison.elm b/src/Lint/Rule/NoExtraBooleanComparison.elm index 0b8b0ce8..c68676ed 100644 --- a/src/Lint/Rule/NoExtraBooleanComparison.elm +++ b/src/Lint/Rule/NoExtraBooleanComparison.elm @@ -32,9 +32,8 @@ module Lint.Rule.NoExtraBooleanComparison exposing (rule) import Elm.Syntax.Expression as Expression exposing (Expression(..)) import Elm.Syntax.Node as Node exposing (Node) -import Lint exposing (Rule, lint) import Lint.Error as Error exposing (Error) -import Lint.Rule as Rule +import Lint.Rule2 as Rule exposing (Rule) {-| Forbid the use of `Debug` before it goes into production. @@ -46,15 +45,9 @@ import Lint.Rule as Rule -} rule : Rule rule = - Lint.createRule - "NoExtraBooleanComparison" - (lint implementation) - - -implementation : Rule.Implementation () -implementation = - Rule.createSimple + Rule.newRuleSchema "NoExtraBooleanComparison" |> Rule.withSimpleExpressionVisitor expressionVisitor + |> Rule.fromSchema error : String -> Node a -> Error diff --git a/src/Lint/Rule/NoImportingEverything.elm b/src/Lint/Rule/NoImportingEverything.elm index ed859dd2..ccaa0a0b 100644 --- a/src/Lint/Rule/NoImportingEverything.elm +++ b/src/Lint/Rule/NoImportingEverything.elm @@ -20,9 +20,8 @@ import Elm.Syntax.Exposing as Exposing import Elm.Syntax.Import exposing (Import) import Elm.Syntax.Node as Node exposing (Node) import Elm.Syntax.Range exposing (Range) -import Lint exposing (Rule, lint) import Lint.Error as Error exposing (Error) -import Lint.Rule as Rule exposing (Implementation) +import Lint.Rule2 as Rule exposing (Rule) import Lint.Util as Util @@ -42,15 +41,9 @@ functions and types are unknown to them. -} rule : Configuration -> Rule rule config = - Lint.createRule - "NoImportingEverything" - (lint (implementation config)) - - -implementation : Configuration -> Implementation () -implementation config = - Rule.createSimple + Rule.newRuleSchema "NoImportingEverything" |> Rule.withSimpleImportVisitor (importVisitor config) + |> Rule.fromSchema error : Range -> String -> Error diff --git a/src/Lint/Rule/NoUnusedVariables.elm b/src/Lint/Rule/NoUnusedVariables.elm index 2b365c25..802dc9fa 100644 --- a/src/Lint/Rule/NoUnusedVariables.elm +++ b/src/Lint/Rule/NoUnusedVariables.elm @@ -30,10 +30,9 @@ import Elm.Syntax.Module as Module exposing (Module(..)) import Elm.Syntax.Node exposing (Node, range, value) import Elm.Syntax.Range exposing (Range) import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..)) -import Lint exposing (Rule, lint) import Lint.Direction as Direction exposing (Direction) import Lint.Error as Error exposing (Error) -import Lint.Rule as Rule +import Lint.Rule2 as Rule exposing (Rule) import List.Nonempty as Nonempty exposing (Nonempty) import Set exposing (Set) @@ -47,9 +46,26 @@ import Set exposing (Set) -} rule : Rule rule = - Lint.createRule - "NoUnusedVariables" - (lint implementation) + Rule.newRuleSchema "NoUnusedVariables" + |> Rule.withInitialContext initialContext + |> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor + |> Rule.withImportVisitor importVisitor + |> Rule.withExpressionVisitor expressionVisitor + |> Rule.withDeclarationVisitor declarationVisitor + |> Rule.withFinalEvaluation finalEvaluation + |> Rule.fromSchema + + +type alias Context = + { scopes : Nonempty Scope + , exposesEverything : Bool + } + + +type alias Scope = + { declared : Dict String ( VariableType, Range ) + , used : Set String + } type VariableType @@ -63,15 +79,10 @@ type VariableType | Port -type alias Scope = - { declared : Dict String ( VariableType, Range ) - , used : Set String - } - - -type alias Context = - { scopes : Nonempty Scope - , exposesEverything : Bool +initialContext : Context +initialContext = + { scopes = Nonempty.fromElement emptyScope + , exposesEverything = False } @@ -143,25 +154,8 @@ variableTypeWarning value = " (Warning: Removing this port may break your application if it is used in the JS code)" -initialContext : Context -initialContext = - { scopes = Nonempty.fromElement emptyScope - , exposesEverything = False - } - - -implementation : Rule.Implementation Context -implementation = - Rule.create initialContext - |> Rule.withModuleDefinitionVisitor moduleDefinitionVisitor - |> Rule.withImportVisitor importVisitor - |> Rule.withExpressionVisitor expressionVisitor - |> Rule.withDeclarationVisitor declarationVisitor - |> Rule.withFinalEvaluation finalEvaluation - - -moduleDefinitionVisitor : Context -> Node Module -> ( List Error, Context ) -moduleDefinitionVisitor context moduleNode = +moduleDefinitionVisitor : Node Module -> Context -> ( List Error, Context ) +moduleDefinitionVisitor moduleNode context = case Module.exposingList (value moduleNode) of All _ -> ( [], { context | exposesEverything = True } ) @@ -190,8 +184,8 @@ moduleDefinitionVisitor context moduleNode = ( [], markAllAsUsed names context ) -importVisitor : Context -> Node Import -> ( List Error, Context ) -importVisitor context node = +importVisitor : Node Import -> Context -> ( List Error, Context ) +importVisitor node context = let exposed = node @@ -226,8 +220,8 @@ importVisitor context node = ) -expressionVisitor : Context -> Direction -> Node Expression -> ( List Error, Context ) -expressionVisitor context direction node = +expressionVisitor : Node Expression -> Direction -> Context -> ( List Error, Context ) +expressionVisitor node direction context = case ( direction, value node ) of ( Direction.Enter, FunctionOrValue [] name ) -> ( [], markAsUsed name context ) @@ -274,8 +268,8 @@ expressionVisitor context direction node = ( [], context ) -declarationVisitor : Context -> Direction -> Node Declaration -> ( List Error, Context ) -declarationVisitor context direction node = +declarationVisitor : Node Declaration -> Direction -> Context -> ( List Error, Context ) +declarationVisitor node direction context = case ( direction, value node ) of ( Direction.Enter, FunctionDeclaration function ) -> let @@ -446,10 +440,12 @@ getModuleName name = makeReport : Scope -> ( List Error, List String ) makeReport { declared, used } = let + nonUsedVars : List String nonUsedVars = Set.diff used (Set.fromList <| Dict.keys declared) |> Set.toList + errors : List Error errors = Dict.filter (\key _ -> not <| Set.member key used) declared |> Dict.toList diff --git a/tests/NoUnusedVariablesTest.elm b/tests/NoUnusedVariablesTest.elm index 9225bfa6..4e61d5f2 100644 --- a/tests/NoUnusedVariablesTest.elm +++ b/tests/NoUnusedVariablesTest.elm @@ -147,9 +147,9 @@ import Foo exposing (a)""" testRule """module A exposing (d) import Foo exposing (C, a, b)""" |> TestUtil.expectErrors - [ Error.create "Imported type `C` is not used" (TestUtil.location ( 2, 22 ) ( 2, 23 )) + [ Error.create "Imported variable `b` is not used" (TestUtil.location ( 2, 28 ) ( 2, 29 )) , Error.create "Imported variable `a` is not used" (TestUtil.location ( 2, 25 ) ( 2, 26 )) - , Error.create "Imported variable `b` is not used" (TestUtil.location ( 2, 28 ) ( 2, 29 )) + , Error.create "Imported type `C` is not used" (TestUtil.location ( 2, 22 ) ( 2, 23 )) ] -- Needs to be improved, every case should create a new scope stack