Rbtz.CredoChecks.Warning.EnumEachInHeex (rbtz_credo_checks v0.1.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 normal and works with any version of Elixir.

Explanation

Forbids <% Enum.each %> (and other side-effecting EEx constructs) in HEEx templates.

<% Enum.each %> runs the enumeration purely for side effects and returns :ok — its body cannot emit markup the way <%= for %> or the :for= element attribute can. In a HEEx template this almost always means the markup the developer intended to render simply doesn't appear.

Use the :for= attribute on the rendered element when iterating one element at a time, or the <%= for %> block expression when wrapping multiple elements per item.

The check inspects every ~H sigil and every .heex template referenced via embed_templates. Detection is line-by-line: a <% Enum.each that wraps onto the next line is not flagged (the formatter keeps the opener on one line in practice).

Bad

<% Enum.each(@items, fn item -> %>
  <li>{item.name}</li>
<% end) %>

Good

<li :for={item <- @items}>{item.name}</li>

<%= for item <- @items do %>
  <li>{item.name}</li>
  <span>{item.subtitle}</span>
<% end %>

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.