Sha256: f742c1fe6b943f1a79b9caafe18b8bcd520b2d0c752c1aee51da7ed4ab324526
Contents?: true
Size: 1.81 KB
Versions: 4
Compression:
Stored size: 1.81 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Minitest # This cop enforces the use of `refute_instance_of(Class, object)` # over `refute(object.instance_of?(Class))`. # # @example # # bad # refute(object.instance_of?(Class)) # refute(object.instance_of?(Class), 'the message') # # # good # refute_instance_of(Class, object) # refute_instance_of(Class, object, 'the message') # class RefuteInstanceOf < Cop include ArgumentRangeHelper MSG = 'Prefer using `refute_instance_of(%<arguments>s)` over ' \ '`refute(%<receiver>s)`.' def_node_matcher :refute_with_instance_of, <<~PATTERN (send nil? :refute $(send $_ :instance_of? $_) $...) PATTERN def on_send(node) refute_with_instance_of(node) do |first_receiver_arg, object, method, rest_args| message = rest_args.first arguments = node_arguments(object, method, message) receiver = [first_receiver_arg.source, message&.source].compact.join(', ') offense_message = format(MSG, arguments: arguments, receiver: receiver) add_offense(node, message: offense_message) end end def autocorrect(node) lambda do |corrector| refute_with_instance_of(node) do |_, object, method| corrector.replace(node.loc.selector, 'refute_instance_of') replacement = [method, object].map(&:source).join(', ') corrector.replace(first_argument_range(node), replacement) end end end private def node_arguments(object, method, message) [method, object, message].compact.map(&:source).join(', ') end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems