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

91 lines
1.7 KiB
Elm
Raw Normal View History

2019-06-16 16:31:40 +03:00
module Lint.Rule.NoExtraBooleanComparison exposing (rule)
{-|
@docs rule
# Fail
if Debug.log "condition" condition then
a
else
b
if condition then
Debug.crash "Nooo!"
else
value
# Success
if condition then
a
else
b
-}
import Elm.Syntax.Expression as Expression exposing (Expression(..))
2019-06-16 16:31:40 +03:00
import Elm.Syntax.Node as Node exposing (Node)
import Lint.Error as Error exposing (Error)
2019-06-24 01:41:56 +03:00
import Lint.Rule as Rule exposing (Rule)
2019-06-16 16:31:40 +03:00
{-| Forbid the use of `Debug` before it goes into production.
config =
[ ( Critical, NoExtraBooleanComparison.rule )
2019-06-16 16:31:40 +03:00
]
-}
rule : Rule
rule =
Rule.newSchema "NoExtraBooleanComparison"
|> Rule.withSimpleExpressionVisitor expressionVisitor
2019-06-24 01:32:27 +03:00
|> Rule.fromSchema
2019-06-16 16:31:40 +03:00
error : String -> Node a -> Error
error comparedValue node =
Error.create
("Unnecessary comparison with `" ++ comparedValue ++ "`")
(Node.range node)
expressionVisitor : Node Expression -> List Error
expressionVisitor node =
case Node.value node of
Expression.OperatorApplication operator _ left right ->
2019-06-16 16:31:40 +03:00
if isEqualityOperator operator then
List.filterMap isTrueOrFalse [ left, right ]
2019-06-16 16:31:40 +03:00
else
[]
2019-06-16 16:31:40 +03:00
_ ->
[]
2019-06-16 16:31:40 +03:00
isEqualityOperator : String -> Bool
isEqualityOperator operator =
operator == "==" || operator == "/="
isTrueOrFalse : Node Expression -> Maybe Error
isTrueOrFalse node =
case Node.value node of
FunctionOrValue [] functionOrValue ->
if functionOrValue == "True" || functionOrValue == "False" then
Just <| error functionOrValue node
else
Nothing
_ ->
Nothing