elm-review/tests/MiscRules/NoBooleanCase.elm

44 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)
rule : Rule
rule =
Rule.newModuleRuleSchema "NoBooleanCase" ()
|> Rule.withSimpleExpressionVisitor expressionVisitor
|> Rule.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 ({ end } as range) { expression, 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
[]
_ ->
[]