Sha256: 93c0072776a5093c542e9d29fbccf01e59ba390efa759eec434ceda31a79fd4b

Contents?: true

Size: 1.43 KB

Versions: 61

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop enforces consistent use of `Object#is_a?` or `Object#kind_of?`.
      #
      # @example EnforcedStyle: is_a? (default)
      #   # bad
      #   var.kind_of?(Date)
      #   var.kind_of?(Integer)
      #
      #   # good
      #   var.is_a?(Date)
      #   var.is_a?(Integer)
      #
      # @example EnforcedStyle: kind_of?
      #   # bad
      #   var.is_a?(Time)
      #   var.is_a?(String)
      #
      #   # good
      #   var.kind_of?(Time)
      #   var.kind_of?(String)
      #
      class ClassCheck < Cop
        include ConfigurableEnforcedStyle

        MSG = 'Prefer `Object#%<prefer>s` over `Object#%<current>s`.'

        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, location: :selector)
          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

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

Version data entries

61 entries across 42 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/class_check.rb
rubocop-0.89.0 lib/rubocop/cop/style/class_check.rb
rubocop-0.88.0 lib/rubocop/cop/style/class_check.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/class_check.rb
rubocop-0.87.1 lib/rubocop/cop/style/class_check.rb
rubocop-0.87.0 lib/rubocop/cop/style/class_check.rb
rubocop-0.86.0 lib/rubocop/cop/style/class_check.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/class_check.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/class_check.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/class_check.rb
rubocop-0.85.1 lib/rubocop/cop/style/class_check.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/class_check.rb
rubocop-0.85.0 lib/rubocop/cop/style/class_check.rb
rubocop-0.84.0 lib/rubocop/cop/style/class_check.rb
rubocop-0.83.0 lib/rubocop/cop/style/class_check.rb
rubocop-0.82.0 lib/rubocop/cop/style/class_check.rb
rubocop-0.81.0 lib/rubocop/cop/style/class_check.rb
rubocop-0.80.1 lib/rubocop/cop/style/class_check.rb
rubocop-0.80.0 lib/rubocop/cop/style/class_check.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/class_check.rb