lib/rubocop/cop/rspec/predicate_matcher.rb in rubocop-rspec-2.17.0 vs lib/rubocop/cop/rspec/predicate_matcher.rb in rubocop-rspec-2.17.1

- old
+ new

@@ -116,11 +116,11 @@ to_symbol == :to ? result : !result end end # A helper for `explicit` style - module ExplicitHelper + module ExplicitHelper # rubocop:disable Metrics/ModuleLength include RuboCop::RSpec::Language extend NodePattern::Macros MSG_EXPLICIT = 'Prefer using `%<predicate_name>s` over ' \ '`%<matcher_name>s` matcher.' @@ -147,16 +147,39 @@ end return if part_of_ignored_node?(node) predicate_matcher?(node) do |actual, matcher| + next unless replaceable_matcher?(matcher) + add_offense(node, message: message_explicit(matcher)) do |corrector| + next if uncorrectable_matcher?(node, matcher) + corrector_explicit(corrector, node, actual, matcher, matcher) end end end + def replaceable_matcher?(matcher) + case matcher.method_name.to_s + when 'include' + matcher.arguments.one? + else + true + end + end + + def uncorrectable_matcher?(node, matcher) + heredoc_argument?(matcher) && !same_line?(node, matcher) + end + + def heredoc_argument?(matcher) + matcher.arguments.select do |arg| + %i[str dstr xstr].include?(arg.type) + end.any?(&:heredoc?) + end + # @!method predicate_matcher?(node) def_node_matcher :predicate_matcher?, <<-PATTERN (send (send nil? :expect $!nil?) #Runners.all @@ -268,9 +291,20 @@ # # bad # expect(foo).to be_something # # # good - the above code is rewritten to it by this cop # expect(foo.something?).to be(true) + # + # # bad - no autocorrect + # expect(foo) + # .to be_something(<<~TEXT) + # bar + # TEXT + # + # # good + # expect(foo.something?(<<~TEXT)).to be(true) + # bar + # TEXT # # @example Strict: false, EnforcedStyle: explicit # # bad # expect(foo).to be_something #