lib/rubocop/cop/rspec/unspecified_exception.rb in rubocop-rspec-3.0.3 vs lib/rubocop/cop/rspec/unspecified_exception.rb in rubocop-rspec-3.0.4
- old
+ new
@@ -30,37 +30,42 @@
#
# 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})
- )
+ RESTRICT_ON_SEND = %i[
+ raise_exception
+ raise_error
+ ].freeze
+
+ # @!method expect_to?(node)
+ def_node_matcher :expect_to?, <<~PATTERN
+ (send (block (send nil? :expect) ...) :to ...)
PATTERN
def on_send(node)
return unless empty_exception_matcher?(node)
- add_offense(node.children.last)
+ add_offense(node)
end
private
def empty_exception_matcher?(node)
- empty_raise_error_or_exception(node) && !block_with_args?(node.parent)
- end
+ return false if node.arguments? || node.block_literal?
- def block_with_args?(node)
- return false unless node&.block_type?
+ expect_to = find_expect_to(node)
+ return false unless expect_to
+ return false if expect_to.block_node&.arguments?
- node.arguments?
+ true
+ end
+
+ def find_expect_to(node)
+ node.each_ancestor(:send).find do |ancestor|
+ expect_to?(ancestor)
+ end
end
end
end
end
end