Skip to content

Commit

Permalink
Merge pull request #9644 from koic/fix_error_for_style_multiline_meth…
Browse files Browse the repository at this point in the history
…od_signature

Fix an error for `Style/MultilineMethodSignature`
  • Loading branch information
koic committed Mar 27, 2021
2 parents 3dba8f3 + ec1b2ea commit 43b26a6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
@@ -0,0 +1 @@
* [#9644](https://github.com/rubocop/rubocop/pull/9644): Fix an error and an incorrect auto-correct for `Style/MultilineMethodSignature` when line break after opening parenthesis. ([@koic][])
13 changes: 10 additions & 3 deletions lib/rubocop/cop/style/multiline_method_signature.rb
Expand Up @@ -40,21 +40,28 @@ def on_def(node)
def autocorrect(corrector, node)
arguments = node.arguments
joined_arguments = arguments.map(&:source).join(', ')
last_line_source_of_arguments = processed_source[arguments.last_line - 1].strip
last_line_source_of_arguments = last_line_source_of_arguments(arguments)

if last_line_source_of_arguments.start_with?(')')
joined_arguments = "#{joined_arguments}#{last_line_source_of_arguments}"

corrector.remove(range_by_whole_lines(arguments.loc.end, include_final_newline: true))
end

corrector.replace(arguments_range(node), joined_arguments)
corrector.remove(arguments_range(node))
corrector.insert_after(arguments.loc.begin, joined_arguments)
end

def last_line_source_of_arguments(arguments)
processed_source[arguments.last_line - 1].strip
end

def arguments_range(node)
range_between(
range = range_between(
node.first_argument.source_range.begin_pos, node.last_argument.source_range.end_pos
)

range_with_surrounding_space(range: range, side: :left)
end

def opening_line(node)
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/style/multiline_method_signature_spec.rb
Expand Up @@ -17,6 +17,25 @@ def foo(bar)
RUBY
end

it 'registers an offense and corrects when line break after opening parenthesis' do
expect_offense(<<~RUBY)
class Foo
def foo(
^^^^^^^^ Avoid multi-line method signatures.
arg
)
end
end
RUBY

expect_correction(<<~RUBY)
class Foo
def foo(arg)
end
end
RUBY
end

context 'when method signature is on a single line' do
it 'does not register an offense for parameterized method' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 43b26a6

Please sign in to comment.