Skip to content

Commit

Permalink
[Fix #9465] Update Metrics/ParameterLists to be able to write `MaxO…
Browse files Browse the repository at this point in the history
…ptionalParameters` in rubocop_todo.yml.
  • Loading branch information
dvandersluis authored and bbatsov committed Feb 1, 2021
1 parent 61627d6 commit 2c2a8e3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_update_metricsparameterlists_to_be_able.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9465](https://github.com/rubocop-hq/rubocop/issues/9465): Update `Metrics/ParameterLists` to be able to write `MaxOptionalParameters` in rubocop_todo.yml. ([@dvandersluis][])
6 changes: 4 additions & 2 deletions lib/rubocop/cop/metrics/parameter_lists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module Metrics
#
class ParameterLists < Base
exclude_limit 'Max'

exclude_limit 'MaxOptionalParameters'

MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
'[%<count>d/%<max>d]'
Expand All @@ -71,7 +71,9 @@ def on_def(node)
count: optargs.count
)

add_offense(node, message: message)
add_offense(node, message: message) do
self.max_optional_parameters = optargs.count
end
end
alias on_defs on_def

Expand Down
10 changes: 10 additions & 0 deletions spec/rubocop/cop/metrics/parameter_lists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,20 @@ def meth(a, b, c, d:, e:)

it 'registers an offense when optargs count exceeds the maximum' do
expect_offense(<<~RUBY)
def foo(a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid parameter lists longer than 4 parameters. [7/4]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Method has too many optional parameters. [7/3]
end
def foo(a = 1, b = 2, c = 3, d = 4)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Method has too many optional parameters. [4/3]
end
RUBY

expect(cop.config_to_allow_offenses[:exclude_limit]).to eq(
'Max' => 7,
'MaxOptionalParameters' => 7
)
end

it 'does not register an offense when method has allowed amount of optargs' do
Expand Down

0 comments on commit 2c2a8e3

Please sign in to comment.