2020-04-05 10:57:34 +03:00
|
|
|
module NoFullyAppliedPrefixOperator exposing (rule)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
|
|
|
|
@docs rule
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
import Elm.Syntax.Expression as Expression exposing (Expression)
|
|
|
|
import Elm.Syntax.Node as Node exposing (Node)
|
|
|
|
import Elm.Syntax.Range exposing (Range)
|
|
|
|
import Review.Rule as Rule exposing (Error, Rule)
|
2020-06-20 16:13:37 +03:00
|
|
|
import Review.Rule3 as Rule3
|
2020-04-05 10:57:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| Reports when an operator is used as a prefix operator and all the operands are already given.
|
|
|
|
|
|
|
|
config =
|
|
|
|
[ NoFullyAppliedPrefixOperator.rule
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
## Fail
|
|
|
|
|
|
|
|
_ =
|
|
|
|
(+) 1 2
|
|
|
|
|
|
|
|
|
|
|
|
## Success
|
|
|
|
|
|
|
|
_ =
|
|
|
|
1 + 2
|
|
|
|
|
|
|
|
_ =
|
|
|
|
(+) 1
|
|
|
|
|
|
|
|
_ =
|
|
|
|
(+)
|
|
|
|
|
|
|
|
-}
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
2020-06-25 23:57:31 +03:00
|
|
|
Rule3.newModuleRuleSchema "NoFullyAppliedPrefixOperator" ()
|
|
|
|
|> Rule3.withSimpleExpressionVisitor expressionVisitor
|
|
|
|
|> Rule3.fromModuleRuleSchema
|
2020-04-05 10:57:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
error : Range -> Error {}
|
|
|
|
error range =
|
|
|
|
Rule.error
|
|
|
|
{ message = "Prefer using the infix form (`a + b`) over the prefix form (`(+) a b`) when possible"
|
|
|
|
, details = [ "The prefix form is generally harder to read over the infix form." ]
|
|
|
|
}
|
|
|
|
range
|
|
|
|
|
|
|
|
|
|
|
|
expressionVisitor : Node Expression -> List (Error {})
|
|
|
|
expressionVisitor node =
|
|
|
|
case Node.value node of
|
|
|
|
Expression.Application [ Node.Node range (Expression.PrefixOperator _), _, _ ] ->
|
|
|
|
[ error range ]
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
[]
|