elm-review/tests/MiscRules/NoBooleanCase.elm
2020-06-25 22:57:31 +02:00

45 lines
1.3 KiB
Elm

module MiscRules.NoBooleanCase exposing (rule)
import Elm.Syntax.Expression as Expression exposing (Expression)
import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Pattern as Pattern
import Elm.Syntax.Range exposing (Range)
import Review.Rule as Rule exposing (Error, Rule)
import Review.Rule3 as Rule3
rule : Rule
rule =
Rule3.newModuleRuleSchema "NoBooleanCase" ()
|> Rule3.withSimpleExpressionVisitor expressionVisitor
|> Rule3.fromModuleRuleSchema
expressionVisitor : Node Expression -> List (Error {})
expressionVisitor (Node.Node range expression) =
case expression of
Expression.CaseExpression caseBlock ->
errorsForCaseBlock range caseBlock
_ ->
[]
errorsForCaseBlock : Range -> Expression.CaseBlock -> List (Error {})
errorsForCaseBlock range { cases } =
case cases of
[ ( Node.Node _ (Pattern.NamedPattern { moduleName, name } []), _ ), _ ] ->
if moduleName == [] && (name == "True" || name == "False") then
[ Rule.error
{ message = "Matching boolean values in a case .. of expression"
, details = [ "It's quite silly" ]
}
range
]
else
[]
_ ->
[]