lib/rubocop/cop/rspec/yield.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/yield.rb in rubocop-rspec-1.42.0
- old
+ new
@@ -10,10 +10,11 @@
# allow(foo).to receive(:bar) { |&block| block.call(1) }
#
# # good
# expect(foo).to be(:bar).and_yield(1)
class Yield < Cop
+ extend AutoCorrector
include RangeHelp
MSG = 'Use `.and_yield`.'
def_node_search :method_on_stub?, '(send nil? :receive ...)'
@@ -25,24 +26,26 @@
def on_block(node)
return unless method_on_stub?(node.send_node)
block_arg(node.arguments) do |block|
if calling_block?(node.body, block)
- add_offense(node, location: block_range(node))
+ range = block_range(node)
+
+ add_offense(range) do |corrector|
+ autocorrect(corrector, node, range)
+ end
end
end
end
- def autocorrect(node)
- lambda do |corrector|
- node_range = range_with_surrounding_space(
- range: block_range(node), side: :left
- )
- corrector.replace(node_range, generate_replacement(node.body))
- end
- end
-
private
+
+ def autocorrect(corrector, node, range)
+ corrector.replace(
+ range_with_surrounding_space(range: range, side: :left),
+ generate_replacement(node.body)
+ )
+ end
def calling_block?(node, block)
if node.begin_type?
node.each_child_node.all? { |child| block_call?(child, block) }
else