Class: RuboCop::Cop::FactoryBot::FactoryNameStyle

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, ConfigurableExplicitOnly, FactoryBot::Language
Defined in:
lib/rubocop/cop/factory_bot/factory_name_style.rb

Overview

Checks for name style for argument of FactoryBot::Syntax::Methods.

Examples:

EnforcedStyle: symbol (default)

# bad
create('user')
build "user", username: "NAME"

# good
create(:user)
build :user, username: "NAME"

# good - namespaced models
create('users/internal')

EnforcedStyle: string

# bad
create(:user)
build :user, username: "NAME"

# good
create('user')
build "user", username: "NAME"

‘ExplicitOnly: false` (default)


# bad - with `EnforcedStyle: symbol`
FactoryBot.create('user')
create('user')

# good - with `EnforcedStyle: symbol`
FactoryBot.create(:user)
create(:user)

‘ExplicitOnly: true`


# bad - with `EnforcedStyle: symbol`
FactoryBot.create(:user)
FactoryBot.build "user", username: "NAME"

# good - with `EnforcedStyle: symbol`
FactoryBot.create('user')
FactoryBot.build "user", username: "NAME"
FactoryBot.create(:user)
create(:user)

Constant Summary collapse

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

Constants included from FactoryBot::Language

FactoryBot::Language::METHODS

Instance Method Summary collapse

Methods included from ConfigurableExplicitOnly

#explicit_only?, #factory_call?

Methods included from FactoryBot::Language

#factory_bot?

Instance Method Details

#factory_call(node) ⇒ Object



62
63
64
65
66
67
# File 'lib/rubocop/cop/factory_bot/factory_name_style.rb', line 62

def_node_matcher :factory_call, <<~PATTERN
  (send
    #factory_call? %FACTORY_CALLS
    ${str sym} ...
  )
PATTERN

#on_send(node) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/rubocop/cop/factory_bot/factory_name_style.rb', line 69

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