Sha256: 60f57a3eb74789e319befe73c1f6b52e905ef7786f1703bc602a2f4ca2e254d9

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module FactoryBot
      # Do not create a FactoryBot sequence for an id column.
      #
      # @example
      #   # bad - can lead to conflicts between FactoryBot and DB sequences
      #   factory :foo do
      #     sequence :id
      #   end
      #
      #   # good - a non-id column
      #   factory :foo do
      #     sequence :some_non_id_column
      #   end
      #
      class IdSequence < ::RuboCop::Cop::Base
        extend AutoCorrector
        include RangeHelp
        include RuboCop::FactoryBot::Language

        MSG = 'Do not create a sequence for an id attribute'
        RESTRICT_ON_SEND = %i[sequence].freeze

        def on_send(node)
          return unless node.receiver.nil? || factory_bot?(node.receiver)
          return unless node.first_argument&.sym_type? &&
            node.first_argument.value == :id

          add_offense(node) do |corrector|
            range_to_remove = range_by_whole_lines(
              node.source_range,
              include_final_newline: true
            )

            corrector.remove(range_to_remove)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 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/id_sequence.rb
rubocop-factory_bot-2.26.1 lib/rubocop/cop/factory_bot/id_sequence.rb
rubocop-factory_bot-2.26.0 lib/rubocop/cop/factory_bot/id_sequence.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/id_sequence.rb
rubocop-factory_bot-2.25.1 lib/rubocop/cop/factory_bot/id_sequence.rb