lib/rubocop/cop/rspec/receive_counts.rb in rubocop-rspec-1.41.0 vs lib/rubocop/cop/rspec/receive_counts.rb in rubocop-rspec-1.42.0
- old
+ new
@@ -22,10 +22,12 @@
# expect(foo).to receive(:bar).at_least(:twice)
# expect(foo).to receive(:bar).at_most(:once)
# expect(foo).to receive(:bar).at_most(:twice).times
#
class ReceiveCounts < Cop
+ extend AutoCorrector
+
MSG = 'Use `%<alternative>s` instead of `%<original>s`.'
def_node_matcher :receive_counts, <<-PATTERN
(send $(send _ {:exactly :at_least :at_most} (int {1 2})) :times)
PATTERN
@@ -36,30 +38,26 @@
receive_counts(node) do |offending_node|
return unless stub?(offending_node.receiver)
offending_range = range(node, offending_node)
- add_offense(
- offending_node,
- message: message_for(offending_node, offending_range.source),
- location: offending_range
- )
+ msg = message_for(offending_node, offending_range.source)
+ add_offense(offending_range, message: msg) do |corrector|
+ autocorrect(corrector, offending_node, offending_range)
+ end
end
end
- def autocorrect(node)
- lambda do |corrector|
- replacement = matcher_for(
- node.method_name,
- node.first_argument.source.to_i
- )
+ private
- original = range(node.parent, node)
- corrector.replace(original, replacement)
- end
- end
+ def autocorrect(corrector, node, range)
+ replacement = matcher_for(
+ node.method_name,
+ node.first_argument.source.to_i
+ )
- private
+ corrector.replace(range, replacement)
+ end
def message_for(node, source)
alternative = matcher_for(
node.method_name,
node.first_argument.source.to_i