Sha256: bc02564ac90259fa24adb23569690740ffa56488aa6832626094538d4a99915c

Contents?: true

Size: 1.49 KB

Versions: 15

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module FactoryBot
      # Use string value when setting the class attribute explicitly.
      #
      # This cop would promote faster tests by lazy-loading of
      # application files. Also, this could help you suppress potential bugs
      # in combination with external libraries by avoiding a preload of
      # application files from the factory files.
      #
      # @example
      #   # bad
      #   factory :foo, class: Foo do
      #   end
      #
      #   # good
      #   factory :foo, class: 'Foo' do
      #   end
      #
      class FactoryClassName < ::RuboCop::Cop::Base
        extend AutoCorrector

        MSG = "Pass '%<class_name>s' string instead of `%<class_name>s` " \
              'constant.'
        ALLOWED_CONSTANTS = %w[Hash OpenStruct].freeze
        RESTRICT_ON_SEND = %i[factory].freeze

        # @!method class_name(node)
        def_node_matcher :class_name, <<~PATTERN
          (send _ :factory _ (hash <(pair (sym :class) $(const ...)) ...>))
        PATTERN

        def on_send(node)
          class_name(node) do |cn|
            next if allowed?(cn.const_name)

            msg = format(MSG, class_name: cn.const_name)
            add_offense(cn, message: msg) do |corrector|
              corrector.replace(cn, "'#{cn.source}'")
            end
          end
        end

        private

        def allowed?(const_name)
          ALLOWED_CONSTANTS.include?(const_name)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 5 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/factory_class_name.rb
rubocop-factory_bot-2.26.1 lib/rubocop/cop/factory_bot/factory_class_name.rb
rubocop-factory_bot-2.26.0 lib/rubocop/cop/factory_bot/factory_class_name.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/factory_class_name.rb
rubocop-factory_bot-2.25.1 lib/rubocop/cop/factory_bot/factory_class_name.rb
rubocop-factory_bot-2.25.0 lib/rubocop/cop/factory_bot/factory_class_name.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_class_name.rb
rubocop-factory_bot-2.24.0 lib/rubocop/cop/factory_bot/factory_class_name.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_class_name.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_class_name.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_class_name.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/cop/factory_bot/factory_class_name.rb
rubocop-factory_bot-2.23.1 lib/rubocop/cop/factory_bot/factory_class_name.rb
rubocop-factory_bot-2.23.0 lib/rubocop/cop/factory_bot/factory_class_name.rb
rubocop-factory_bot-2.22.0 lib/rubocop/cop/factory_bot/factory_class_name.rb