Module: RuboCop::Cop::Kaizo::ArgumentCounting

Included in:
KeywordArguments, PositionalArguments, TotalArguments
Defined in:
lib/rubocop/cop/kaizo/argument_counting.rb

Overview

Shared traversal and counting for the argument-arity cops.

Walks method definitions written with def, def self., define_method, and define_singleton_method, and reports when the argument count produced by the including cop exceeds the configured Max. Including cops must define a private arity(arguments) method and a KIND constant. Operator methods ([]=, [], <=>, ...) and the initialize of a Struct.new/Data.define block are exempt.

Constant Summary collapse

POSITIONAL_TYPES =
%i[arg optarg].freeze
KEYWORD_TYPES =
%i[kwarg kwoptarg].freeze
DEFINE_METHODS =
%i[define_method define_singleton_method].freeze
STRUCT_OR_DATA =
{ "Struct" => :new, "Data" => :define }.freeze
OPERATOR_METHOD_NAMES =

Ruby's operator method names, mirroring the list behind RuboCop's operator_method? (private upstream, so it cannot be reused). Needed only for the define_method(:[]=) form, where the name being defined is a symbol argument rather than a def node we could ask directly.

[
  :!, :!=, :"!@", :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=,
  :<=>, :==, :===, :=~, :>, :>=, :>>, :[], :[]=, :^, :`, :|, :~, :"~@"
].freeze
MSG =
"Method has too many %<kind>s. [%<count>d/%<max>d]".freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock, on_itblock



35
36
37
38
39
40
# File 'lib/rubocop/cop/kaizo/argument_counting.rb', line 35

def on_block(node)
  return unless DEFINE_METHODS.include?(node.method_name)
  return if defines_operator?(node)

  check_arity(node)
end

#on_def(node) ⇒ Object Also known as: on_defs



28
29
30
31
32
# File 'lib/rubocop/cop/kaizo/argument_counting.rb', line 28

def on_def(node)
  return if exempt?(node)

  check_arity(node)
end