Jump. CredoChecks. UnusedLiveViewAssign
(Jump.CredoChecks v0.4.0)
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
Checks for literal LiveView assigns that are written by a module but never read in that module's Elixir or HEEx source.
This check is intentionally conservative. It is a source-level Credo check, not compiler-grade dataflow analysis, so it only tracks assign usage patterns that are obvious from the current module's Elixir source and related HEEx templates.
Conventions and limitations:
- Helper functions should read the assigns map through a variable named
assignsor throughsocket.assigns. If you bind assigns to another name (like%{assigns: opts} = socket, followed by usingopts.my_attr), the reads will not be recognized as being related to LiveView assigns. - Only literal writes via
assign,assign_new,assign_async,allow_upload,stream,stream_async, andupdateare checked. - Writes to dynamic assign keys are not tracked. Examples include
assign(socket, field, value),socket.assigns[field], andMap.get(assigns, field). Phoenix.Component.assigns_to_attributes/2is not treated as a read. Prefer explicitattrdeclarations andattr :rest, :globalfor forwarded attributes.- Some assigns are intended to be consumed by framework or layout code
outside the current LiveView module. Use
ignored_assignsto allowlist those keys.
Check-Specific Parameters
Use the following parameters to configure this check:
:ignored_assigns
Assign names that should be ignored because they are consumed outside the current LiveView module, such as by layouts, hooks, parent components, child components, or framework behavior.
:page_title is always included by default.
Example:
{Jump.CredoChecks.UnusedLiveViewAssign,
ignored_assigns: [:active_path]}This parameter defaults to [:page_title].
General Parameters
Like with all checks, general params can be applied.
Parameters can be configured via the .credo.exs config file.