mirror of
https://github.com/jfmengels/elm-review.git
synced 2024-09-11 16:16:09 +03:00
65 lines
1.3 KiB
Elm
65 lines
1.3 KiB
Elm
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)
|
|
import Review.Rule3 as Rule3
|
|
|
|
|
|
{-| 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 =
|
|
Rule3.newModuleRuleSchema_New "NoFullyAppliedPrefixOperator" ()
|
|
|> Rule3.withSimpleExpressionVisitor_New expressionVisitor
|
|
|> Rule3.fromModuleRuleSchema_New
|
|
|
|
|
|
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 ]
|
|
|
|
_ ->
|
|
[]
|