Sha256: 9a5fb4e8943ad111f065120155a88cc231d70312ca99ac1fc58c599effbe6a46

Contents?: true

Size: 999 Bytes

Versions: 1

Compression:

Stored size: 999 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      module FactoryBot
        # Use string value when setting the class attribute explicitly.
        #
        # @example
        #   # bad
        #   factory :foo, class: Foo do
        #   end
        #
        #   # good
        #   factory :foo, class: 'Foo' do
        #   end
        class FactoryClassName < Cop
          MSG = "Pass '%<class_name>s' instead of %<class_name>s."

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

          def on_send(node)
            class_name(node) do |cn|
              add_offense(cn, message: format(MSG, class_name: cn.const_name))
            end
          end

          def autocorrect(node)
            lambda do |corrector|
              corrector.replace(node.loc.expression, "'#{node.source}'")
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/factory_bot/factory_class_name.rb