-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexp_match.rb
284 lines (243 loc) · 8.02 KB
/
regexp_match.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# In Ruby 2.4, `String#match?`, `Regexp#match?`, and `Symbol#match?`
# have been added. The methods are faster than `match`.
# Because the methods avoid creating a `MatchData` object or saving
# backref.
# So, when `MatchData` is not used, use `match?` instead of `match`.
#
# @example
# # bad
# def foo
# if x =~ /re/
# do_something
# end
# end
#
# # bad
# def foo
# if x !~ /re/
# do_something
# end
# end
#
# # bad
# def foo
# if x.match(/re/)
# do_something
# end
# end
#
# # bad
# def foo
# if /re/ === x
# do_something
# end
# end
#
# # good
# def foo
# if x.match?(/re/)
# do_something
# end
# end
#
# # good
# def foo
# if !x.match?(/re/)
# do_something
# end
# end
#
# # good
# def foo
# if x =~ /re/
# do_something(Regexp.last_match)
# end
# end
#
# # good
# def foo
# if x.match(/re/)
# do_something($~)
# end
# end
#
# # good
# def foo
# if /re/ === x
# do_something($~)
# end
# end
class RegexpMatch < Base
extend AutoCorrector
extend TargetRubyVersion
minimum_target_ruby_version 2.4
# Constants are included in this list because it is unlikely that
# someone will store `nil` as a constant and then use it for comparison
TYPES_IMPLEMENTING_MATCH = %i[const regexp str sym].freeze
MSG = 'Use `match?` instead of `%<current>s` when `MatchData` is not used.'
def_node_matcher :match_method?, <<~PATTERN
{
(send _recv :match {regexp str sym})
(send {regexp str sym} :match _)
}
PATTERN
def_node_matcher :match_with_int_arg_method?, <<~PATTERN
(send _recv :match _ (int ...))
PATTERN
def_node_matcher :match_operator?, <<~PATTERN
(send !nil? {:=~ :!~} !nil?)
PATTERN
def_node_matcher :match_threequals?, <<~PATTERN
(send (regexp (str _) {(regopt) (regopt _)}) :=== !nil?)
PATTERN
def match_with_lvasgn?(node)
return false unless node.match_with_lvasgn_type?
regexp, _rhs = *node
regexp.to_regexp.named_captures.empty?
end
MATCH_NODE_PATTERN = <<~PATTERN
{
#match_method?
#match_with_int_arg_method?
#match_operator?
#match_threequals?
#match_with_lvasgn?
}
PATTERN
def_node_matcher :match_node?, MATCH_NODE_PATTERN
def_node_search :search_match_nodes, MATCH_NODE_PATTERN
def_node_search :last_matches, <<~PATTERN
{
(send (const {nil? cbase} :Regexp) :last_match)
(send (const {nil? cbase} :Regexp) :last_match _)
({back_ref nth_ref} _)
(gvar #match_gvar?)
}
PATTERN
def self.autocorrect_incompatible_with
[ConstantRegexp]
end
def on_if(node)
check_condition(node.condition)
end
def on_case(node)
return if node.condition
node.each_when do |when_node|
when_node.each_condition do |condition|
check_condition(condition)
end
end
end
private
def check_condition(cond)
match_node?(cond) do
return if last_match_used?(cond)
message = message(cond)
add_offense(cond, message: message) do |corrector|
autocorrect(corrector, cond)
end
end
end
def autocorrect(corrector, node)
if match_method?(node) || match_with_int_arg_method?(node)
corrector.replace(node.loc.selector, 'match?')
elsif match_operator?(node) || match_threequals?(node)
recv, oper, arg = *node
correct_operator(corrector, recv, arg, oper)
elsif match_with_lvasgn?(node)
recv, arg = *node
correct_operator(corrector, recv, arg)
end
end
def message(node)
format(MSG, current: node.loc.selector.source)
end
def last_match_used?(match_node)
scope_root = scope_root(match_node)
body = scope_root ? scope_body(scope_root) : match_node.ancestors.last
range = range_to_search_for_last_matches(match_node, body, scope_root)
find_last_match(body, range, scope_root)
end
def range_to_search_for_last_matches(match_node, body, scope_root)
expression = if modifier_form?(match_node)
match_node.parent.if_branch.source_range
else
match_node.source_range
end
match_node_pos = expression.begin_pos
next_match_pos = next_match_pos(body, match_node_pos, scope_root)
match_node_pos..next_match_pos
end
def next_match_pos(body, match_node_pos, scope_root)
node = search_match_nodes(body).find do |match|
begin_pos = if modifier_form?(match)
match.parent.if_branch.source_range.begin_pos
else
match.source_range.begin_pos
end
begin_pos > match_node_pos && scope_root(match) == scope_root
end
node ? node.source_range.begin_pos : Float::INFINITY
end
def modifier_form?(match_node)
match_node.parent.if_type? && match_node.parent.modifier_form?
end
def find_last_match(body, range, scope_root)
last_matches(body).find do |ref|
ref_pos = ref.source_range.begin_pos
range.cover?(ref_pos) && scope_root(ref) == scope_root
end
end
def scope_body(node)
children = node.children
case node.type
when :module
children[1]
when :defs
children[3]
else
children[2]
end
end
def scope_root(node)
node.each_ancestor.find do |ancestor|
ancestor.def_type? || ancestor.defs_type? || ancestor.class_type? || ancestor.module_type?
end
end
def match_gvar?(sym)
%i[$~ $MATCH $PREMATCH $POSTMATCH $LAST_PAREN_MATCH $LAST_MATCH_INFO].include?(sym)
end
def correct_operator(corrector, recv, arg, oper = nil)
op_range = correction_range(recv, arg)
replace_with_match_predicate_method(corrector, recv, arg, op_range)
corrector.insert_after(arg, ')') unless op_range.source.end_with?('(')
corrector.insert_before(recv, '!') if oper == :!~
end
def replace_with_match_predicate_method(corrector, recv, arg, op_range)
if TYPES_IMPLEMENTING_MATCH.include?(recv.type)
corrector.replace(op_range, '.match?(')
elsif TYPES_IMPLEMENTING_MATCH.include?(arg.type)
corrector.replace(op_range, '.match?(')
swap_receiver_and_arg(corrector, recv, arg)
else
corrector.replace(op_range, '&.match?(')
end
end
def swap_receiver_and_arg(corrector, recv, arg)
corrector.replace(recv, arg.source)
corrector.replace(arg, recv.source)
end
def correction_range(recv, arg)
buffer = processed_source.buffer
op_begin_pos = recv.source_range.end_pos
op_end_pos = arg.source_range.begin_pos
Parser::Source::Range.new(buffer, op_begin_pos, op_end_pos)
end
end
end
end
end