Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10186] Explicit block arg is not counted for Metrics/ParameterLists #10188

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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