Class: RuboCop::Cop::RSpec::FactoryBot::ConsistentParenthesesStyle
- Inherits:
-
Base
- Object
- Base
- Base
- RuboCop::Cop::RSpec::FactoryBot::ConsistentParenthesesStyle
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.
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
RSpec::FactoryBot::Language::METHODS
Class Method Summary
collapse
Instance Method Summary
collapse
#factory_bot?
Methods inherited from Base
inherited, #on_new_investigation
#block_pattern, #numblock_pattern, #send_pattern
#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Class Method Details
.autocorrect_incompatible_with ⇒ Object
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)
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
|