Class: RuboCop::Cop::Kaizo::KeywordArguments

Inherits:
Base
  • Object
show all
Includes:
ArgumentCounting
Defined in:
lib/rubocop/cop/kaizo/keyword_arguments.rb

Overview

Checks that a method does not declare too many keyword arguments.

Keyword arguments are self-documenting, but a long list of them is still a sign that the related values want to be modeled as an object. Required (kwarg) and optional (kwoptarg) parameters are counted; **rest is not.

Configuration

[+Max+] Most keyword arguments a method may declare. Default: 1. [+AllowedMethods+] Method names exempt from the limit. Default: none. [+AllowedPatterns+] Regexps matched against the method name; a match is exempt. Default: none. [+Exclude+] Paths the cop skips. Default: **/spec// and */test//* -- wide keyword interfaces are the testing idiom (FactoryBot-style builders), so keyword counts are not policed there. Set it to [] to police tests too.

Kaizo/KeywordArguments:
Max: 2
AllowedMethods:
  - initialize

Examples:

Max: 2

# bad
def calculate_volume(width:, length:, height:)
end

# good
def calculate_volume(shape)
end

AllowedMethods: ['initialize'] (default: [])

# good - exempt by name
def initialize(host:, port:, ssl: true)
end

Constant Summary collapse

KIND =
"keyword arguments".freeze

Constants included from ArgumentCounting

ArgumentCounting::DEFINE_METHODS, ArgumentCounting::KEYWORD_TYPES, ArgumentCounting::MSG, ArgumentCounting::OPERATOR_METHOD_NAMES, ArgumentCounting::POSITIONAL_TYPES, ArgumentCounting::STRUCT_OR_DATA

Method Summary

Methods included from ArgumentCounting

#on_block, #on_def