Sha256: 2ff2145077c49d6249ef0d62ee9881b3478eb02b97e2f97b6e7bb7a6a4fde691

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module FactoryBot
      # Checks for name style for argument of FactoryBot::Syntax::Methods.
      #
      # @example EnforcedStyle: symbol (default)
      #   # bad
      #   create('user')
      #   build "user", username: "NAME"
      #
      #   # good
      #   create(:user)
      #   build :user, username: "NAME"
      #
      # @example EnforcedStyle: string
      #   # bad
      #   create(:user)
      #   build :user, username: "NAME"
      #
      #   # good
      #   create('user')
      #   build "user", username: "NAME"
      #
      class FactoryNameStyle < ::RuboCop::Cop::Base
        extend AutoCorrector
        include ConfigurableEnforcedStyle
        include RuboCop::FactoryBot::Language

        MSG = 'Use %<prefer>s to refer to a factory.'
        FACTORY_CALLS = RuboCop::FactoryBot::Language::METHODS
        RESTRICT_ON_SEND = FACTORY_CALLS

        # @!method factory_call(node)
        def_node_matcher :factory_call, <<-PATTERN
          (send
            {#factory_bot? nil?} %FACTORY_CALLS
            ${str sym} ...
          )
        PATTERN

        def on_send(node)
          factory_call(node) do |name|
            if offense_for_symbol_style?(name)
              register_offense(name, name.value.to_sym.inspect)
            elsif offense_for_string_style?(name)
              register_offense(name, name.value.to_s.inspect)
            end
          end
        end

        private

        def offense_for_symbol_style?(name)
          name.str_type? && style == :symbol
        end

        def offense_for_string_style?(name)
          name.sym_type? && style == :string
        end

        def register_offense(name, prefer)
          add_offense(name,
                      message: format(MSG, prefer: style.to_s)) do |corrector|
            corrector.replace(name, prefer)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-factory_bot-2.22.0 lib/rubocop/cop/factory_bot/factory_name_style.rb