2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
require 'rubocop-rspec'
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module RSpec
|
|
|
|
# This cop checks for `allow_any_instance_of` or `expect_any_instance_of`
|
|
|
|
# usage in specs.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
#
|
|
|
|
# # bad
|
|
|
|
# allow_any_instance_of(User).to receive(:invalidate_issue_cache_counts)
|
|
|
|
#
|
|
|
|
# # bad
|
|
|
|
# expect_any_instance_of(User).to receive(:invalidate_issue_cache_counts)
|
|
|
|
#
|
|
|
|
# # good
|
|
|
|
# allow_next_instance_of(User) do |instance|
|
|
|
|
# allow(instance).to receive(:invalidate_issue_cache_counts)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # good
|
|
|
|
# expect_next_instance_of(User) do |instance|
|
|
|
|
# expect(instance).to receive(:invalidate_issue_cache_counts)
|
|
|
|
# end
|
|
|
|
#
|
2022-10-11 01:57:18 +05:30
|
|
|
class AnyInstanceOf < RuboCop::Cop::Base
|
|
|
|
extend RuboCop::Cop::AutoCorrector
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
MESSAGE_EXPECT = 'Do not use `expect_any_instance_of` method, use `expect_next_instance_of` instead.'
|
|
|
|
MESSAGE_ALLOW = 'Do not use `allow_any_instance_of` method, use `allow_next_instance_of` instead.'
|
|
|
|
|
|
|
|
def_node_search :expect_any_instance_of?, <<~PATTERN
|
|
|
|
(send (send nil? :expect_any_instance_of ...) ...)
|
|
|
|
PATTERN
|
|
|
|
def_node_search :allow_any_instance_of?, <<~PATTERN
|
|
|
|
(send (send nil? :allow_any_instance_of ...) ...)
|
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
if expect_any_instance_of?(node)
|
2022-10-11 01:57:18 +05:30
|
|
|
add_offense(node, message: MESSAGE_EXPECT) do |corrector|
|
|
|
|
corrector.replace(
|
|
|
|
node.loc.expression,
|
|
|
|
replacement_any_instance_of(node, 'expect')
|
|
|
|
)
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
elsif allow_any_instance_of?(node)
|
2022-10-11 01:57:18 +05:30
|
|
|
add_offense(node, message: MESSAGE_ALLOW) do |corrector|
|
|
|
|
corrector.replace(
|
|
|
|
node.loc.expression,
|
|
|
|
replacement_any_instance_of(node, 'allow')
|
|
|
|
)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def replacement_any_instance_of(node, rspec_prefix)
|
|
|
|
method_call =
|
|
|
|
node.receiver.source.sub(
|
|
|
|
"#{rspec_prefix}_any_instance_of",
|
|
|
|
"#{rspec_prefix}_next_instance_of")
|
|
|
|
|
|
|
|
block = <<~RUBY.chomp
|
|
|
|
do |instance|
|
|
|
|
#{rspec_prefix}(instance).#{node.method_name} #{node.children.last.source}
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
"#{method_call} #{block}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|