forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
implicit_block_expectation.rb
64 lines (55 loc) · 1.77 KB
/
implicit_block_expectation.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
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Check that implicit block expectation syntax is not used.
#
# Prefer using explicit block expectations.
#
# @example
# # bad
# subject { -> { do_something } }
# it { is_expected.to change(something).to(new_value) }
#
# # good
# it 'changes something to a new value' do
# expect { do_something }.to change(something).to(new_value)
# end
class ImplicitBlockExpectation < Cop
MSG = 'Avoid implicit block expectations.'
def_node_matcher :lambda?, <<-PATTERN
{
(send (const nil? :Proc) :new)
(send nil? :proc)
(send nil? :lambda)
}
PATTERN
def_node_matcher :lambda_subject?, '(block #lambda? ...)'
def_node_matcher :implicit_expect, <<-PATTERN
$(send nil? {:is_expected :should :should_not} ...)
PATTERN
def on_send(node)
implicit_expect(node) do |implicit_expect|
subject = nearest_subject(implicit_expect)
add_offense(implicit_expect) if lambda_subject?(subject&.body)
end
end
private
def nearest_subject(node)
node
.each_ancestor(:block)
.lazy
.select { |block_node| multi_statement_example_group?(block_node) }
.map { |block_node| find_subject(block_node) }
.find(&:itself)
end
def multi_statement_example_group?(node)
example_group_with_body?(node) && node.body.begin_type?
end
def find_subject(block_node)
block_node.body.child_nodes.find { |send_node| subject?(send_node) }
end
end
end
end
end