lib/rubocop/cop/rspec/predicate_matcher.rb in rubocop-rspec-1.32.0 vs lib/rubocop/cop/rspec/predicate_matcher.rb in rubocop-rspec-1.33.0
- old
+ new
@@ -1,14 +1,17 @@
+# frozen_string_literal: true
+
module RuboCop
module Cop
module RSpec
# A helper for `inflected` style
module InflectedHelper
+ include RuboCop::RSpec::Language
extend NodePattern::Macros
MSG_INFLECTED = 'Prefer using `%<matcher_name>s` matcher over ' \
- '`%<predicate_name>s`.'.freeze
+ '`%<predicate_name>s`.'
private
def check_inflected(node)
predicate_in_actual?(node) do |predicate|
@@ -23,11 +26,11 @@
def_node_matcher :predicate_in_actual?, <<-PATTERN
(send
(send nil? :expect {
(block $(send !nil? #predicate? ...) ...)
$(send !nil? #predicate? ...)})
- ${:to :not_to :to_not}
+ $#{Runners::ALL.node_pattern_union}
$#boolean_matcher?)
PATTERN
def_node_matcher :be_bool?, <<-PATTERN
(send nil? {:be :eq :eql :equal} {true false})
@@ -84,14 +87,14 @@
end
end
end
def remove_predicate(corrector, predicate)
- range = range_between(
- predicate.loc.dot.begin_pos,
- predicate.loc.expression.end_pos
+ range = predicate.loc.dot.with(
+ end_pos: predicate.loc.expression.end_pos
)
+
corrector.remove(range)
block_range = block_loc(predicate)
corrector.remove(block_range) if block_range
end
@@ -121,14 +124,15 @@
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 ' \
- '`%<matcher_name>s` matcher.'.freeze
+ '`%<matcher_name>s` matcher.'
BUILT_IN_MATCHERS = %w[
be_truthy be_falsey be_falsy
have_attributes have_received
be_between be_within
].freeze
@@ -158,20 +162,20 @@
end
def_node_matcher :predicate_matcher?, <<-PATTERN
(send
(send nil? :expect $!nil?)
- {:to :not_to :to_not}
+ #{Runners::ALL.node_pattern_union}
{$(send nil? #predicate_matcher_name? ...)
(block $(send nil? #predicate_matcher_name? ...) ...)})
PATTERN
def_node_matcher :predicate_matcher_block?, <<-PATTERN
(block
(send
(send nil? :expect $!nil?)
- {:to :not_to :to_not}
+ #{Runners::ALL.node_pattern_union}
$(send nil? #predicate_matcher_name?))
...)
PATTERN
def predicate_matcher_name?(name)
@@ -297,11 +301,10 @@
# expect(foo.something?).to be_truthy
class PredicateMatcher < Cop
include ConfigurableEnforcedStyle
include InflectedHelper
include ExplicitHelper
- include RangeHelp
def on_send(node)
case style
when :inflected
check_inflected(node)
@@ -328,24 +331,24 @@
# returns args location with whitespace
# @example
# foo 1, 2
# ^^^^^
def args_loc(send_node)
- range_between(send_node.loc.selector.end_pos,
- send_node.loc.expression.end_pos)
+ send_node.loc.selector.end.with(
+ end_pos: send_node.loc.expression.end_pos
+ )
end
# returns block location with whitespace
# @example
# foo { bar }
# ^^^^^^^^
def block_loc(send_node)
parent = send_node.parent
return unless parent.block_type?
- range_between(
- send_node.loc.expression.end_pos,
- parent.loc.expression.end_pos
+ send_node.loc.expression.end.with(
+ end_pos: parent.loc.expression.end_pos
)
end
end
end
end