forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect_in_hook.rb
61 lines (54 loc) · 1.42 KB
/
expect_in_hook.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
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Do not use `expect` in hooks such as `before`.
#
# @example
# # bad
# before do
# expect(something).to eq 'foo'
# end
#
# # bad
# after do
# expect_any_instance_of(Something).to receive(:foo)
# end
#
# # good
# it do
# expect(something).to eq 'foo'
# end
class ExpectInHook < Cop
MSG = 'Do not use `%<expect>s` in `%<hook>s` hook'.freeze
HOOKS = Hooks::ALL.node_pattern_union.freeze
def_node_matcher :hook, <<-PATTERN
(block (send _ $#{HOOKS} ...) _ $!nil)
PATTERN
def_node_search :expect, <<-PATTERN
{
#{Expectations::ALL.send_pattern}
#{Expectations::ALL.block_pattern}
}
PATTERN
def on_block(node)
hook(node) do |hook_name, body|
expect(body) do |expect|
method = send_node(expect)
add_offense(method, :selector,
message(method, hook_name))
end
end
end
private
def message(expect, hook)
format(MSG, expect: expect.method_name, hook: hook)
end
def send_node(node)
return node if node.send_type?
node.children.first
end
end
end
end
end