Class: RuboCop::Cop::FactoryBot::ExcessiveCreateList

Inherits:
Base
  • Object
show all
Includes:
ConfigurableExplicitOnly
Defined in:
lib/rubocop/cop/factory_bot/excessive_create_list.rb

Overview

Check for excessive model creation in a list.

Examples:

MaxAmount: 10 (default)

# We do not allow more than 10 items to be created

# bad
create_list(:merge_request, 1000, state: :opened)

# good
create_list(:merge_request, 10, state: :opened)

MaxAmount: 20

# We do not allow more than 20 items to be created

# bad
create_list(:merge_request, 1000, state: :opened)

# good
create_list(:merge_request, 15, state: :opened)

Constant Summary collapse

MESSAGE =
'Avoid using `create_list` with more than %<max_amount>s items.'
RESTRICT_ON_SEND =
%i[create_list].freeze

Constants included from FactoryBot::Language

FactoryBot::Language::METHODS

Instance Method Summary collapse

Methods included from ConfigurableExplicitOnly

#explicit_only?, #factory_call?

Methods included from FactoryBot::Language

#factory_bot?

Instance Method Details

#create_list?(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/factory_bot/excessive_create_list.rb', line 33

def_node_matcher :create_list?, <<~PATTERN
  (send #factory_call? :create_list {sym str} $(int _) ...)
PATTERN

#on_send(node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/factory_bot/excessive_create_list.rb', line 39

def on_send(node)
  number_node = create_list?(node)
  return unless number_node

  max_amount = cop_config['MaxAmount']
  return if number_node.value <= max_amount

  add_offense(number_node, message:
    format(MESSAGE, max_amount: max_amount))
end