Sha256: 7bb449f8446a7f679ef25b7da613db33c4e05698280fb25c398e04fa0c0cc78a

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module FactoryBot
      # Check for excessive model creation in a list.
      #
      # @example 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)
      #
      # @example 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)
      #
      class ExcessiveCreateList < ::RuboCop::Cop::Base
        include ConfigurableExplicitOnly

        MESSAGE =
          'Avoid using `create_list` with more than %<max_amount>s items.'

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

        RESTRICT_ON_SEND = %i[create_list].freeze

        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
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-factory_bot-2.26.1/lib/rubocop/cop/factory_bot/excessive_create_list.rb
rubocop-factory_bot-2.26.1 lib/rubocop/cop/factory_bot/excessive_create_list.rb
rubocop-factory_bot-2.26.0 lib/rubocop/cop/factory_bot/excessive_create_list.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.25.1/lib/rubocop/cop/factory_bot/excessive_create_list.rb
rubocop-factory_bot-2.25.1 lib/rubocop/cop/factory_bot/excessive_create_list.rb
rubocop-factory_bot-2.25.0 lib/rubocop/cop/factory_bot/excessive_create_list.rb