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

88 lines
1.6 KiB
Elm
Raw Normal View History

2018-11-11 01:43:58 +03:00
module Lint.Rule.NoDebug exposing (rule)
2017-01-07 23:17:01 +03:00
2019-07-03 15:19:29 +03:00
{-| Forbid the use of `Debug` before it goes into production or fails in the CI.
2018-11-05 16:44:22 +03:00
2019-07-03 15:19:29 +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
2019-07-03 15:19:29 +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
2019-07-03 15:19:29 +03:00
# Rule
@docs rule
-}
2018-11-05 21:00:17 +03:00
import Elm.Syntax.Expression exposing (Expression(..))
import Elm.Syntax.Import exposing (Import)
import Elm.Syntax.Node as Node exposing (Node)
2019-06-26 14:47:00 +03:00
import Lint.Rule as Rule exposing (Error, Rule)
2017-01-07 23:17:01 +03:00
2019-07-03 15:19:29 +03:00
{-| Forbid the use of `Debug` before it goes into production or fails in the CI.
config =
[ ( Critical, NoDebug.rule )
]
2018-11-05 16:44:22 +03:00
-}
2018-11-11 01:37:18 +03:00
rule : Rule
2019-06-15 22:14:40 +03:00
rule =
Rule.newSchema "NoDebug"
|> Rule.withSimpleImportVisitor importVisitor
|> Rule.withSimpleExpressionVisitor expressionVisitor
2019-06-24 01:32:27 +03:00
|> Rule.fromSchema
2017-01-07 23:17:01 +03:00
2018-11-11 01:37:18 +03:00
error : Node a -> Error
error node =
2019-06-26 14:47:00 +03:00
Rule.error "Forbidden use of Debug" (Node.range node)
importVisitor : Node Import -> List Error
importVisitor node =
let
moduleNameNode : Node (List String)
moduleNameNode =
node |> Node.value |> .moduleName
in
if Node.value moduleNameNode == [ "Debug" ] then
[ error moduleNameNode ]
else
[]
expressionVisitor : Node Expression -> List Error
expressionVisitor node =
case Node.value node of
FunctionOrValue moduleName fnName ->
if moduleName == [ "Debug" ] then
[ error node ]
2018-11-05 16:44:22 +03:00
else
[]
2017-01-07 23:17:01 +03:00
_ ->
[]