forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredicate_matcher.rb
341 lines (297 loc) · 10.5 KB
/
predicate_matcher.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# A helper for `inflected` style
module InflectedHelper
include RuboCop::RSpec::Language
extend NodePattern::Macros
MSG_INFLECTED = 'Prefer using `%<matcher_name>s` matcher over ' \
'`%<predicate_name>s`.'
private
def check_inflected(node)
predicate_in_actual?(node) do |predicate, to, matcher|
msg = message_inflected(predicate)
add_offense(node, message: msg) do |corrector|
remove_predicate(corrector, predicate)
corrector.replace(node.loc.selector,
true?(to, matcher) ? 'to' : 'not_to')
rewrite_matcher(corrector, predicate, matcher)
end
end
end
# @!method predicate_in_actual?(node)
def_node_matcher :predicate_in_actual?, <<~PATTERN
(send
(send nil? :expect {
(block $(send !nil? #predicate? ...) ...)
$(send !nil? #predicate? ...)})
$#Runners.all
$#boolean_matcher? ...)
PATTERN
# @!method be_bool?(node)
def_node_matcher :be_bool?, <<~PATTERN
(send nil? {:be :eq :eql :equal} {true false})
PATTERN
# @!method be_boolthy?(node)
def_node_matcher :be_boolthy?, <<~PATTERN
(send nil? {:be_truthy :be_falsey :be_falsy :a_truthy_value :a_falsey_value :a_falsy_value})
PATTERN
def boolean_matcher?(node)
if cop_config['Strict']
be_boolthy?(node)
else
be_bool?(node) || be_boolthy?(node)
end
end
def predicate?(sym)
sym.to_s.end_with?('?')
end
def message_inflected(predicate)
format(MSG_INFLECTED,
predicate_name: predicate.method_name,
matcher_name: to_predicate_matcher(predicate.method_name))
end
# rubocop:disable Metrics/MethodLength
def to_predicate_matcher(name)
case name = name.to_s
when 'is_a?'
'be_a'
when 'instance_of?'
'be_an_instance_of'
when 'include?', 'respond_to?'
name[0..-2]
when 'exist?', 'exists?'
'exist'
when /\Ahas_/
name.sub('has_', 'have_')[0..-2]
else
"be_#{name[0..-2]}"
end
end
# rubocop:enable Metrics/MethodLength
def remove_predicate(corrector, predicate)
range = predicate.loc.dot.with(
end_pos: predicate.source_range.end_pos
)
corrector.remove(range)
block_range = LocationHelp.block_with_whitespace(predicate)
corrector.remove(block_range) if block_range
end
def rewrite_matcher(corrector, predicate, matcher)
args = LocationHelp.arguments_with_whitespace(predicate).source
block_loc = LocationHelp.block_with_whitespace(predicate)
block = block_loc ? block_loc.source : ''
corrector.replace(
matcher,
to_predicate_matcher(predicate.method_name) + args + block
)
end
def true?(to_symbol, matcher)
result = case matcher.method_name
when :be, :eq
matcher.first_argument.true_type?
when :be_truthy, :a_truthy_value
true
when :be_falsey, :be_falsy, :a_falsey_value, :a_falsy_value
false
end
to_symbol == :to ? result : !result
end
end
# A helper for `explicit` style
module ExplicitHelper # rubocop:disable Metrics/ModuleLength
include RuboCop::RSpec::Language
extend NodePattern::Macros
MSG_EXPLICIT = 'Prefer using `%<predicate_name>s` over ' \
'`%<matcher_name>s` matcher.'
BUILT_IN_MATCHERS = %w[
be_truthy be_falsey be_falsy
have_attributes have_received
be_between be_within
].freeze
private
def allowed_explicit_matchers
cop_config.fetch('AllowedExplicitMatchers', []) + BUILT_IN_MATCHERS
end
def check_explicit(node) # rubocop:disable Metrics/MethodLength
predicate_matcher_block?(node) do |actual, matcher|
add_offense(node, message: message_explicit(matcher)) do |corrector|
to_node = node.send_node
corrector_explicit(corrector, to_node, actual, matcher, to_node)
end
ignore_node(node.children.first)
return
end
return if part_of_ignored_node?(node)
predicate_matcher?(node) do |actual, matcher|
next unless replaceable_matcher?(matcher)
add_offense(node, message: message_explicit(matcher)) do |corrector|
next if uncorrectable_matcher?(node, matcher)
corrector_explicit(corrector, node, actual, matcher, matcher)
end
end
end
def replaceable_matcher?(matcher)
case matcher.method_name.to_s
when 'include'
matcher.arguments.one?
else
true
end
end
def uncorrectable_matcher?(node, matcher)
heredoc_argument?(matcher) && !same_line?(node, matcher)
end
def heredoc_argument?(matcher)
matcher.arguments.select do |arg|
arg.str_type? || arg.dstr_type? || arg.xstr_type?
end.any?(&:heredoc?)
end
# @!method predicate_matcher?(node)
def_node_matcher :predicate_matcher?, <<~PATTERN
(send
(send nil? :expect $!nil?)
#Runners.all
{
$(send nil? #predicate_matcher_name? ...)
(block $(send nil? #predicate_matcher_name? ...) ...)
}
...
)
PATTERN
# @!method predicate_matcher_block?(node)
def_node_matcher :predicate_matcher_block?, <<~PATTERN
(block
(send
(send nil? :expect $!nil?)
#Runners.all
$(send nil? #predicate_matcher_name?))
...)
PATTERN
def predicate_matcher_name?(name)
name = name.to_s
return false if allowed_explicit_matchers.include?(name)
(name.start_with?('be_', 'have_') && !name.end_with?('?')) ||
%w[include respond_to].include?(name)
end
def message_explicit(matcher)
format(MSG_EXPLICIT,
predicate_name: to_predicate_method(matcher.method_name),
matcher_name: matcher.method_name)
end
def corrector_explicit(corrector, to_node, actual, matcher, block_child)
replacement_matcher = replacement_matcher(to_node)
corrector.replace(matcher, replacement_matcher)
move_predicate(corrector, actual, matcher, block_child)
corrector.replace(to_node.loc.selector, 'to')
end
def move_predicate(corrector, actual, matcher, block_child)
predicate = to_predicate_method(matcher.method_name)
args = LocationHelp.arguments_with_whitespace(matcher).source
block_loc = LocationHelp.block_with_whitespace(block_child)
block = block_loc ? block_loc.source : ''
corrector.remove(block_loc) if block_loc
corrector.insert_after(actual, ".#{predicate}" + args + block)
end
# rubocop:disable Metrics/MethodLength
def to_predicate_method(matcher)
case matcher = matcher.to_s
when 'be_a', 'be_an', 'be_a_kind_of', 'a_kind_of', 'be_kind_of'
'is_a?'
when 'be_an_instance_of', 'be_instance_of', 'an_instance_of'
'instance_of?'
when 'include'
'include?'
when 'respond_to'
'respond_to?'
when /\Ahave_(.+)/
"has_#{Regexp.last_match(1)}?"
else
"#{matcher[/\Abe_(.+)/, 1]}?"
end
end
# rubocop:enable Metrics/MethodLength
def replacement_matcher(node)
case [cop_config['Strict'], node.method?(:to)]
when [true, true]
'be(true)'
when [true, false]
'be(false)'
when [false, true]
'be_truthy'
when [false, false]
'be_falsey'
end
end
end
# Prefer using predicate matcher over using predicate method directly.
#
# RSpec defines magic matchers for predicate methods.
# This cop recommends to use the predicate matcher instead of using
# predicate method directly.
#
# @example Strict: true, EnforcedStyle: inflected (default)
# # bad
# expect(foo.something?).to be_truthy
#
# # good
# expect(foo).to be_something
#
# # also good - It checks "true" strictly.
# expect(foo.something?).to be(true)
#
# @example Strict: false, EnforcedStyle: inflected
# # bad
# expect(foo.something?).to be_truthy
# expect(foo.something?).to be(true)
#
# # good
# expect(foo).to be_something
#
# @example Strict: true, EnforcedStyle: explicit
# # bad
# expect(foo).to be_something
#
# # good - the above code is rewritten to it by this cop
# expect(foo.something?).to be(true)
#
# # bad - no autocorrect
# expect(foo)
# .to be_something(<<~TEXT)
# bar
# TEXT
#
# # good
# expect(foo.something?(<<~TEXT)).to be(true)
# bar
# TEXT
#
# @example Strict: false, EnforcedStyle: explicit
# # bad
# expect(foo).to be_something
#
# # good - the above code is rewritten to it by this cop
# expect(foo.something?).to be_truthy
#
class PredicateMatcher < Base
extend AutoCorrector
include ConfigurableEnforcedStyle
include InflectedHelper
include ExplicitHelper
RESTRICT_ON_SEND = Runners.all
def on_send(node)
case style
when :inflected
check_inflected(node)
when :explicit
check_explicit(node)
end
end
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
check_explicit(node) if style == :explicit
end
end
end
end
end