Sha256: 817c71e78f6fefe40a7fe951595c45bd528cf95b1dc7cf32737f935701e398ec

Contents?: true

Size: 1.39 KB

Versions: 37

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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 < Base
        include ConfigurableEnforcedStyle
        extend AutoCorrector

        MSG = 'Prefer `Object#%<prefer>s` over `Object#%<current>s`.'
        RESTRICT_ON_SEND = %i[is_a? kind_of?].freeze

        def on_send(node)
          return if style == node.method_name

          message = message(node)
          add_offense(node.loc.selector, message: message) do |corrector|
            replacement = node.method?(:is_a?) ? 'kind_of?' : 'is_a?'

            corrector.replace(node.loc.selector, replacement)
          end
        end
        alias on_csend on_send

        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

37 entries across 37 versions & 8 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/style/class_check.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/class_check.rb
rubocop-1.69.2 lib/rubocop/cop/style/class_check.rb
rubocop-1.69.1 lib/rubocop/cop/style/class_check.rb
rubocop-1.69.0 lib/rubocop/cop/style/class_check.rb
rubocop-1.68.0 lib/rubocop/cop/style/class_check.rb
rubocop-1.67.0 lib/rubocop/cop/style/class_check.rb
rubocop-1.66.1 lib/rubocop/cop/style/class_check.rb
rubocop-1.66.0 lib/rubocop/cop/style/class_check.rb
rubocop-1.65.1 lib/rubocop/cop/style/class_check.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/class_check.rb
rubocop-1.65.0 lib/rubocop/cop/style/class_check.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/class_check.rb
rubocop-1.64.1 lib/rubocop/cop/style/class_check.rb
rubocop-1.63.4 lib/rubocop/cop/style/class_check.rb
rubocop-1.63.3 lib/rubocop/cop/style/class_check.rb
rubocop-1.63.2 lib/rubocop/cop/style/class_check.rb
rubocop-1.63.1 lib/rubocop/cop/style/class_check.rb
rubocop-1.63.0 lib/rubocop/cop/style/class_check.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/class_check.rb