lib/rubocop/cop/rspec/predicate_matcher.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/predicate_matcher.rb in rubocop-rspec-1.42.0
- old
+ new
@@ -12,15 +12,18 @@
'`%<predicate_name>s`.'
private
def check_inflected(node)
- predicate_in_actual?(node) do |predicate|
- add_offense(
- node,
- message: message_inflected(predicate)
- )
+ predicate_in_actual?(node) do |predicate, to, matcher|
+ msg = message_inflected(predicate)
+ add_offense(node, message: msg) do |corrector|
+ remove_predicate(corrector, predicate)
+ corrector.replace(node.loc.selector,
+ true?(to, matcher) ? 'to' : 'not_to')
+ rewrite_matcher(corrector, predicate, matcher)
+ end
end
end
def_node_matcher :predicate_in_actual?, <<-PATTERN
(send
@@ -74,21 +77,10 @@
"be_#{name[0..-2]}"
end
end
# rubocop:enable Metrics/MethodLength
- def autocorrect_inflected(node)
- predicate_in_actual?(node) do |predicate, to, matcher|
- lambda do |corrector|
- remove_predicate(corrector, predicate)
- corrector.replace(node.loc.selector,
- true?(to, matcher) ? 'to' : 'not_to')
- rewrite_matcher(corrector, predicate, matcher)
- end
- end
- end
-
def remove_predicate(corrector, predicate)
range = predicate.loc.dot.with(
end_pos: predicate.loc.expression.end_pos
)
@@ -121,11 +113,10 @@
to_symbol == :to ? result : !result
end
end
# A helper for `explicit` style
- # rubocop:disable Metrics/ModuleLength
module ExplicitHelper
include RuboCop::RSpec::Language
extend NodePattern::Macros
MSG_EXPLICIT = 'Prefer using `%<predicate_name>s` over ' \
@@ -141,26 +132,25 @@
def allowed_explicit_matchers
cop_config.fetch('AllowedExplicitMatchers', []) + BUILT_IN_MATCHERS
end
def check_explicit(node) # rubocop:disable Metrics/MethodLength
- predicate_matcher_block?(node) do |_actual, matcher|
- add_offense(
- node,
- message: message_explicit(matcher)
- )
+ predicate_matcher_block?(node) do |actual, matcher|
+ add_offense(node, message: message_explicit(matcher)) do |corrector|
+ to_node = node.send_node
+ corrector_explicit(corrector, to_node, actual, matcher, to_node)
+ end
ignore_node(node.children.first)
return
end
return if part_of_ignored_node?(node)
- predicate_matcher?(node) do |_actual, matcher|
- add_offense(
- node,
- message: message_explicit(matcher)
- )
+ predicate_matcher?(node) do |actual, matcher|
+ add_offense(node, message: message_explicit(matcher)) do |corrector|
+ corrector_explicit(corrector, node, actual, matcher, matcher)
+ end
end
end
def_node_matcher :predicate_matcher?, <<-PATTERN
(send
@@ -191,37 +181,17 @@
format(MSG_EXPLICIT,
predicate_name: to_predicate_method(matcher.method_name),
matcher_name: matcher.method_name)
end
- def autocorrect_explicit(node)
- autocorrect_explicit_send(node) ||
- autocorrect_explicit_block(node)
+ def corrector_explicit(corrector, to_node, actual, matcher, block_child)
+ replacement_matcher = replacement_matcher(to_node)
+ corrector.replace(matcher.loc.expression, replacement_matcher)
+ move_predicate(corrector, actual, matcher, block_child)
+ corrector.replace(to_node.loc.selector, 'to')
end
- def autocorrect_explicit_send(node)
- predicate_matcher?(node) do |actual, matcher|
- corrector_explicit(node, actual, matcher, matcher)
- end
- end
-
- def autocorrect_explicit_block(node)
- predicate_matcher_block?(node) do |actual, matcher|
- to_node = node.send_node
- corrector_explicit(to_node, actual, matcher, to_node)
- end
- end
-
- def corrector_explicit(to_node, actual, matcher, block_child)
- lambda do |corrector|
- replacement_matcher = replacement_matcher(to_node)
- corrector.replace(matcher.loc.expression, replacement_matcher)
- move_predicate(corrector, actual, matcher, block_child)
- corrector.replace(to_node.loc.selector, 'to')
- end
- end
-
def move_predicate(corrector, actual, matcher, block_child)
predicate = to_predicate_method(matcher.method_name)
args = args_loc(matcher).source
block_loc = block_loc(block_child)
block = block_loc ? block_loc.source : ''
@@ -259,11 +229,10 @@
when [false, false]
'be_falsey'
end
end
end
- # rubocop:enable Metrics/ModuleLength
# Prefer using predicate matcher over using predicate method directly.
#
# RSpec defines magic matchers for predicate methods.
# This cop recommends to use the predicate matcher instead of using
@@ -299,9 +268,10 @@
# expect(foo).to be_something
#
# # good - the above code is rewritten to it by this cop
# expect(foo.something?).to be_truthy
class PredicateMatcher < Cop
+ extend AutoCorrector
include ConfigurableEnforcedStyle
include InflectedHelper
include ExplicitHelper
def on_send(node)