Skip to content

Language Server: Rename a strict local across its call sites #2043

Description

@marcoroth

Renaming a strict local should be a rename, not a find-and-replace. Putting the cursor on user and pressing F2 in a partial should rename the declaration, every reference in the template, and the user: key at every render call site that passes it.

app/views/users/_card.html.erb

<%# locals: (user:) %>

<h1><%= user.name %></h1>
<p><%= user.bio %></p>

app/views/users/show.html.erb

<%= render partial: "users/card", locals: { user: @user } %>

Renaming user to author has to touch both files:

<%# locals: (author:) %>

<h1><%= author.name %></h1>
<p><%= author.bio %></p>
<%= render partial: "users/card", locals: { author: @user } %>

The in-file half alone is worse than not offering the rename at all. A required local that gets renamed in the declaration and nowhere else raises ActionView::StrictLocalsError on the next render, the same failure the actionview-no-strict-locals-error rule (#639) should be reporting.

What already works

The parser already provides both ends of the edit.

Declarations come from ERBStrictLocalsNode.locals, a list of RubyParameterNode with a name token, so the declaration side needs no new parsing.

Call sites come from render_nodes: true, which turns a render call into an ERBRenderNode with keywords.partial and keywords.locals, each a RubyRenderLocalNode with its own name token:

<%= render partial: "users/card", locals: { user: @user, size: "sm" } %>

partial: "users/card"
local:   user  cols 44–49
local:   size  cols 57–62

One thing to watch on both sides: the name token's location includes the trailing colon, so user is four characters but spans five columns. A rename that replaces the token's full range eats the : and produces locals: (author).

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or requestlanguage-server@herb-tools/language-server and Language Server Protocol support

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions