lib/rubocop/cop/rspec/around_block.rb in rubocop-rspec-1.12.0 vs lib/rubocop/cop/rspec/around_block.rb in rubocop-rspec-1.13.0
- old
+ new
@@ -22,56 +22,45 @@
# around do |test|
# some_method
# test.run
# end
class AroundBlock < Cop
- MSG_NO_ARG = 'Test object should be passed to around block'.freeze
- MSG_UNUSED_ARG = 'You should call `%<arg>s.call` ' \
- 'or `%<arg>s.run`'.freeze
+ MSG_NO_ARG = 'Test object should be passed to around block.'.freeze
+ MSG_UNUSED_ARG = 'You should call `%<arg>s.call` '\
+ 'or `%<arg>s.run`.'.freeze
- def_node_matcher :scoped_hook, <<-PATTERN
- (block (send nil :around (sym {:each :example})) $(args ...) ...)
+ def_node_matcher :hook, <<-PATTERN
+ (block {(send nil :around) (send nil :around sym)} (args $...) ...)
PATTERN
- def_node_matcher :unscoped_hook, <<-PATTERN
- (block (send nil :around) $(args ...) ...)
+ def_node_search :find_arg_usage, <<-PATTERN
+ {(send $... {:call :run}) (send _ _ $...) (yield $...) (block-pass $...)}
PATTERN
- def_node_search :find_arg_usage, '(lvar $_)'
-
def on_block(node)
- hook(node) do |parameters|
- missing_parameters(parameters) do
- add_offense(node, :expression, MSG_NO_ARG)
- return
+ hook(node) do |(example_proxy)|
+ if example_proxy.nil?
+ add_no_arg_offense(node)
+ else
+ check_for_unused_proxy(node, example_proxy)
end
-
- unused_parameters(parameters) do |param, name|
- add_offense(param, :expression, format(MSG_UNUSED_ARG, arg: name))
- end
end
end
private
- def missing_parameters(node)
- yield if node.children[0].nil?
+ def add_no_arg_offense(node)
+ add_offense(node, :expression, MSG_NO_ARG)
end
- def unused_parameters(node)
- first_arg = node.children[0]
- param, _methods, _args = *first_arg
- start = node.parent
+ def check_for_unused_proxy(block, proxy)
+ name, = *proxy
- find_arg_usage(start) do |name|
- return if param == name
+ find_arg_usage(block) do |usage|
+ return if usage.include?(s(:lvar, name))
end
- yield first_arg, param
- end
-
- def hook(node, &block)
- scoped_hook(node, &block) || unscoped_hook(node, &block)
+ add_offense(proxy, :expression, format(MSG_UNUSED_ARG, arg: name))
end
end
end
end
end