Skip to content

Commit

Permalink
[Fix #10186] Explicit block arg is not counted for `Metrics/Parameter…
Browse files Browse the repository at this point in the history
…Lists`

Fixes #10186.

Explicit block arg `&block` is not counted by default to prevent
erroneous change that is avoided by making block arg implicit.
  • Loading branch information
koic authored and bbatsov committed Oct 19, 2021
1 parent 1894717 commit 5b27789
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
@@ -0,0 +1 @@
* [#10186](https://github.com/rubocop/rubocop/issues/10186): Explicit block arg is not counted for `Metrics/ParameterLists`. ([@koic][])
7 changes: 5 additions & 2 deletions lib/rubocop/cop/metrics/parameter_lists.rb
Expand Up @@ -9,6 +9,9 @@ module Metrics
# Keyword arguments can optionally be excluded from the total count,
# as they add less complexity than positional or optional parameters.
#
# NOTE: Explicit block argument `&block` is not counted to prevent
# erroneous change that is avoided by making block argument implicit.
#
# @example Max: 3
# # good
# def foo(a, b, c = 1)
Expand Down Expand Up @@ -94,9 +97,9 @@ def on_args(node)

def args_count(node)
if count_keyword_args?
node.children.size
node.children.count { |a| !a.blockarg_type? }
else
node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) }
node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) && !a.blockarg_type? }
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/metrics/parameter_lists_spec.rb
Expand Up @@ -82,4 +82,18 @@ def foo(a, b = 2, c = 3, d = 4)
end
RUBY
end

it 'does not register an offense when method has allowed amount of args with block arg' do
expect_no_offenses(<<~RUBY)
def foo(a, b, c, d, &block)
end
RUBY
end

it 'does not register an offense when method has no args' do
expect_no_offenses(<<~RUBY)
def foo
end
RUBY
end
end

0 comments on commit 5b27789

Please sign in to comment.