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 template reads of assigns that mount/3 does not set on every
path before the first render. LiveView raises KeyError on the first
render that reaches such a read.
Before:
def mount(_params, _session, socket), do: {:ok, socket}
def handle_event("refresh", _params, socket),
do: {:noreply, assign(socket, :stats, Stats.load())}
def render(assigns), do: ~H"<p>{@stats.total}</p>"After:
def mount(_params, _session, socket), do: {:ok, assign(socket, :stats, nil)}
def render(assigns), do: ~H"<p :if={@stats}>{@stats.total}</p>"The check follows mount/3 in both the disconnected and the
connected context. A key counts as set before the first render when
mount/3 sets it on every path, when handle_params/3 or an
on_mount hook may set it, or when it is listed in assume_set. Only
reads that run on every render count: reads inside :if elements,
EEx if/case/for blocks, and slots are skipped. Keys set only
when connected are left to AssignSetOnlyWhenConnected (SC202).
When mount/3 passes the socket to a function soundcheck does not
model, the check makes no claim. When handle_params/3 does, or an
on_mount hook cannot be read, findings are unverifiable.
Confidence: medium. 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"].
:assume_set
Keys the team's conventions guarantee before the first render.
This parameter defaults to [:current_user, :current_scope, :flash, :live_action, :page_title].
:strict
Report unverifiable findings too.
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.