Sha256: b2140cd7c030d82c4e440983f6cc25fc884a920d5c3f3e0221b5414671262829
Contents?: true
Size: 1.99 KB
Versions: 2
Compression:
Stored size: 1.99 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Minitest # This cop enforces the test to use `refute_respond_to(object, :some_method)` # over `refute(object.respond_to?(:some_method))`. # # @example # # bad # refute(object.respond_to?(:some_method)) # refute(object.respond_to?(:some_method), 'the message') # refute(respond_to?(:some_method)) # # # good # refute_respond_to(object, :some_method) # refute_respond_to(object, :some_method, 'the message') # refute_respond_to(self, :some_method) # class RefuteRespondTo < Cop MSG = 'Prefer using `refute_respond_to(%<preferred>s)` over ' \ '`refute(%<over>s)`.' def_node_matcher :refute_with_respond_to, <<~PATTERN (send nil? :refute $(send $_ :respond_to? $_) $...) PATTERN def on_send(node) refute_with_respond_to(node) do |over, object, method, rest_args| custom_message = rest_args.first preferred = build_preferred_arguments(object, method, custom_message) over = [over, custom_message].compact.map(&:source).join(', ') message = format(MSG, preferred: preferred, over: over) add_offense(node, message: message) end end def autocorrect(node) lambda do |corrector| refute_with_respond_to(node) do |_, object, method, rest_args| custom_message = rest_args.first preferred = build_preferred_arguments(object, method, custom_message) replacement = "refute_respond_to(#{preferred})" corrector.replace(node.loc.expression, replacement) end end end private def build_preferred_arguments(receiver, method, message) receiver = receiver ? receiver.source : 'self' [receiver, method.source, message&.source].compact.join(', ') end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rubocop-minitest-0.5.0 | lib/rubocop/cop/minitest/refute_respond_to.rb |
rubocop-minitest-0.4.1 | lib/rubocop/cop/minitest/refute_respond_to.rb |