/
case_equality.rb
53 lines (48 loc) · 1.2 KB
/
case_equality.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
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks for uses of the case equality operator(===).
#
# @example
# # bad
# Array === something
# (1..100) === 7
# /something/ === some_string
#
# # good
# something.is_a?(Array)
# (1..100).include?(7)
# some_string =~ /something/
#
# @example AllowOnConstant
# # Style/CaseEquality:
# # AllowOnConstant: true
#
# # bad
# (1..100) === 7
# /something/ === some_string
#
# # good
# Array === something
# (1..100).include?(7)
# some_string =~ /something/
#
class CaseEquality < Cop
MSG = 'Avoid the use of the case equality operator `===`.'
def_node_matcher :case_equality?, '(send #const? :=== _)'
def on_send(node)
case_equality?(node) { add_offense(node, location: :selector) }
end
private
def const?(node)
if cop_config.fetch('AllowOnConstant', false)
!node.const_type?
else
true
end
end
end
end
end
end