Rbtz.CredoChecks.Readability.RedundantClassAttrWrapping (rbtz_credo_checks v0.2.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

Flags HEEx class={...} attributes whose wrapping is unnecessary. Three shapes are reported, each with a simpler equivalent form:

  1. class={"foo bar"} — a static string inside interp braces. Use the literal attribute form: class="foo bar".

  2. class={["foo bar"]} — a single static string inside a list. Drop the list and the interp: class="foo bar".

  3. class={[@cls]} / class={[some_fn()]} — a single expression inside a list. Drop the list wrapper: class={@cls} / class={some_fn()}.

Deliberately not flagged:

  • class={"foo #{@x} bar"} — the interp braces are required once the string contains a #{...} expression.

  • class={["foo", "bar"]} — multi-element lists. The companion Rbtz.CredoChecks.Readability.ClassAttrFormatting rule actively recommends splitting long class values into list elements, so collapsing these would contradict that neighbouring rule.

  • class={[]} / class={""} / class={nil} — empty/nil shapes. A different concern (dead attribute) and often intentional.

Note on case 3

HEEx's class={...} special-cases lists: nested lists flatten and nil/false entries are filtered. For most expressions (@cls, if(@x, do: "foo"), function calls returning strings or lists), class={[expr]} and class={expr} render identically. If the codebase relies on the subtle difference between class={[nil]} (empty class attribute) and class={nil} (attribute omitted), refactor the expression to be explicit rather than relying on the list wrapper.

Bad

<a class={"px-2 text-white"}>x</a>
<a class={["px-2 text-white"]}>x</a>
<a class={[@extra]}>x</a>

Good

<a class="px-2 text-white">x</a>
<a class={@extra}>x</a>
<a class={["px-2 text-white", @extra]}>x</a>

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.