diff --git a/changelog/change_explicit_block_arg_is_not_counted_for_metrics_parameter_lists.md b/changelog/change_explicit_block_arg_is_not_counted_for_metrics_parameter_lists.md new file mode 100644 index 00000000000..c112ac2f7df --- /dev/null +++ b/changelog/change_explicit_block_arg_is_not_counted_for_metrics_parameter_lists.md @@ -0,0 +1 @@ +* [#10186](https://github.com/rubocop/rubocop/issues/10186): Explicit block arg is not counted for `Metrics/ParameterLists`. ([@koic][]) diff --git a/lib/rubocop/cop/metrics/parameter_lists.rb b/lib/rubocop/cop/metrics/parameter_lists.rb index 060fb9cf90a..253ad08c342 100644 --- a/lib/rubocop/cop/metrics/parameter_lists.rb +++ b/lib/rubocop/cop/metrics/parameter_lists.rb @@ -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) @@ -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 diff --git a/spec/rubocop/cop/metrics/parameter_lists_spec.rb b/spec/rubocop/cop/metrics/parameter_lists_spec.rb index 36558ab4bc6..84b1b15f5c4 100644 --- a/spec/rubocop/cop/metrics/parameter_lists_spec.rb +++ b/spec/rubocop/cop/metrics/parameter_lists_spec.rb @@ -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