/
be_eql.rb
57 lines (53 loc) · 1.77 KB
/
be_eql.rb
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
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Check for expectations where `be(...)` can replace `eql(...)`.
#
# The `be` matcher compares by identity while the `eql` matcher
# compares using `eql?`. Integers, floats, booleans, symbols, and nil
# can be compared by identity and therefore the `be` matcher is
# preferable as it is a more strict test.
#
# @example
#
# # bad
# expect(foo).to eql(1)
# expect(foo).to eql(1.0)
# expect(foo).to eql(true)
# expect(foo).to eql(false)
# expect(foo).to eql(:bar)
# expect(foo).to eql(nil)
#
# # good
# expect(foo).to be(1)
# expect(foo).to be(1.0)
# expect(foo).to be(true)
# expect(foo).to be(false)
# expect(foo).to be(:bar)
# expect(foo).to be(nil)
#
# This cop only looks for instances of `expect(...).to eql(...)`. We
# do not check `to_not` or `not_to` since `!eql?` is more strict
# than `!equal?`. We also do not try to flag `eq` because if
# `a == b`, and `b` is comparable by identity, `a` is still not
# necessarily the same type as `b` since the `#==` operator can
# coerce objects for comparison.
#
class BeEql < Cop
extend AutoCorrector
MSG = 'Prefer `be` over `eql`.'
def_node_matcher :eql_type_with_identity, <<-PATTERN
(send _ :to $(send nil? :eql {true false int float sym nil_type?}))
PATTERN
def on_send(node)
eql_type_with_identity(node) do |eql|
add_offense(eql.loc.selector) do |corrector|
corrector.replace(eql.loc.selector, 'be')
end
end
end
end
end
end
end