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

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

Overview

Use a consistent style for parentheses in factory bot calls.

Examples:


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

‘EnforcedStyle: require_parentheses` (default)


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

‘EnforcedStyle: omit_parentheses`


# good
create :user
build :user
create :login
create :login

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

Constant Summary collapse

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

Constants included from RSpec::FactoryBot::Language

RSpec::FactoryBot::Language::METHODS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RSpec::FactoryBot::Language

#factory_bot?

Class Method Details

.autocorrect_incompatible_withObject



49
50
51
# File 'lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb', line 49

def self.autocorrect_incompatible_with
  [Style::MethodCallWithArgsParentheses]
end

Instance Method Details

#factory_call(node) ⇒ Object



61
62
63
64
65
66
# File 'lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb', line 61

def_node_matcher :factory_call, "  (send\n    {#factory_bot? nil?} %FACTORY_CALLS\n    {sym str send lvar} _*\n  )\n"

#on_send(node) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb', line 68

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

  factory_call(node) do
    return if node.method?(:generate) && node.arguments.count > 1

    if node.parenthesized?
      process_with_parentheses(node)
    else
      process_without_parentheses(node)
    end
  end
end