Skip to content

Linter: Add support for as: in actionview-prefer-collection-render - #1997

Merged
marcoroth merged 1 commit into
mainfrom
prefer-collection-render-as
Aug 4, 2026
Merged

marcoroth merged 1 commit into
mainfrom
prefer-collection-render-as

Conversation

@marcoroth

Copy link
Copy Markdown
Owner

This pull request updates the actionview-prefer-collection-render linter rule so the replacement it reports keeps working when the loop passes the element under a name that isn't the partial name.

The rule reports the exact tag to paste over the loop, and it built that tag from the receiver and the partial alone. Collection rendering names the local after the partial, so any local named something else was silently dropped from the suggestion:

<% @gems.each do |topic_gem| %>
  <%= render partial: "gem_card", locals: { topic_gem: topic_gem } %>
<% end %>
Prefer `<%= render partial: "gem_card", collection: @gems %>` over rendering a partial once per iteration.

Following that advice hands _gem_card.html.erb a gem_card local, while the partial still reads topic_gem, so the page raises undefined local variable or method 'topic_gem'. The suggestion now compares the local's key against the name collection rendering would use and appends as: when the two differ:

- Prefer `<%= render partial: "gem_card", collection: @gems %>` over rendering a partial once per iteration.
+ Prefer `<%= render partial: "gem_card", collection: @gems, as: :topic_gem %>` over rendering a partial once per iteration.

That implicit name is the last path segment of the partial, so a nested partial whose local already matches keeps the shorter form and doesn't grow a redundant as::

<% @gems.each do |gem_card| %>
  <%= render partial: "gems/gem_card", locals: { gem_card: gem_card } %>
<% end %>

Both spellings of the local are covered, the explicit locals: hash and the shorthand render "gem_card", topic_gem: topic_gem. The object shorthand (render user inside the loop) is unaffected, since render @users carries no local name to rename.

When the parser reports a local without a name, the rule now stays silent instead of reporting a replacement it can't verify.

Resolves #1981

@github-actions github-actions Bot added documentation Improvements or additions to documentation linter @herb-tools/linter for HTML+ERB templates typescript TypeScript source across the javascript/ packages linter-rule Individual linter rules and their documentation labels Aug 4, 2026
@marcoroth
marcoroth marked this pull request as ready for review August 4, 2026 18:52
@github-actions

github-actions Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

🌿 Interactive Playground and Documentation Preview

A preview deployment has been built for this pull request. Try out the changes live in the interactive playground:


🌱 Grown from commit 13213c5


✅ Preview deployment has been cleaned up.

@pkg-pr-new

pkg-pr-new Bot commented Aug 4, 2026

Copy link
Copy Markdown
npx https://pkg.pr.new/@herb-tools/formatter@1997
npx https://pkg.pr.new/@herb-tools/language-server@1997
npx https://pkg.pr.new/@herb-tools/linter@1997

commit: 13213c5

@marcoroth
marcoroth merged commit 32c41b0 into main Aug 4, 2026
22 checks passed
@marcoroth
marcoroth deleted the prefer-collection-render-as branch August 4, 2026 18:57

This branch was successfully deployed

1 active deployment
herb-tools (Preview) — 13213c5a Deployed Aug 4, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation linter @herb-tools/linter for HTML+ERB templates linter-rule Individual linter rules and their documentation typescript TypeScript source across the javascript/ packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linter: actionview-prefer-collection-render needs to account for renamed locals

1 participant