lib/rubocop/cop/rspec/around_block.rb in rubocop-rspec-2.12.1 vs lib/rubocop/cop/rspec/around_block.rb in rubocop-rspec-2.13.0

- old
+ new

@@ -23,35 +23,47 @@ # # around do |test| # some_method # test.run # end + # class AroundBlock < Base MSG_NO_ARG = 'Test object should be passed to around block.' MSG_UNUSED_ARG = 'You should call `%<arg>s.call` ' \ 'or `%<arg>s.run`.' - # @!method hook(node) - def_node_matcher :hook, <<-PATTERN + # @!method hook_block(node) + def_node_matcher :hook_block, <<-PATTERN (block (send nil? :around sym ?) (args $...) ...) PATTERN + # @!method hook_numblock(node) + def_node_matcher :hook_numblock, <<-PATTERN + (numblock (send nil? :around sym ?) ...) + PATTERN + # @!method find_arg_usage(node) def_node_search :find_arg_usage, <<-PATTERN {(send $... {:call :run}) (send _ _ $...) (yield $...) (block-pass $...)} PATTERN def on_block(node) - hook(node) do |(example_proxy)| + hook_block(node) do |(example_proxy)| if example_proxy.nil? add_no_arg_offense(node) else check_for_unused_proxy(node, example_proxy) end end end + def on_numblock(node) + hook_numblock(node) do + check_for_numblock(node) + end + end + private def add_no_arg_offense(node) add_offense(node, message: MSG_NO_ARG) end @@ -64,9 +76,20 @@ end add_offense( proxy, message: format(MSG_UNUSED_ARG, arg: name) + ) + end + + def check_for_numblock(block) + find_arg_usage(block) do |usage| + return if usage.include?(s(:lvar, :_1)) + end + + add_offense( + block.children.last, + message: format(MSG_UNUSED_ARG, arg: :_1) ) end end end end