2020-06-03 19:23:19 +03:00
|
|
|
module NoRecursiveUpdate exposing (rule)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
|
|
|
|
@docs rule
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
import Elm.Syntax.Declaration as Declaration exposing (Declaration)
|
|
|
|
import Elm.Syntax.Expression as Expression exposing (Expression)
|
|
|
|
import Elm.Syntax.Node as Node exposing (Node)
|
|
|
|
import Review.Rule as Rule exposing (Error, Rule)
|
|
|
|
|
|
|
|
|
|
|
|
{-| Reports when the `update` function calls itself.
|
|
|
|
|
|
|
|
This is often done in order to have one message (A) trigger (all or some of) the same
|
|
|
|
model updates and commands as another message (B).
|
|
|
|
|
|
|
|
update msg model =
|
|
|
|
case msg of
|
|
|
|
Foo ->
|
|
|
|
{ model | foo = True }
|
|
|
|
|
|
|
|
Bar ->
|
|
|
|
update Foo { model | bar = True }
|
|
|
|
|
|
|
|
This is advised against, because if the way that message B is handled changes,
|
|
|
|
that will implicitly change how message A is handled in ways that may not have
|
|
|
|
been foreseen.
|
|
|
|
|
|
|
|
A better solution is to move the common handling into a different function and
|
|
|
|
have it called in the handling of both messages.
|
|
|
|
|
|
|
|
update msg model =
|
|
|
|
case msg of
|
|
|
|
Foo ->
|
|
|
|
commonOperationOnModel model
|
|
|
|
|
|
|
|
Bar ->
|
|
|
|
commonOperationOnModel { model | bar = True }
|
|
|
|
|
|
|
|
commonOperationOnModel model =
|
|
|
|
{ model | foo = True }
|
|
|
|
|
|
|
|
Calls to other modules' `update` function are allowed.
|
|
|
|
|
|
|
|
To add the rule to your configuration:
|
|
|
|
|
|
|
|
config =
|
|
|
|
[ NoRecursiveUpdate.rule
|
|
|
|
]
|
|
|
|
|
2020-09-22 20:40:30 +03:00
|
|
|
|
|
|
|
## Try it out
|
|
|
|
|
|
|
|
You can try this rule out by running the following command:
|
|
|
|
|
|
|
|
```bash
|
|
|
|
elm-review --template jfmengels/elm-review-the-elm-architecture/example --rules NoRecursiveUpdate
|
|
|
|
```
|
|
|
|
|
2020-06-03 19:23:19 +03:00
|
|
|
-}
|
|
|
|
rule : Rule
|
|
|
|
rule =
|
2020-06-28 08:49:27 +03:00
|
|
|
Rule.newModuleRuleSchema "NoRecursiveUpdate" { isInUpdateFunction = False }
|
|
|
|
|> Rule.withDeclarationEnterVisitor declarationVisitor
|
|
|
|
|> Rule.withExpressionEnterVisitor expressionVisitor
|
|
|
|
|> Rule.fromModuleRuleSchema
|
2020-06-03 19:23:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
type alias Context =
|
|
|
|
{ isInUpdateFunction : Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-20 21:09:03 +03:00
|
|
|
declarationVisitor : Node Declaration -> Context -> ( List nothing, Context )
|
|
|
|
declarationVisitor node _ =
|
|
|
|
case Node.value node of
|
|
|
|
Declaration.FunctionDeclaration function ->
|
2020-06-03 19:23:19 +03:00
|
|
|
( []
|
|
|
|
, { isInUpdateFunction =
|
|
|
|
(function.declaration
|
|
|
|
|> Node.value
|
|
|
|
|> .name
|
|
|
|
|> Node.value
|
|
|
|
)
|
|
|
|
== "update"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
( [], { isInUpdateFunction = False } )
|
|
|
|
|
|
|
|
|
2020-06-20 21:09:03 +03:00
|
|
|
expressionVisitor : Node Expression -> Context -> ( List (Error {}), Context )
|
|
|
|
expressionVisitor node context =
|
2020-06-03 19:23:19 +03:00
|
|
|
if context.isInUpdateFunction then
|
2020-06-20 21:09:03 +03:00
|
|
|
case Node.value node of
|
|
|
|
Expression.FunctionOrValue [] "update" ->
|
2020-06-03 19:23:19 +03:00
|
|
|
( [ Rule.error
|
|
|
|
{ message = "`update` shouldn't call itself"
|
2022-04-17 09:59:11 +03:00
|
|
|
, details = [ "If you wish to have the same behavior for different messages, move that behavior into a new function and have it called in the handling of both messages." ]
|
2020-06-03 19:23:19 +03:00
|
|
|
}
|
|
|
|
(Node.range node)
|
|
|
|
]
|
|
|
|
, context
|
|
|
|
)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
( [], context )
|
|
|
|
|
|
|
|
else
|
|
|
|
( [], context )
|