forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
around_block.rb
72 lines (63 loc) · 1.68 KB
/
around_block.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
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks that around blocks actually run the test.
#
# @example
# # bad
# around do
# some_method
# end
#
# around do |test|
# some_method
# end
#
# # good
# around do |test|
# some_method
# test.call
# end
#
# around do |test|
# some_method
# test.run
# end
class AroundBlock < Cop
MSG_NO_ARG = 'Test object should be passed to around block.'
MSG_UNUSED_ARG = 'You should call `%<arg>s.call` '\
'or `%<arg>s.run`.'
def_node_matcher :hook, <<-PATTERN
(block {(send nil? :around) (send nil? :around sym)} (args $...) ...)
PATTERN
def_node_search :find_arg_usage, <<-PATTERN
{(send $... {:call :run}) (send _ _ $...) (yield $...) (block-pass $...)}
PATTERN
def on_block(node)
hook(node) do |(example_proxy)|
if example_proxy.nil?
add_no_arg_offense(node)
else
check_for_unused_proxy(node, example_proxy)
end
end
end
private
def add_no_arg_offense(node)
add_offense(node, message: MSG_NO_ARG)
end
def check_for_unused_proxy(block, proxy)
name, = *proxy
find_arg_usage(block) do |usage|
return if usage.include?(s(:lvar, name))
end
add_offense(
proxy,
message: format(MSG_UNUSED_ARG, arg: name)
)
end
end
end
end
end