Bylaw.Credo.Check.PhoenixLiveView.RequireFunctionComponentAttrs (bylaw_credo v0.4.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

Requires caller-facing assigns in HEEx function components to have attr or slot declarations.

Declarations make a component's public inputs explicit, enable compile-time validation and useful warnings, and give callers documentation at the point where the component is defined. Without them, misspelled or missing assigns fail later in rendering and the component's contract stays implicit.

Examples

Avoid:

defp summary_card(assigns) do
  ~H"""
  <h2>{@title}</h2>
  """
end

Prefer:

attr :title, :string, required: true

defp summary_card(assigns) do
  ~H"""
  <h2>{@title}</h2>
  """
end

render/1 is excluded because a LiveView's page assigns belong to its lifecycle rather than a function-component contract. Assigns established internally with assign/2,3 are also excluded. assign_new/3 does not replace a declaration for a caller-facing input.

Notes

This check treats public and private arity-one functions with a terminal ~H return as function components. It uses static AST and HEEx token analysis and requires phoenix_live_view to be available.

Options

This check has no check-specific options. Configure it with an empty option list.

Usage

Add this check to Credo's checks: list in .credo.exs:

{Bylaw.Credo.Check.PhoenixLiveView.RequireFunctionComponentAttrs, []}

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.