Class: RuboCop::Cop::Momocop::FactoryBotSingularFactoryName

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Momocop::Helpers::FactoryBotHelper
Defined in:
lib/rubocop/cop/momocop/factory_bot_singular_factory_name.rb

Overview

Ensures that FactoryBot factories have singular names.

Examples:

# bad
FactoryBot.define do
  factory :users do
  end
end

# good
FactoryBot.define do
  factory :user do
  end
end

Constant Summary collapse

MSG =
'Factory name should be singular, not plural.'
RESTRICT_ON_SEND =
%i[factory].freeze

Constants included from Momocop::Helpers::FactoryBotHelper

Momocop::Helpers::FactoryBotHelper::RUBOCOP_HELPER_METHODS

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/momocop/factory_bot_singular_factory_name.rb', line 30

def on_send(node)
  return unless inside_factory_bot_define?(node)

  factory_name = node.first_argument.value.to_s
  return if factory_name.singularize == factory_name

  add_offense(node.first_argument) do |corrector|
    if node.first_argument.type == :sym
      corrector.replace(node.first_argument.loc.expression, ":#{factory_name.singularize}")
    elsif node.first_argument.type == :str
      if node.first_argument.source.start_with?("'")
        corrector.replace(node.first_argument.loc.expression, "'#{factory_name.singularize}'")
      elsif node.first_argument.source.start_with?('"')
        corrector.replace(node.first_argument.loc.expression, "\"#{factory_name.singularize}\"")
      end
    end
  end
end