elm-review/src/Lint/Rules/NoDebug.elm

80 lines
1.4 KiB
Elm
Raw Normal View History

2017-01-29 22:03:31 +03:00
module Lint.Rules.NoDebug exposing (rule)
2017-01-07 23:17:01 +03:00
{-|
2018-11-05 16:44:22 +03:00
@docs rule
2018-11-05 16:44:22 +03:00
# Fail
2018-11-05 16:44:22 +03:00
if Debug.log "condition" condition then
a
else
b
if condition then
Debug.crash "Nooo!"
2018-11-05 16:44:22 +03:00
else
value
2018-11-05 16:44:22 +03:00
# Success
if condition then
a
2018-11-05 16:44:22 +03:00
else
b
2018-11-05 16:44:22 +03:00
-}
2018-11-05 21:00:17 +03:00
import Elm.Syntax.Expression exposing (Expression(..))
import Elm.Syntax.Node exposing (Node, range, value)
2018-11-11 01:37:18 +03:00
import Lint exposing (Rule, lint)
import Lint.Error exposing (Error)
import Lint.Types exposing (Direction(..), LintRuleImplementation, createRule)
2017-01-07 23:17:01 +03:00
type alias Context =
{}
{-| Forbid the use of `Debug` before it goes into production.
rules =
[ NoDebug.rule
]
2018-11-05 16:44:22 +03:00
-}
2018-11-11 01:37:18 +03:00
rule : Rule
rule input =
lint input implementation
2017-06-12 21:04:02 +03:00
implementation : LintRuleImplementation Context
implementation =
2018-11-10 19:05:17 +03:00
createRule
Context
2018-11-11 01:37:18 +03:00
(\v -> { v | visitExpression = visitExpression })
2017-01-07 23:17:01 +03:00
2018-11-11 01:37:18 +03:00
error : Node a -> Error
error node =
2018-11-11 01:37:18 +03:00
Error "NoDebug" "Forbidden use of Debug" (range node)
2018-11-11 01:37:18 +03:00
visitExpression : Context -> Direction -> Node Expression -> ( List Error, Context )
visitExpression ctx direction node =
case ( direction, value node ) of
( Enter, FunctionOrValue moduleName fnName ) ->
if List.member "Debug" moduleName then
( [ error node ], ctx )
2018-11-05 16:44:22 +03:00
else
( [], ctx )
2017-01-07 23:17:01 +03:00
_ ->
( [], ctx )