Skip to content

Commit

Permalink
[Fix #11765] Fix an error for Style/MultilineMethodSignature
Browse files Browse the repository at this point in the history
Fixes #11765.

This PR fixes an error for `Style/MultilineMethodSignature`
when line break after `def` keyword.

It seems preferable that the following cases can be detected by a new different cop:

```ruby
def
method_name arg; end
```

This is probably not a common line break, the new cop will be able to handle `class`, `module`,
class method definitions, and others as well.

And this `Style/MultilineMethodSignature` cop is disabled by default,
but I have a feeling that the new cop can be enabled by default.
  • Loading branch information
koic authored and bbatsov committed Apr 6, 2023
1 parent f325579 commit 7bb7d6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11765](https://github.com/rubocop/rubocop/issues/11765): Fix an error for `Style/MultilineMethodSignature` when line break after `def` keyword. ([@koic][])
9 changes: 6 additions & 3 deletions lib/rubocop/cop/style/multiline_method_signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ def on_def(node)
return unless node.arguments?
return if opening_line(node) == closing_line(node)
return if correction_exceeds_max_line_length?(node)
return unless (begin_of_arguments = node.arguments.loc.begin)

add_offense(node) { |corrector| autocorrect(corrector, node) }
add_offense(node) do |corrector|
autocorrect(corrector, node, begin_of_arguments)
end
end
alias on_defs on_def

private

def autocorrect(corrector, node)
def autocorrect(corrector, node, begin_of_arguments)
arguments = node.arguments
joined_arguments = arguments.map(&:source).join(', ')
last_line_source_of_arguments = last_line_source_of_arguments(arguments)
Expand All @@ -47,7 +50,7 @@ def autocorrect(corrector, node)
end

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

def last_line_source_of_arguments(arguments)
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/style/multiline_method_signature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ def foo(arg)
RUBY
end

it 'registers an offense and corrects when closing paren is on the following line' \
'and line break after `def` keyword' do
expect_offense(<<~RUBY)
def
^^^ Avoid multi-line method signatures.
foo(bar
)
end
RUBY

expect_correction(<<~RUBY)
def
foo(bar)
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 All @@ -51,6 +68,13 @@ def foo
RUBY
end
end

it 'does not register an offense when line break after `def` keyword' do
expect_no_offenses(<<~RUBY)
def
method_name arg; end
RUBY
end
end

context 'when defining an class method' do
Expand Down

0 comments on commit 7bb7d6d

Please sign in to comment.