forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplicit_subject.rb
167 lines (148 loc) · 4.25 KB
/
implicit_subject.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
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks for usage of implicit subject (`is_expected` / `should`).
#
# This cop can be configured using the `EnforcedStyle` option
#
# @example `EnforcedStyle: single_line_only` (default)
# # bad
# it do
# is_expected.to be_truthy
# end
#
# # good
# it { is_expected.to be_truthy }
# it do
# expect(subject).to be_truthy
# end
#
# @example `EnforcedStyle: single_statement_only`
# # bad
# it do
# foo = 1
# is_expected.to be_truthy
# end
#
# # good
# it do
# foo = 1
# expect(subject).to be_truthy
# end
# it do
# is_expected.to be_truthy
# end
#
# @example `EnforcedStyle: disallow`
# # bad
# it { is_expected.to be_truthy }
#
# # good
# it { expect(subject).to be_truthy }
#
# @example `EnforcedStyle: require_implicit`
# # bad
# it { expect(subject).to be_truthy }
#
# # good
# it { is_expected.to be_truthy }
#
# # bad
# it do
# expect(subject).to be_truthy
# end
#
# # good
# it do
# is_expected.to be_truthy
# end
#
# # good
# it { expect(named_subject).to be_truthy }
#
class ImplicitSubject < Base
extend AutoCorrector
include ConfigurableEnforcedStyle
MSG_REQUIRE_EXPLICIT = "Don't use implicit subject."
MSG_REQUIRE_IMPLICIT = "Don't use explicit subject."
RESTRICT_ON_SEND = %i[
expect
is_expected
should
should_not
].freeze
# @!method explicit_unnamed_subject?(node)
def_node_matcher :explicit_unnamed_subject?, <<-PATTERN
(send nil? :expect (send nil? :subject))
PATTERN
# @!method implicit_subject?(node)
def_node_matcher :implicit_subject?, <<-PATTERN
(send nil? {:should :should_not :is_expected} ...)
PATTERN
def on_send(node)
return unless invalid?(node)
add_offense(node) do |corrector|
autocorrect(corrector, node)
end
end
private
def autocorrect(corrector, node)
case node.method_name
when :expect
corrector.replace(node, 'is_expected')
when :is_expected
corrector.replace(node.location.selector, 'expect(subject)')
when :should
corrector.replace(node.location.selector, 'expect(subject).to')
when :should_not
corrector.replace(node.location.selector, 'expect(subject).not_to')
end
end
def message(_node)
case style
when :require_implicit
MSG_REQUIRE_IMPLICIT
else
MSG_REQUIRE_EXPLICIT
end
end
def invalid?(node)
case style
when :require_implicit
explicit_unnamed_subject?(node)
when :disallow
implicit_subject_in_non_its?(node)
when :single_line_only
implicit_subject_in_non_its_and_non_single_line?(node)
when :single_statement_only
implicit_subject_in_non_its_and_non_single_statement?(node)
end
end
def implicit_subject_in_non_its?(node)
implicit_subject?(node) && !its?(node)
end
def implicit_subject_in_non_its_and_non_single_line?(node)
implicit_subject_in_non_its?(node) && !single_line?(node)
end
def implicit_subject_in_non_its_and_non_single_statement?(node)
implicit_subject_in_non_its?(node) && !single_statement?(node)
end
def its?(node)
example_of(node)&.method?(:its)
end
def single_line?(node)
example_of(node)&.single_line?
end
def single_statement?(node)
!example_of(node)&.body&.begin_type?
end
def example_of(node)
node.each_ancestor.find do |ancestor|
example?(ancestor)
end
end
end
end
end
end