Repository navigation
Linter: Implement erb-prefer-do-end-blocks rule - #1973
Merged
Merged
Conversation
marcoroth
marked this pull request as ready for review
August 3, 2026 10:57
🌿 Interactive Playground and Documentation PreviewA preview deployment has been built for this pull request. Try out the changes live in the interactive playground: 🌱 Grown from commit ✅ Preview deployment has been cleaned up. |
commit: |
joaoGabriel55
pushed a commit
to joaoGabriel55/herb
that referenced
this pull request
Aug 3, 2026
This pull request implements `erb-prefer-do-end-blocks`, a new rule that
flags a block opened with `{` in one ERB tag and closed with `<% } %>`
in another, and points at the `do ... end` form instead.
```erb
<% @users.each { |user| %>
<p><%= user.name %></p>
<% } %>
```
```
Avoid using `{ ... }` for a block that spans multiple ERB tags. Use `do ... end` instead.
```
A block that spans ERB tags is a multi-line block, and `do ... end` is
the typical convention for those in Ruby.
In a template the argument is stronger than in plain Ruby: every other
construct that wraps markup, like `if`, `unless`, `case`, or `each` with
`do`, closes with `<% end %>`, so a lone `<% } %>` is the one closing
tag that has to be read differently from the rest of the file. The
braces are also easy to lose, since `<% } %>` is a single character of
signal surrounded by ERB punctuation and looks the same whether it
closes a block, a hash, or an interpolation.
A brace block written entirely inside one ERB tag is not reported, so
`<%= @users.map { |user| user.name }.join(", ") %>` stays as it is.
#### Autofix
The rule is autocorrectable. The fix replaces `{` with `do` in the
opening tag and `}` with `end` in the closing tag, keeping trim markers
intact and inserting the missing space for `{|user|`:
```erb
<%= form_with(model: @user) { |form| %>
<%= form.submit %>
<% } %>
```
```erb
<%= form_with(model: @user) do |form| %>
<%= form.submit %>
<% end %>
```
Swapping the delimiters is not unconditionally safe, because `{` binds
to the closest method call while `do` binds to the outermost one. The
fix is marked safe only when both are the same call, which it checks on
the Prism node: the outermost node has to be a `CallNode` that owns the
block, after unwrapping an assignment so that `<% names = @users.map {
|user| %>` still qualifies.
```erb
<% puts @users.map { |user| %>
<p><%= user.name %></p>
<% } %>
```
Here the block belongs to `map`, but a `do` would hand it to `puts`, so
the offense is still reported while no autofix is offered.
joaoGabriel55
pushed a commit
to joaoGabriel55/herb
that referenced
this pull request
Aug 3, 2026
This pull request implements `erb-prefer-do-end-blocks`, a new rule that
flags a block opened with `{` in one ERB tag and closed with `<% } %>`
in another, and points at the `do ... end` form instead.
```erb
<% @users.each { |user| %>
<p><%= user.name %></p>
<% } %>
```
```
Avoid using `{ ... }` for a block that spans multiple ERB tags. Use `do ... end` instead.
```
A block that spans ERB tags is a multi-line block, and `do ... end` is
the typical convention for those in Ruby.
In a template the argument is stronger than in plain Ruby: every other
construct that wraps markup, like `if`, `unless`, `case`, or `each` with
`do`, closes with `<% end %>`, so a lone `<% } %>` is the one closing
tag that has to be read differently from the rest of the file. The
braces are also easy to lose, since `<% } %>` is a single character of
signal surrounded by ERB punctuation and looks the same whether it
closes a block, a hash, or an interpolation.
A brace block written entirely inside one ERB tag is not reported, so
`<%= @users.map { |user| user.name }.join(", ") %>` stays as it is.
#### Autofix
The rule is autocorrectable. The fix replaces `{` with `do` in the
opening tag and `}` with `end` in the closing tag, keeping trim markers
intact and inserting the missing space for `{|user|`:
```erb
<%= form_with(model: @user) { |form| %>
<%= form.submit %>
<% } %>
```
```erb
<%= form_with(model: @user) do |form| %>
<%= form.submit %>
<% end %>
```
Swapping the delimiters is not unconditionally safe, because `{` binds
to the closest method call while `do` binds to the outermost one. The
fix is marked safe only when both are the same call, which it checks on
the Prism node: the outermost node has to be a `CallNode` that owns the
block, after unwrapping an assignment so that `<% names = @users.map {
|user| %>` still qualifies.
```erb
<% puts @users.map { |user| %>
<p><%= user.name %></p>
<% } %>
```
Here the block belongs to `map`, but a `do` would hand it to `puts`, so
the offense is still reported while no autofix is offered.
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request implements
erb-prefer-do-end-blocks, a new rule that flags a block opened with{in one ERB tag and closed with<% } %>in another, and points at thedo ... endform instead.A block that spans ERB tags is a multi-line block, and
do ... endis the typical convention for those in Ruby.In a template the argument is stronger than in plain Ruby: every other construct that wraps markup, like
if,unless,case, oreachwithdo, closes with<% end %>, so a lone<% } %>is the one closing tag that has to be read differently from the rest of the file. The braces are also easy to lose, since<% } %>is a single character of signal surrounded by ERB punctuation and looks the same whether it closes a block, a hash, or an interpolation.A brace block written entirely inside one ERB tag is not reported, so
<%= @users.map { |user| user.name }.join(", ") %>stays as it is.Autofix
The rule is autocorrectable. The fix replaces
{withdoin the opening tag and}withendin the closing tag, keeping trim markers intact and inserting the missing space for{|user|:Swapping the delimiters is not unconditionally safe, because
{binds to the closest method call whiledobinds to the outermost one. The fix is marked safe only when both are the same call, which it checks on the Prism node: the outermost node has to be aCallNodethat owns the block, after unwrapping an assignment so that<% names = @users.map { |user| %>still qualifies.Here the block belongs to
map, but adowould hand it toputs, so the offense is still reported while no autofix is offered.