forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunspecified_exception.rb
65 lines (58 loc) · 1.67 KB
/
unspecified_exception.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
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks for a specified error in checking raised errors.
#
# Enforces one of an Exception type, a string, or a regular
# expression to match against the exception message as a parameter
# to `raise_error`
#
# @example
# # bad
# expect {
# raise StandardError.new('error')
# }.to raise_error
#
# # good
# expect {
# raise StandardError.new('error')
# }.to raise_error(StandardError)
#
# expect {
# raise StandardError.new('error')
# }.to raise_error('error')
#
# expect {
# raise StandardError.new('error')
# }.to raise_error(/err/)
#
# expect { do_something }.not_to raise_error
#
class UnspecifiedException < Base
MSG = 'Specify the exception being captured'
RESTRICT_ON_SEND = %i[to].freeze
# @!method empty_raise_error_or_exception(node)
def_node_matcher :empty_raise_error_or_exception, <<-PATTERN
(send
(block
(send nil? :expect) ...)
:to
(send nil? {:raise_error :raise_exception})
)
PATTERN
def on_send(node)
return unless empty_exception_matcher?(node)
add_offense(node.children.last)
end
def empty_exception_matcher?(node)
empty_raise_error_or_exception(node) && !block_with_args?(node.parent)
end
def block_with_args?(node)
return unless node&.block_type?
node.arguments?
end
end
end
end
end