-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
numeric_predicate.rb
157 lines (137 loc) · 4.54 KB
/
numeric_predicate.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# Checks for usage of comparison operators (`==`,
# `>`, `<`) to test numbers as zero, positive, or negative.
# These can be replaced by their respective predicate methods.
# This cop can also be configured to do the reverse.
#
# This cop can be customized ignored methods with `IgnoredMethods`.
# By default, there are no methods to ignored.
#
# This cop disregards `#nonzero?` as its value is truthy or falsey,
# but not `true` and `false`, and thus not always interchangeable with
# `!= 0`.
#
# This cop ignores comparisons to global variables, since they are often
# populated with objects which can be compared with integers, but are
# not themselves `Integer` polymorphic.
#
# @safety
# This cop is unsafe because it cannot be guaranteed that the receiver
# defines the predicates or can be compared to a number, which may lead
# to a false positive for non-standard classes.
#
# @example EnforcedStyle: predicate (default)
# # bad
# foo == 0
# 0 > foo
# bar.baz > 0
#
# # good
# foo.zero?
# foo.negative?
# bar.baz.positive?
#
# @example EnforcedStyle: comparison
# # bad
# foo.zero?
# foo.negative?
# bar.baz.positive?
#
# # good
# foo == 0
# 0 > foo
# bar.baz > 0
#
# @example IgnoredMethods: [] (default) with EnforcedStyle: predicate
# # bad
# foo == 0
# 0 > foo
# bar.baz > 0
#
# @example IgnoredMethods: [==] with EnforcedStyle: predicate
# # good
# foo == 0
#
# # bad
# 0 > foo
# bar.baz > 0
#
class NumericPredicate < Base
include ConfigurableEnforcedStyle
include IgnoredMethods
extend AutoCorrector
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
REPLACEMENTS = { 'zero?' => '==', 'positive?' => '>', 'negative?' => '<' }.freeze
RESTRICT_ON_SEND = %i[== > < positive? negative? zero?].freeze
def on_send(node)
numeric, replacement = check(node)
return unless numeric
return if ignored_method?(node.method_name) ||
node.each_ancestor(:send, :block).any? do |ancestor|
ignored_method?(ancestor.method_name)
end
message = format(MSG, prefer: replacement, current: node.source)
add_offense(node, message: message) do |corrector|
corrector.replace(node, replacement)
end
end
private
def check(node)
numeric, operator =
if style == :predicate
comparison(node) || inverted_comparison(node, &invert)
else
predicate(node)
end
return unless numeric && operator && replacement_supported?(operator)
[numeric, replacement(numeric, operator)]
end
def replacement(numeric, operation)
if style == :predicate
[parenthesized_source(numeric), REPLACEMENTS.invert[operation.to_s]].join('.')
else
[numeric.source, REPLACEMENTS[operation.to_s], 0].join(' ')
end
end
def parenthesized_source(node)
if require_parentheses?(node)
"(#{node.source})"
else
node.source
end
end
def require_parentheses?(node)
node.send_type? && node.binary_operation? && !node.parenthesized?
end
def replacement_supported?(operator)
if %i[> <].include?(operator)
target_ruby_version >= 2.3
else
true
end
end
def invert
lambda do |comparison, numeric|
comparison = { :> => :<, :< => :> }[comparison] || comparison
[numeric, comparison]
end
end
# @!method predicate(node)
def_node_matcher :predicate, <<~PATTERN
(send $(...) ${:zero? :positive? :negative?})
PATTERN
# @!method comparison(node)
def_node_matcher :comparison, <<~PATTERN
(send [$(...) !gvar_type?] ${:== :> :<} (int 0))
PATTERN
# @!method inverted_comparison(node)
def_node_matcher :inverted_comparison, <<~PATTERN
(send (int 0) ${:== :> :<} [$(...) !gvar_type?])
PATTERN
end
end
end
end