Class: RuboCop::Cop::RSpec::RedundantPredicateMatcher

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/redundant_predicate_matcher.rb

Overview

Checks for redundant predicate matcher.

Examples:

# bad
expect(foo).to be_exist(bar)
expect(foo).not_to be_include(bar)
expect(foo).to be_all(bar)

# good
expect(foo).to exist(bar)
expect(foo).not_to include(bar)
expect(foo).to all be(bar)

Constant Summary collapse

MSG =
'Use `%<good>s` instead of `%<bad>s`.'
RESTRICT_ON_SEND =
%i[be_all be_cover be_end_with be_eql be_equal
be_exist be_exists be_include be_match
be_respond_to be_start_with].freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

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

Instance Method Details

#on_send(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/rspec/redundant_predicate_matcher.rb', line 28

def on_send(node)
  return if node.parent.block_type? || node.arguments.empty?
  return unless replaceable_arguments?(node)

  method_name = node.method_name.to_s
  replaced = replaced_method_name(method_name)
  add_offense(node, message: message(method_name,
                                     replaced)) do |corrector|
    unless node.method?(:be_all)
      corrector.replace(node.loc.selector, replaced)
    end
  end
end