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

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?

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

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



59
60
61
62
63
# File 'lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb', line 59

def_node_matcher :factory_call, <<-PATTERN
    (send
      ${#factory_bot? nil?} %FACTORY_CALLS
    $...)
PATTERN

#nested_call?(node) ⇒ Boolean



96
97
98
# File 'lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb', line 96

def nested_call?(node)
  node.parent&.send_type?
end

#on_send(node) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb', line 65

def on_send(node)
  return if nested_call?(node) # prevent from nested matching

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

#process_with_parentheses(node) ⇒ Object



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

def process_with_parentheses(node)
  return unless style == :omit_parentheses
  return unless same_line?(node, node.first_argument)

  add_offense(node.loc.selector,
              message: MSG_OMIT_PARENS) do |corrector|
    remove_parentheses(corrector, node)
  end
end

#process_without_parentheses(node) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/rubocop/cop/rspec/factory_bot/consistent_parentheses_style.rb', line 87

def process_without_parentheses(node)
  return unless style == :require_parentheses

  add_offense(node.loc.selector,
              message: MSG_REQUIRE_PARENS) do |corrector|
    add_parentheses(node, corrector)
  end
end