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
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2019-06-16 22:12:37 +03:00
|
|
|
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.
|
|
|
|
|
2019-06-26 01:34:57 +03:00
|
|
|
config =
|
|
|
|
[ ( Critical, NoExtraBooleanComparison.rule )
|
2019-06-16 16:31:40 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
-}
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
2019-06-24 23:35:30 +03:00
|
|
|
Rule.newSchema "NoExtraBooleanComparison"
|
2019-06-16 22:12:37 +03:00
|
|
|
|> 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)
|
|
|
|
|
|
|
|
|
2019-06-16 22:12:37 +03:00
|
|
|
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
|
2019-06-16 22:12:37 +03:00
|
|
|
List.filterMap isTrueOrFalse [ left, right ]
|
2019-06-16 16:31:40 +03:00
|
|
|
|
|
|
|
else
|
2019-06-16 22:12:37 +03:00
|
|
|
[]
|
2019-06-16 16:31:40 +03:00
|
|
|
|
|
|
|
_ ->
|
2019-06-16 22:12:37 +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
|