Bylaw.Credo.Check.PhoenixLiveView.RequireFunctionComponentAttrs (bylaw_credo v0.3.1)

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.

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.