-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
parameter_lists.rb
53 lines (42 loc) · 1.35 KB
/
parameter_lists.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true
module RuboCop
module Cop
module Metrics
# This cop checks for methods with too many parameters.
# The maximum number of parameters is configurable.
# Keyword arguments can optionally be excluded from the total count.
class ParameterLists < Base
include ConfigurableMax
MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
'[%<count>d/%<max>d]'
NAMED_KEYWORD_TYPES = %i[kwoptarg kwarg].freeze
private_constant :NAMED_KEYWORD_TYPES
def on_args(node)
count = args_count(node)
return unless count > max_params
return if argument_to_lambda_or_proc?(node)
add_offense(node, message: format(MSG, max: max_params, count: args_count(node))) do
self.max = count
end
end
private
def_node_matcher :argument_to_lambda_or_proc?, <<~PATTERN
^lambda_or_proc?
PATTERN
def args_count(node)
if count_keyword_args?
node.children.size
else
node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) }
end
end
def max_params
cop_config['Max']
end
def count_keyword_args?
cop_config['CountKeywordArgs']
end
end
end
end
end