1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Check for expectations where `be(...)` can replace `eq(...)`.
#
# The `be` matcher compares by identity while the `eq` matcher compares
# using `==`. Booleans and nil can be compared by identity and therefore
# the `be` matcher is preferable as it is a more strict test.
#
# @safety
# This cop is unsafe because it changes how values are compared.
#
# @example
# # bad
# expect(foo).to eq(true)
# expect(foo).to eq(false)
# expect(foo).to eq(nil)
#
# # good
# expect(foo).to be(true)
# expect(foo).to be(false)
# expect(foo).to be(nil)
#
class BeEq < Base
extend AutoCorrector
MSG = 'Prefer `be` over `eq`.'
RESTRICT_ON_SEND = %i[eq].freeze
# @!method eq_type_with_identity?(node)
def_node_matcher :eq_type_with_identity?, <<-PATTERN
(send nil? :eq {true false nil})
PATTERN
def on_send(node)
return unless eq_type_with_identity?(node)
add_offense(node.loc.selector) do |corrector|
corrector.replace(node.loc.selector, 'be')
end
end
end
end
end
end
|