Skip to content

Linter Rule: Disallow inline case conditions ​

Rule: erb-no-inline-case-conditions

Description ​

Disallow placing case and its first when/in condition in the same ERB tag. This rule flags such patterns and guides users toward separate ERB tags.

Rationale ​

The parser handles this pattern by splitting the tag, so the case and its first condition each own the Ruby they introduce. The template then compiles the same as one written with separate tags, and herb format rewrites it into that shape for you.

What the shared tag costs is readability. The case expression and its first branch run together in one tag while every later branch gets its own, so the branches no longer line up. Separate tags also match the conventional ERB style used across the Ruby on Rails ecosystem.

Two parser errors cover the same shape from a different angle. Under strict parsing, which is the default, ERB_CASE_WITH_CONDITIONS_ERROR rejects the tag outright, so this rule only reports where strict mode is off. A case and its first in pattern on the same line (<% case x in y %>) is Ruby's one-line pattern match instead of a case/in, so ERB_CASE_INLINE_PATTERN_MATCH_ERROR reports it in both modes. That one is a real parse failure, so herb format leaves it for you to fix.

Examples ​

✅ Good ​

case/when in separate ERB tags:

erb
<% case variable %>
<% when "a" %>
  A
<% when "b" %>
  B
<% else %>
  Other
<% end %>

case/in (pattern matching) in separate ERB tags:

erb
<% case value %>
<% in 1 %>
  One
<% in 2 %>
  Two
<% else %>
  Other
<% end %>

🚫 Bad ​

Inline case/when in a single ERB tag:

erb
<% case variable when "a" %>
A `case` statement and its first `when` condition share an ERB tag. Use separate ERB tags for `case` and its conditions (e.g., `<% case x %>` followed by `<% when y %>`). (erb-no-inline-case-conditions)
A `case` statement with `when`/`in` conditions in a single ERB tag cannot be reliably parsed, compiled, and formatted. Use separate ERB tags for `case` and its conditions (e.g., `<% case x %>` followed by `<% when y %>`), or set `strict: false` to allow this. (`ERB_CASE_WITH_CONDITIONS_ERROR`) (parser-no-errors)
A <% when "b" %> B <% end %>

Inline case/in in a single ERB tag:

erb
<% case value in 1 %>
A `case` statement and its first `in` pattern on the same line read as Ruby's one-line pattern match, not as a `case`/`in`. Put the `in` pattern on its own line, or in its own ERB tag (e.g., `<% case x %>` followed by `<% in y %>`). (`ERB_CASE_INLINE_PATTERN_MATCH_ERROR`) (parser-no-errors)
One <% in 2 %> Two <% end %>

case/when on separate lines but still in the same ERB tag:

erb
<% case variable
A `case` statement and its first `when` condition share an ERB tag. Use separate ERB tags for `case` and its conditions (e.g., `<% case x %>` followed by `<% when y %>`). (erb-no-inline-case-conditions)
A `case` statement with `when`/`in` conditions in a single ERB tag cannot be reliably parsed, compiled, and formatted. Use separate ERB tags for `case` and its conditions (e.g., `<% case x %>` followed by `<% when y %>`), or set `strict: false` to allow this. (`ERB_CASE_WITH_CONDITIONS_ERROR`) (parser-no-errors)
when "a" %> A <% when "b" %> B <% end %>

References ​

-

Released under the MIT License.