Skip to content

Commit

Permalink
[Fix rubocop#10563] Fix Style/BlockDelimiters unexpectedly deletes …
Browse files Browse the repository at this point in the history
…block on moving comment if methods with block are chained
  • Loading branch information
nobuyo committed Apr 22, 2022
1 parent 54f4c82 commit 87501e5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10563](https://github.com/rubocop/rubocop/issues/10563): Fix `Style/BlockDelimiters` unexpectedly deletes block on moving comment if methods with block are chained. ([@nobuyo][])
5 changes: 5 additions & 0 deletions lib/rubocop/cop/style/block_delimiters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,16 @@ def move_comment_before_block(corrector, comment, block_node, closing_brace)
end

def end_of_chain(node)
return end_of_chain(node.block_node) if with_block?(node)
return node unless node.chained?

end_of_chain(node.parent)
end

def with_block?(node)
node.respond_to?(:block_node) && node.block_node
end

def get_blocks(node, &block)
case node.type
when :block
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/style/block_delimiters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,20 @@
RUBY
end

it 'registers an offense and keep chained block when there is a comment after the closing brace and block body is not empty' do
expect_offense(<<~RUBY)
baz.map { |x|
^ Avoid using `{...}` for multi-line blocks.
foo(x) }.map { |x| x.quux } # comment
RUBY

expect_correction(<<~RUBY)
# comment
baz.map do |x|
foo(x) end.map { |x| x.quux }
RUBY
end

it 'registers an offense when there is a comment after the closing brace and using method chain' do
expect_offense(<<~RUBY)
baz.map { |x|
Expand Down

0 comments on commit 87501e5

Please sign in to comment.