Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extracting a method without another declaration to anchor replacement #2429

Merged
merged 1 commit into from
Aug 19, 2024

Conversation

vinistock
Copy link
Member

Motivation

Closes #2393

Currently, if someone tries to extract a selection into a method and there are no other methods in the same file, we don't file any node to anchor the selection (we end up finding a program node).

We need to account for this possibility to avoid breaking.

Implementation

When we don't have another method to anchor the replacement, there's not much that we can use to find a spot. So I went with the line immediately above the selection. The extracted source is slightly different, because we normally extract the method to a position below the selected one, but in this case we want to extract it above it.

Automated Tests

Added a test that reproduces the issue.

@vinistock vinistock added bugfix This PR will fix an existing bug server This pull request should be included in the server gem's release notes labels Aug 12, 2024
@vinistock vinistock self-assigned this Aug 12, 2024
@vinistock vinistock requested a review from a team as a code owner August 12, 2024 20:09
@vinistock vinistock requested review from andyw8 and st0012 August 12, 2024 20:09
@vinistock vinistock force-pushed the vs-fix-extract-method-on-script branch from 3539d76 to 86130e7 Compare August 19, 2024 17:50
@vinistock vinistock enabled auto-merge (squash) August 19, 2024 17:50
@vinistock vinistock disabled auto-merge August 19, 2024 17:55
@vinistock vinistock merged commit 76f292e into main Aug 19, 2024
35 checks passed
@vinistock vinistock deleted the vs-fix-extract-method-on-script branch August 19, 2024 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This PR will fix an existing bug server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NoMethodError: undefined method end_keyword_loc for an instance of Prism::ProgramNode
3 participants