Skip to content

Commit

Permalink
[Fix rubocop#6493] Layout/FirstMethodArgumentLineBreak for multi-li…
Browse files Browse the repository at this point in the history
…ne single argument
  • Loading branch information
marcotc committed Mar 21, 2019
1 parent ebbafd2 commit 2ddbc78
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@
### Changes

* [#5977](https://github.com/rubocop-hq/rubocop/issues/5977): Warn for Performance Cops. ([@koic][])
* [#6493](https://github.com/rubocop-hq/rubocop/issues/6493): Fix `Layout/FirstMethodArgumentLineBreak` for multi-line single argument. ([@marcotc][])
* [#6699](https://github.com/rubocop-hq/rubocop/issues/6699): Fix infinite loop for `Layout/IndentationWidth` and `Layout/IndentationConsistency` when bad modifier indentation before good method definition. ([@koic][])

## 0.66.0 (2019-03-18)

Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/ast/node.rb
Expand Up @@ -464,7 +464,10 @@ def chained?
end

def argument?
parent && parent.send_type? && parent.arguments.include?(self)
# TODO: Adds support safe navigator calls.
# TODO: Discuss with maintainers if this should be applied 'everywhere'
parent && (parent.send_type? || parent.csend_type?) &&
parent.arguments.include?(self)
end

def numeric_type?
Expand Down
14 changes: 13 additions & 1 deletion lib/rubocop/cop/layout/first_method_argument_line_break.rb
Expand Up @@ -46,7 +46,19 @@ def on_send(node)
alias on_csend on_send

def autocorrect(node)
EmptyLineCorrector.insert_before(node)
lambda do |corrector|
first_arg = if node.argument?
node
else
# In case of keyword arguments
node.parent
end

block_start_col = first_arg.parent.source_range.column

corrector.insert_before(first_arg.source_range,
"\n#{' ' * block_start_col}")
end
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/cop/mixin/first_element_line_break.rb
Expand Up @@ -21,6 +21,14 @@ def method_uses_parens?(node, limit)
end

def check_children_line_break(node, children, start = node)
return if children.empty?

return add_offense(children.first) if children.first.multiline?

check_multiple_children_line_break(children, start)
end

def check_multiple_children_line_break(children, start)
return if children.size < 2

line = start.first_line
Expand Down
27 changes: 27 additions & 0 deletions spec/rubocop/cop/layout/first_method_argument_line_break_spec.rb
Expand Up @@ -95,6 +95,33 @@
end
end

context 'single arg spanning multiple lines' do
it 'detects the offense' do
expect_offense(<<-RUBY.strip_indent)
foo([
^ Add a line break before the first argument of a multi-line method argument list.
])
RUBY
end

it 'autocorrects the offense' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
begin
foo([
])
end
RUBY

expect(new_source).to eq(<<-RUBY.strip_indent)
begin
foo(
[
])
end
RUBY
end
end

it 'ignores arguments listed on a single line' do
expect_no_offenses('foo(bar, baz, bing)')
end
Expand Down

0 comments on commit 2ddbc78

Please sign in to comment.