File tree Expand file tree Collapse file tree 3 files changed +49
-15
lines changed Expand file tree Collapse file tree 3 files changed +49
-15
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## Master (Unreleased)
4
4
5
+ - Fix false-negative for ` UnspecifiedException ` when matcher is chained. ([ @r7kamura ] )
6
+
5
7
## 3.0.3 (2024-07-12)
6
8
7
9
- Add support for Unicode RIGHT SINGLE QUOTATION MARK in ` RSpec/ExampleWording ` . ([ @jdufresne ] )
Original file line number Diff line number Diff line change @@ -32,34 +32,39 @@ module RSpec
32
32
#
33
33
class UnspecifiedException < Base
34
34
MSG = 'Specify the exception being captured'
35
- RESTRICT_ON_SEND = %i[ to ] . freeze
36
35
37
- # @!method empty_raise_error_or_exception(node)
38
- def_node_matcher :empty_raise_error_or_exception , <<~PATTERN
39
- (send
40
- (block
41
- (send nil? :expect) ...)
42
- :to
43
- (send nil? {:raise_error :raise_exception})
44
- )
36
+ RESTRICT_ON_SEND = %i[
37
+ raise_exception
38
+ raise_error
39
+ ] . freeze
40
+
41
+ # @!method expect_to?(node)
42
+ def_node_matcher :expect_to? , <<~PATTERN
43
+ (send (block (send nil? :expect) ...) :to ... )
45
44
PATTERN
46
45
47
46
def on_send ( node )
48
47
return unless empty_exception_matcher? ( node )
49
48
50
- add_offense ( node . children . last )
49
+ add_offense ( node )
51
50
end
52
51
53
52
private
54
53
55
54
def empty_exception_matcher? ( node )
56
- empty_raise_error_or_exception ( node ) && !block_with_args? ( node . parent )
57
- end
55
+ return false if node . arguments? || node . block_literal?
58
56
59
- def block_with_args? ( node )
60
- return false unless node &.block_type?
57
+ expect_to = find_expect_to ( node )
58
+ return false unless expect_to
59
+ return false if expect_to . block_node &.arguments?
60
+
61
+ true
62
+ end
61
63
62
- node . arguments?
64
+ def find_expect_to ( node )
65
+ node . each_ancestor ( :send ) . find do |ancestor |
66
+ expect_to? ( ancestor )
67
+ end
63
68
end
64
69
end
65
70
end
Original file line number Diff line number Diff line change 171
171
}.to raise_exception(my_exception)
172
172
RUBY
173
173
end
174
+
175
+ it 'detects chained offenses' do
176
+ expect_offense ( <<~RUBY )
177
+ expect {
178
+ foo
179
+ }.to raise_exception.and change { bar }
180
+ ^^^^^^^^^^^^^^^ Specify the exception being captured
181
+ RUBY
182
+ end
183
+
184
+ it 'detects more chained offenses' do
185
+ expect_offense ( <<~RUBY )
186
+ expect {
187
+ foo
188
+ }.to raise_exception.and change { bar }.and change { baz }
189
+ ^^^^^^^^^^^^^^^ Specify the exception being captured
190
+ RUBY
191
+ end
192
+
193
+ it 'detects more complex chained offenses' do
194
+ expect_offense ( <<~RUBY )
195
+ expect {
196
+ foo
197
+ }.to change { bar }.and raise_exception.and change { baz }
198
+ ^^^^^^^^^^^^^^^ Specify the exception being captured
199
+ RUBY
200
+ end
174
201
end
175
202
end
You can’t perform that action at this time.
0 commit comments