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
Flags @assign reads in a function component that declares its
interface with attr/slot but declares no attr or slot of that name.
Phoenix only puts declared attrs (and their defaults) into a
component's assigns, and warns at call sites that pass undeclared
ones. An undeclared @key therefore raises KeyError on every
render that does not happen to receive it.
Before:
attr :title, :string, required: true
def card(assigns) do
~H"<h2>{@title}</h2><p>{@subtitle}</p>"
endAfter:
attr :title, :string, required: true
attr :subtitle, :string, default: nil
def card(assigns) do
~H"<h2>{@title}</h2><p>{@subtitle}</p>"
endThe check skips a component when:
- it declares no
attrorslotat all (its interface is undeclared by design); - it is a LiveView or LiveComponent
render/1, whose assigns come from the socket rather than from attrs; - its body assigns keys from a non-literal source, such as
assign(assigns, some_map),Map.merge/2, orassigns = normalize(assigns), because the set of keys is then unknown; - its module calls it directly as a function, which bypasses attr validation.
Keys assigned in the component body before rendering
(assign(assigns, :key, value), assign_new/3, update/3) count as
declared.
Confidence: high. Tier: syntactic (works without compiling).
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"].
:implicit_assigns
Assign keys never reported (Phoenix-provided or global by convention).
This parameter defaults to [:inner_block, :rest, :__changed__, :__given__].
: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.