elm-review/tests/NoUselessSubscriptions.elm

99 lines
2.9 KiB
Elm
Raw Normal View History

2020-06-03 19:23:19 +03:00
module NoUselessSubscriptions exposing (rule)
{-|
@docs rule
-}
import Elm.Syntax.Declaration as Declaration exposing (Declaration)
import Elm.Syntax.Expression as Expression
import Elm.Syntax.Node as Node exposing (Node(..))
import Review.Rule as Rule exposing (Error, Rule)
2020-06-20 16:13:37 +03:00
import Review.Rule3 as Rule3
2020-06-03 19:23:19 +03:00
{-| Reports `subscriptions` functions that never return a subscription.
In my opinion, this is often a sign of premature architectural work, where you
set up an `init`, `view`, `update` and `subscriptions` functions. I think
it is better to define them as they are needed, to avoid adding upfront complexity
that turn out to be unnecessary later.
config =
[ NoUselessSubscriptions.rule
]
## Fail
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
## Success
main =
Browser.element
{ init = init
, update = update
, subscriptions = \_ -> Sub.none
, view = view
}
-}
rule : Rule
rule =
2020-06-25 23:57:31 +03:00
Rule3.newModuleRuleSchema "NoUselessSubscriptions" ()
|> Rule3.withSimpleDeclarationVisitor declarationVisitor
|> Rule3.fromModuleRuleSchema
2020-06-03 19:23:19 +03:00
declarationVisitor : Node Declaration -> List (Error {})
declarationVisitor declaration =
case Node.value declaration of
Declaration.FunctionDeclaration function ->
if
(function.declaration
|> Node.value
|> .name
|> Node.value
)
== "subscriptions"
then
case Node.value function.declaration |> .expression |> Node.value of
Expression.FunctionOrValue [ "Sub" ] "none" ->
[ error function
]
Expression.Application [ Node _ (Expression.FunctionOrValue [] "always"), Node _ (Expression.FunctionOrValue [ "Sub" ] "none") ] ->
[ error function
]
Expression.Application [ Node _ (Expression.FunctionOrValue [ "Basics" ] "always"), Node _ (Expression.FunctionOrValue [ "Sub" ] "none") ] ->
[ error function
]
Expression.Application [ Node _ (Expression.FunctionOrValue [ "Sub" ] "batch"), Node _ (Expression.ListExpr []) ] ->
[ error function
]
_ ->
[]
else
[]
_ ->
[]
error : Expression.Function -> Error {}
error function =
Rule.error
{ message = "The `subscription` function never returns any subscriptions"
, details = [ "The `subscription` function never returns any subscriptions. You might as well remove it." ]
}
(Node.value function.declaration |> .expression |> Node.range)