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 #9355] Fix Style/SingleLineMethods autocorrection to endless method when the original code had parens #9361

Merged
merged 1 commit into from Jan 10, 2021
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
1 change: 1 addition & 0 deletions changelog/fix_fix_stylesinglelinemethods.md
@@ -0,0 +1 @@
* [#9355](https://github.com/rubocop-hq/rubocop/issues/9355): Fix `Style/SingleLineMethods` autocorrection to endless method when the original code had parens. ([@dvandersluis][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/single_line_methods.rb
Expand Up @@ -89,7 +89,8 @@ def correct_to_multiline(corrector, node)
end

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

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

it 'handles arguments properly' do
expect_correction(<<~RUBY.strip, source: 'def some_method(a, b, c) body end')
def some_method(a, b, c) = body
RUBY
end

it 'does not add parens if they are already present' do
expect_correction(<<~RUBY.strip, source: 'def some_method() body end')
def some_method() = body
RUBY
end

it 'does not correct to an endless method if the method body contains multiple statements' do
expect_correction(<<~RUBY.strip, source: 'def some_method; foo; bar end')
def some_method;#{trailing_whitespace}
Expand Down