Skip to content

Commit

Permalink
Metrics/ParameterLists supports MaxOptionalParameters config para…
Browse files Browse the repository at this point in the history
…meter
  • Loading branch information
fatkodima authored and bbatsov committed Nov 26, 2020
1 parent b5357aa commit 356baf8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9010](https://github.com/rubocop-hq/rubocop/pull/9010): `Metrics/ParameterLists` supports `MaxOptionalParameters` config parameter. ([@fatkodima][])
2 changes: 2 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2185,8 +2185,10 @@ Metrics/ParameterLists:
StyleGuide: '#too-many-params'
Enabled: true
VersionAdded: '0.25'
VersionChanged: '<<next>>'
Max: 5
CountKeywordArgs: true
MaxOptionalParameters: 3

Metrics/PerceivedComplexity:
Description: >-
Expand Down
33 changes: 33 additions & 0 deletions lib/rubocop/cop/metrics/parameter_lists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,44 @@ module Metrics
# # good (assuming Max is 3)
# def foo(a, b, c, d: 1)
# end
#
# This cop also checks for the maximum number of optional parameters.
# This can be configured using the `MaxOptionalParameters` config option.
#
# @example MaxOptionalParameters: 3 (default)
# # good
# def foo(a = 1, b = 2, c = 3)
# end
#
# @example MaxOptionalParameters: 2
# # bad
# def foo(a = 1, b = 2, c = 3)
# end
#
class ParameterLists < Base
include ConfigurableMax

MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
'[%<count>d/%<max>d]'
OPTIONAL_PARAMETERS_MSG = 'Method has too many optional parameters. [%<count>d/%<max>d]'

NAMED_KEYWORD_TYPES = %i[kwoptarg kwarg].freeze
private_constant :NAMED_KEYWORD_TYPES

def on_def(node)
optargs = node.arguments.select(&:optarg_type?)
return if optargs.count <= max_optional_parameters

message = format(
OPTIONAL_PARAMETERS_MSG,
max: max_optional_parameters,
count: optargs.count
)

add_offense(node, message: message)
end
alias on_defs on_def

def on_args(node)
count = args_count(node)
return unless count > max_params
Expand Down Expand Up @@ -74,6 +103,10 @@ def max_params
cop_config['Max']
end

def max_optional_parameters
cop_config['MaxOptionalParameters']
end

def count_keyword_args?
cop_config['CountKeywordArgs']
end
Expand Down
18 changes: 17 additions & 1 deletion spec/rubocop/cop/metrics/parameter_lists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
let(:cop_config) do
{
'Max' => 4,
'CountKeywordArgs' => true
'CountKeywordArgs' => true,
'MaxOptionalParameters' => 3
}
end

Expand Down Expand Up @@ -62,4 +63,19 @@ def meth(a, b, c, d:, e:)
RUBY
end
end

it 'registers an offense when optargs count exceeds the maximum' do
expect_offense(<<~RUBY)
def foo(a = 1, b = 2, c = 3, d = 4)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Method has too many optional parameters. [4/3]
end
RUBY
end

it 'does not register an offense when method has allowed amount of optargs' do
expect_no_offenses(<<~RUBY)
def foo(a, b = 2, c = 3, d = 4)
end
RUBY
end
end

0 comments on commit 356baf8

Please sign in to comment.