Skip to content

Commit

Permalink
Make Metrics/BlockNesting aware of numbered parameter
Browse files Browse the repository at this point in the history
This PR makes `Metrics/BlockNesting` aware of numbered parameter.
  • Loading branch information
koic committed Apr 2, 2023
1 parent 3f5753a commit 97a6d52
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11750](https://github.com/rubocop/rubocop/pull/11750): Make `Metrics/BlockNesting` aware of numbered parameter. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/metrics/block_nesting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def check_nesting_level(node, max, current_level)
def consider_node?(node)
return true if NESTING_BLOCKS.include?(node.type)

count_blocks? && node.block_type?
count_blocks? && (node.block_type? || node.numblock_type?)
end

def message(max)
Expand Down
54 changes: 54 additions & 0 deletions spec/rubocop/cop/metrics/block_nesting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,30 @@
end
RUBY
end

context 'when numbered parameter', :ruby27 do
it 'accepts nested multiline blocks' do
expect_no_offenses(<<~RUBY)
if a
if b
[1, 2].each do
puts _1
end
end
end
RUBY
end

it 'accepts nested inline blocks' do
expect_no_offenses(<<~RUBY)
if a
if b
[1, 2].each { puts _1 }
end
end
RUBY
end
end
end

context 'when CountBlocks is true' do
Expand Down Expand Up @@ -259,5 +283,35 @@
RUBY
end
end

context 'when numbered parameter', :ruby27 do
context 'nested multiline block' do
it 'registers an offense' do
expect_offense(<<~RUBY)
if a
if b
[1, 2].each do
^^^^^^^^^^^^^^ Avoid more than 2 levels of block nesting.
puts _1
end
end
end
RUBY
end
end

context 'nested inline block' do
it 'registers an offense' do
expect_offense(<<~RUBY)
if a
if b
[1, 2].each { puts _1 }
^^^^^^^^^^^^^^^^^^^^^^^ Avoid more than 2 levels of block nesting.
end
end
RUBY
end
end
end
end
end

0 comments on commit 97a6d52

Please sign in to comment.