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 error for Style/MultilineMethodSignature #9644

Merged
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 @@
* [#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