Skip to content

Commit

Permalink
[Fix #10311] Expand send nodes correctly
Browse files Browse the repository at this point in the history
When Layout/RedundantLineBreak inspects a send node, it starts by
expanding the node to inspect. It does this by traversing the AST
upwards as long as the parent is a send node of a block node.

The problem was that if the inspected node is a single node inside
a do..end block, we shouldn't move to the parent block and inspect
it for redundant line breaks. It's only send nodes before the
do..end that we want to expand in order to inspect "the whole
expression". So we should check what kind of block/send
relationship it is before moving upwards in the AST.
  • Loading branch information
jonas054 authored and bbatsov committed Mar 27, 2022
1 parent c8dcda8 commit f94cd24
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/fix_RedundantLineBreak_inside_block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10311](https://github.com/rubocop/rubocop/issues/10311): Fix false negative inside `do`..`end` for `Layout/RedundantLineBreak`. ([@jonas054][])
7 changes: 3 additions & 4 deletions lib/rubocop/cop/layout/redundant_line_break.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ def suitable_as_single_line?(node)
end

def convertible_block?(node)
return false unless node.parent&.block_type?

send_node = node.parent&.send_node
send_node.parenthesized? || !send_node.arguments?
parent = node.parent
parent&.block_type? && node == parent.send_node &&
(node.parenthesized? || !node.arguments?)
end

def comment_within?(node)
Expand Down
21 changes: 19 additions & 2 deletions spec/rubocop/cop/layout/redundant_line_break_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def resolve_inheritance_from_gems(hash)
^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
"can't inherit configuration from the rubocop gem"
end
#{' '}
hash['inherit_from'] = Array(hash['inherit_from'])
Array(config_path).reverse_each do |path|
# Put gem configuration first so local configuration overrides it.
Expand All @@ -148,7 +148,7 @@ def resolve_inheritance_from_gems(hash)
if gem_name == 'rubocop'
raise ArgumentError, "can't inherit configuration from the rubocop gem"
end
#{' '}
hash['inherit_from'] = Array(hash['inherit_from'])
Array(config_path).reverse_each do |path|
# Put gem configuration first so local configuration overrides it.
Expand All @@ -173,6 +173,23 @@ def resolve_inheritance_from_gems(hash)
RUBY
end

it 'registers an offense for a method call on multiple lines inside a block' do
expect_offense(<<~RUBY)
some_array.map do |something|
my_method(
^^^^^^^^^^ Redundant line break detected.
something,
)
end
RUBY

expect_correction(<<~RUBY)
some_array.map do |something|
my_method( something, )
end
RUBY
end

it 'accepts a method call on multiple lines if there are comments on them' do
expect_no_offenses(<<~RUBY)
my_method(1,
Expand Down

0 comments on commit f94cd24

Please sign in to comment.