Skip to content

Linter: Implement actionview-no-redundant-local-assigns rule - #2014

Merged
marcoroth merged 1 commit into
mainfrom
actionview-no-redundant-local-assigns
Aug 5, 2026
Merged

marcoroth merged 1 commit into
mainfrom
actionview-no-redundant-local-assigns

Conversation

@marcoroth

@marcoroth marcoroth commented Aug 5, 2026 •

Copy link
Copy Markdown
Owner

This pull request implements the actionview-no-redundant-local-assigns rule, which reports local_assigns lookups in a partial that already has a <%# locals: (...) %> declaration, where the declaration makes the lookup either redundant or dead.

local_assigns stays useful in a partial that has a strict locals declaration, so this is deliberately not a rule against local_assigns itself. What it targets are the lookups the declaration already answers: a required local is a local variable, so reading it back out of the hash is a longer way to write the name, and it is always present, so asking key? about it is a condition that can only take one branch. A name the declaration does not mention can never arrive at all, since Rails raises ActionView::StrictLocalsError for callers that pass an undeclared local, which makes the lookup dead code.

<%# locals: (user:) %>

<%= local_assigns[:user].name %>
Strict local `user` is already a local variable in this partial, so `local_assigns[:user]` reads
back a value that is already in scope. Use `user` instead.
<%# locals: (user:) %>

<% if local_assigns.key?(:user) %>
  <%= user.name %>
<% end %>
Strict local `user` is required, so `local_assigns.key?(:user)` is always `true`. Remove the
condition, or give `user` a default value to make it optional.
<%# locals: (user:) %>

<%= user.name %>
<%= local_assigns.fetch(:size, "large") %>
`size` is not declared in the `locals:` declaration, so Rails raises if a caller passes it and
`local_assigns.fetch(:size, ...)` can never find it. Declare `size:` in the declaration, or
remove the lookup.

Resolves #2011

@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 5, 2026
@github-actions

github-actions Bot commented Aug 5, 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 57a4a5d


✅ Preview deployment has been cleaned up.

@pkg-pr-new

pkg-pr-new Bot commented Aug 5, 2026

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

commit: 57a4a5d

@marcoroth
marcoroth marked this pull request as ready for review August 5, 2026 11:54
@marcoroth
marcoroth merged commit 1fa6393 into main Aug 5, 2026
22 checks passed
@marcoroth
marcoroth deleted the actionview-no-redundant-local-assigns branch August 5, 2026 11:54

This branch was successfully deployed

1 active deployment
herb-tools (Preview) — 57a4a5d2 Deployed Aug 5, 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 Rule: Report redundant local_assigns reads

1 participant