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

Metrics/ParameterLists supports MaxOptionalParameters config parameter #9010

Merged
merged 1 commit into from Nov 26, 2020
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 @@
* [#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
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
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
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