Skip to content

Commit

Permalink
Merge pull request rubocop#13674 from dvandersluis/block-delimiters-s…
Browse files Browse the repository at this point in the history
…afe-nav

Add support for safe navigation to `Style/BlockDelimiters`
  • Loading branch information
koic authored Jan 13, 2025
2 parents c98a83e + a860646 commit f852457
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13674](https://github.com/rubocop/rubocop/pull/13674): Add support for safe navigation to `Style/BlockDelimiters`. ([@dvandersluis][])
15 changes: 3 additions & 12 deletions lib/rubocop/cop/style/block_delimiters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def on_send(node)
end
end
end
alias on_csend on_send

def on_block(node)
return if ignored_node?(node)
Expand Down Expand Up @@ -348,7 +349,7 @@ def get_blocks(node, &block)
case node.type
when :block, :numblock
yield node
when :send
when :send, :csend
# When a method has an argument which is another method with a block,
# that block needs braces, otherwise a syntax error will be introduced
# for subsequent arguments.
Expand All @@ -369,9 +370,8 @@ def get_blocks(node, &block)
end
# rubocop:enable Metrics/CyclomaticComplexity

# rubocop:disable Metrics/CyclomaticComplexity
def proper_block_style?(node)
return true if require_braces?(node) || require_do_end?(node)
return true if require_do_end?(node)
return special_method_proper_block_style?(node) if special_method?(node.method_name)

case style
Expand All @@ -381,15 +381,6 @@ def proper_block_style?(node)
when :always_braces then braces_style?(node)
end
end
# rubocop:enable Metrics/CyclomaticComplexity

def require_braces?(node)
return false unless node.braces?

node.each_ancestor(:send).any? do |send|
send.arithmetic_operation? && node.source_range.end_pos < send.loc.selector.begin_pos
end
end

def require_do_end?(node)
return false if node.braces? || node.multiline?
Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/cop/style/block_delimiters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,22 @@
RUBY
end

it 'accepts braces with safe navigation if do-end would change the meaning' do
expect_no_offenses(<<~RUBY)
foo&.bar baz {
y
}
RUBY
end

it 'accepts braces with chained safe navigation if do-end would change the meaning' do
expect_no_offenses(<<~RUBY)
foo.bar baz {
y
}&.quux
RUBY
end

it 'accepts a multi-line functional block with {} if it is an ignored method' do
expect_no_offenses(<<~RUBY)
foo = proc {
Expand Down Expand Up @@ -728,6 +744,13 @@
RUBY
end

it 'does not register an offense for a multi-line block with `{` and `}` with method chain and safe navigation' do
expect_no_offenses(<<~RUBY)
foo x&.bar + baz {
}.qux.quux
RUBY
end

it 'does not register an offense when multi-line blocks to `{` and `}` with arithmetic operation method chain' do
expect_no_offenses(<<~RUBY)
foo bar + baz {
Expand Down

0 comments on commit f852457

Please sign in to comment.