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 an incorrect auto-correct for Style/SingleLineMethods #9645

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#9645](https://github.com/rubocop/rubocop/pull/9645): Fix an incorrect auto-correct for `Style/SingleLineMethods` when using single line class method definition. ([@koic][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/single_line_methods.rb
Expand Up @@ -91,8 +91,9 @@ def correct_to_multiline(corrector, node)
end

def correct_to_endless(corrector, node)
self_receiver = node.self_receiver? ? 'self.' : ''
arguments = node.arguments.any? ? node.arguments.source : '()'
replacement = "def #{node.method_name}#{arguments} = #{node.body.source}"
replacement = "def #{self_receiver}#{node.method_name}#{arguments} = #{node.body.source}"
corrector.replace(node, replacement)
end

Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/single_line_methods_spec.rb
Expand Up @@ -168,6 +168,12 @@ def some_method() = body
RUBY
end

it 'corrects to an endless class method definition' do
expect_correction(<<~RUBY.strip, source: 'def self.some_method; body end')
def self.some_method() = body
RUBY
end

it 'retains comments' do
source = 'def some_method; body end # comment'
expect_correction(<<~RUBY.strip, source: source)
Expand Down