Skip to content

Commit

Permalink
Merge pull request #9361 from dvandersluis/issue/9355
Browse files Browse the repository at this point in the history
[Fix #9355] Fix `Style/SingleLineMethods` autocorrection to endless method when the original code had parens
  • Loading branch information
koic committed Jan 10, 2021
2 parents 7f09c74 + f588088 commit 94ced7b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
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

0 comments on commit 94ced7b

Please sign in to comment.