mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-11-27 02:19:27 +03:00
Use new Rule API in existing rules
This commit is contained in:
parent
7bea2a6e00
commit
9923ec0368
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user