Sha256: ca1015091631b5909044d6b571a0e564e6b43ef1810931d082afeb58b71082e0

Contents?: true

Size: 989 Bytes

Versions: 5

Compression:

Stored size: 989 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop enforces consistent use of `Object#is_a?` or `Object#kind_of?`.
      class ClassCheck < Cop
        include ConfigurableEnforcedStyle

        MSG = 'Prefer `Object#%s` over `Object#%s`.'.freeze

        def_node_matcher :class_check?, '(send _ ${:is_a? :kind_of?} _)'

        def on_send(node)
          class_check?(node) do |method_name|
            return if style == method_name

            add_offense(node, :selector)
          end
        end

        def message(node)
          if node.method?(:is_a?)
            format(MSG, 'kind_of?', 'is_a?')
          else
            format(MSG, 'is_a?', 'kind_of?')
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            replacement = node.method?(:is_a?) ? 'kind_of?' : 'is_a?'

            corrector.replace(node.loc.selector, replacement)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.50.0 lib/rubocop/cop/style/class_check.rb
rubocop-0.49.1 lib/rubocop/cop/style/class_check.rb
rubocop-0.49.0 lib/rubocop/cop/style/class_check.rb
rubocop-0.48.1 lib/rubocop/cop/style/class_check.rb
rubocop-0.48.0 lib/rubocop/cop/style/class_check.rb