Rbtz.CredoChecks.Readability.LiveViewCallbackOrder
(rbtz_credo_checks v0.1.0)
Copy Markdown
View Source
Basics
This check is disabled by default.
Learn how to enable it via .credo.exs.
This check has a base priority of normal and works with any version of Elixir.
Explanation
Enforces a consistent top-to-bottom callback order in
Phoenix.LiveView modules:
mounthandle_paramshandle_eventhandle_infohandle_asyncrender
Reading a LiveView top-to-bottom in this order matches the lifecycle itself: how the view boots, how it reacts to URL changes, how it reacts to user events, how it reacts to messages, then the rendering. When callbacks are interleaved arbitrarily it takes longer to find the bit of behavior you're looking for and harder to spot missing handlers.
Non-callback def/defp (helpers) are ignored — they may appear
anywhere in the module. Only the relative order of the LiveView
callbacks listed above is checked.
The check only runs against modules that declare use Phoenix.LiveView
directly or use SomethingWeb, :live_view. Multiple clauses of the
same callback are fine; they share a bucket. The first out-of-order
callback is flagged.
Bad
def mount(_, _, socket), do: {:ok, socket}
def render(assigns), do: ~H""
def handle_event("x", _, socket), do: {:noreply, socket}Good
def mount(_, _, socket), do: {:ok, socket}
def handle_event("x", _, socket), do: {:noreply, socket}
def render(assigns), do: ~H""Check-Specific Parameters
There are no specific parameters for this check.
General Parameters
Like with all checks, general params can be applied.
Parameters can be configured via the .credo.exs config file.