class RuboCop::Cop::RSpec::RedundantPredicateMatcher

Checks for redundant predicate matcher.

@example

# 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)

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# 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

Private Instance Methods

message(bad_method, good_method) click to toggle source
# File lib/rubocop/cop/rspec/redundant_predicate_matcher.rb, line 44
def message(bad_method, good_method)
  format(MSG, bad: bad_method, good: good_method)
end
replaceable_arguments?(node) click to toggle source
# File lib/rubocop/cop/rspec/redundant_predicate_matcher.rb, line 48
def replaceable_arguments?(node)
  if node.method?(:be_all)
    node.first_argument.send_type?
  else
    true
  end
end
replaced_method_name(method_name) click to toggle source
# File lib/rubocop/cop/rspec/redundant_predicate_matcher.rb, line 56
def replaced_method_name(method_name)
  name = method_name.to_s.delete_prefix('be_')
  if name == 'exists'
    'exist'
  else
    name
  end
end