-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MultilineMethodParameterLineBreaks cop
Same as MultilineMethodArgumentLineBreaks but for method definitions, this completes the set of cops that already existed for method calls, array definitions, and Hash definitions.
- Loading branch information
Showing
7 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/new_layout_multiline_method_parameter_line_breaks_cop.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#10691](https://github.com/rubocop/rubocop/pull/10691): Add new `Layout/MultilineMethodParameterLineBreaks` cop. ([@Korri][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Layout | ||
# Ensures that each parameter in a multi-line method definition | ||
# starts on a separate line. | ||
# | ||
# NOTE: This cop does not move the first argument, if you want that to | ||
# be on a separate line, see `Layout/FirstMethodParameterLineBreak`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# def foo(a, b, | ||
# c | ||
# ) | ||
# end | ||
# | ||
# # good | ||
# def foo( | ||
# a, | ||
# b, | ||
# c | ||
# ) | ||
# end | ||
# | ||
# # good | ||
# def foo(a, b, c) | ||
# end | ||
class MultilineMethodParameterLineBreaks < Base | ||
include MultilineElementLineBreaks | ||
extend AutoCorrector | ||
|
||
MSG = 'Each parameter in a multi-line method definition must start on a separate line.' | ||
|
||
def on_def(node) | ||
return if node.arguments.empty? | ||
|
||
check_line_breaks(node, node.arguments) | ||
end | ||
end | ||
end | ||
end | ||
end |
162 changes: 162 additions & 0 deletions
162
spec/rubocop/cop/layout/multiline_method_parameter_line_breaks_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Layout::MultilineMethodParameterLineBreaks, :config do | ||
context 'when one parameter on same line' do | ||
it 'does not add any offenses' do | ||
expect_no_offenses(<<~RUBY) | ||
def taz(abc) | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when there are no parameters' do | ||
it 'does not add any offenses' do | ||
expect_no_offenses(<<~RUBY) | ||
def taz | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when two parameters are on next line' do | ||
it 'does not add any offenses' do | ||
expect_no_offenses(<<~RUBY) | ||
def taz( | ||
foo, bar | ||
) | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when many parameter are on multiple lines, two on same line' do | ||
it 'registers an offense and corrects' do | ||
expect_offense(<<~RUBY) | ||
def taz(abc, | ||
foo, bar, | ||
^^^ Each parameter in a multi-line method definition must start on a separate line. | ||
baz | ||
) | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
def taz(abc, | ||
foo,#{trailing_whitespace} | ||
bar, | ||
baz | ||
) | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when many parameters are on multiple lines, three on same line' do | ||
it 'registers an offense and corrects' do | ||
expect_offense(<<~RUBY) | ||
def taz(abc, | ||
foo, bar, barz, | ||
^^^^ Each parameter in a multi-line method definition must start on a separate line. | ||
^^^ Each parameter in a multi-line method definition must start on a separate line. | ||
baz | ||
) | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
def taz(abc, | ||
foo,#{trailing_whitespace} | ||
bar,#{trailing_whitespace} | ||
barz, | ||
baz | ||
) | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when many parameters including hash are on multiple lines, three on same line' do | ||
it 'registers an offense and corrects' do | ||
expect_offense(<<~RUBY) | ||
def taz(abc, | ||
foo, bar, z: "barz", | ||
^^^^^^^^^ Each parameter in a multi-line method definition must start on a separate line. | ||
^^^ Each parameter in a multi-line method definition must start on a separate line. | ||
x: | ||
) | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
def taz(abc, | ||
foo,#{trailing_whitespace} | ||
bar,#{trailing_whitespace} | ||
z: "barz", | ||
x: | ||
) | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when parameter\'s default value starts on same line but ends on different line' do | ||
it 'registers an offense and corrects' do | ||
expect_offense(<<~RUBY) | ||
def taz(abc, foo = { | ||
^^^^^^^ Each parameter in a multi-line method definition must start on a separate line. | ||
foo: "edf", | ||
}) | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
def taz(abc,#{trailing_whitespace} | ||
foo = { | ||
foo: "edf", | ||
}) | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when second parameter starts on same line as end of first' do | ||
it 'registers an offense and corrects' do | ||
expect_offense(<<~RUBY) | ||
def taz(abc = { | ||
foo: "edf", | ||
}, bar:) | ||
^^^^ Each parameter in a multi-line method definition must start on a separate line. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
def taz(abc = { | ||
foo: "edf", | ||
},#{trailing_whitespace} | ||
bar:) | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when there are multiple parameters on the first line' do | ||
it 'registers an offense and corrects starting from the 2nd argument' do | ||
expect_offense(<<~RUBY) | ||
def do_something(foo, bar, baz, | ||
^^^ Each parameter in a multi-line method definition must start on a separate line. | ||
^^^ Each parameter in a multi-line method definition must start on a separate line. | ||
quux) | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
def do_something(foo,#{trailing_whitespace} | ||
bar,#{trailing_whitespace} | ||
baz, | ||
quux) | ||
end | ||
RUBY | ||
end | ||
end | ||
end |