Class: RuboCop::Cop::FactoryBot::ConsistentParenthesesStyle

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

Overview

Use a consistent style for parentheses in factory_bot calls.

Examples:

‘EnforcedStyle: require_parentheses` (default)


# bad
create :user
build :login

# good
create(:user)
build(:login)

‘EnforcedStyle: omit_parentheses`


# bad
create(:user)
build(:login)

# good
create :user
build :login

# also good
# when method name and first argument are not on same line
create(
  :user
)
build(
  :user,
  name: 'foo'
)

‘ExplicitOnly: false` (default)


# bad - with `EnforcedStyle: require_parentheses`
FactoryBot.create :user
build :user

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

‘ExplicitOnly: true`


# bad - with `EnforcedStyle: require_parentheses`
FactoryBot.create :user
FactoryBot.build :user

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

Constant Summary collapse

MSG_REQUIRE_PARENS =
'Prefer method call with parentheses'
MSG_OMIT_PARENS =
'Prefer method call without parentheses'
FACTORY_CALLS =
RuboCop::FactoryBot::Language::METHODS
RESTRICT_ON_SEND =
FACTORY_CALLS

Constants included from FactoryBot::Language

FactoryBot::Language::METHODS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConfigurableExplicitOnly

#explicit_only?, #factory_call?

Methods included from FactoryBot::Language

#factory_bot?

Class Method Details

.autocorrect_incompatible_withObject



89
90
91
# File 'lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb', line 89

def self.autocorrect_incompatible_with
  [Style::MethodCallWithArgsParentheses]
end

Instance Method Details

#factory_call(node) ⇒ Object



71
72
73
74
75
76
# File 'lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb', line 71

def_node_matcher :factory_call, <<~PATTERN
  (send
    #factory_call? %FACTORY_CALLS
    {sym str send lvar} _*
  )
PATTERN

#omit_hash_value?(node) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb', line 79

def_node_matcher :omit_hash_value?, <<~PATTERN
  (send
    #factory_call? %FACTORY_CALLS
    {sym str send lvar}
    (hash
      <value_omission? ...>
    )
  )
PATTERN

#on_send(node) ⇒ Object



93
94
95
96
97
# File 'lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb', line 93

def on_send(node)
  return if ambiguous_without_parentheses?(node)

  factory_call(node) { register_offense(node) }
end