Soundcheck.Check.Warning.UndefinedMountHook (soundcheck 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 high and works with any version of Elixir.

Explanation

Flags on_mount hooks whose module has no on_mount/4 clause that accepts the hook's argument. LiveView calls Module.on_mount(arg, params, session, socket) before every mount, so such a hook raises FunctionClauseError in every LiveView it guards. The compiler already warns when the module or its on_mount/4 is missing.

Before:

live_session :admin, on_mount: [{MyAppWeb.UserAuth, :require_admin}] do
  live "/admin", AdminLive
end

# MyAppWeb.UserAuth defines on_mount(:require_authenticated, ...) only

After:

def on_mount(:require_admin, _params, session, socket) do
  # ...
end

The check reads hooks from on_mount/1 calls in LiveViews and LiveComponents and from the :on_mount option of a router's live_session, and the hook module's compiled on_mount/4 clauses. A bare module hook passes :default. A clause counts as accepting unless its first pattern is a different atom; hooks whose argument is not a literal are skipped.

Confidence: high. Tier: semantic (run mix compile before mix credo).

Check-Specific Parameters

Use the following parameters to configure this check:

:excluded_paths

Paths to skip (atoms, binaries as path segments, or regexes).

This parameter defaults to [~r/\/test\//, "test"].

:strict

Unused.

This parameter defaults to false.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.