Skip to content

Linter Rule: Require strict locals on the first line with a blank line after

Rule: actionview-strict-locals-first-line

Description

Requires that the strict locals declaration:

  1. Appears on the first line of a Rails partial template
  2. Is followed by a blank line before any content

A partial is any template whose filename begins with an underscore (e.g. _card.html.erb).

Rationale

While Rails accepts strict locals declarations anywhere in a partial, placing them at the very top, followed by a blank line, makes the partial's expected locals immediately visible and visually separated from the template body. This mirrors conventions like # frozen_string_literal: true in Ruby files.

Enforcing this placement ensures that locals are the first thing you see when opening the file, that the partial's public API is clearly separated from its content, and consistent across all partials in the codebase.

Examples

✅ Good

app/views/users/_card.html.erb
erb
<%# locals: (user:) %>

<div class="user-card">
  <%= user.name %>
</div>

🚫 Bad

Strict locals not on the first line:

app/views/users/_card.html.erb
erb
<div class="user-card">
  <%# locals: (user:) %>
  <%= user.name %>
</div>

Strict locals after a leading blank line:

app/views/users/_card.html.erb
erb

<%# locals: (user:) %>

<div class="user-card">
  <%= user.name %>
</div>

Strict locals on line 1 but no blank line before content:

app/views/users/_card.html.erb
erb
<%# locals: (user:) %>
<div class="user-card">
  <%= user.name %>
</div>

References

Released under the MIT License.