Sha256: 10b090501d959fd63a0ba169157518bd033acab66c2548d962f04d06e4f8cacc

Contents?: true

Size: 1.37 KB

Versions: 71

Compression:

Stored size: 1.37 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 < 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

        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

71 entries across 71 versions & 7 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/class_check.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/class_check.rb
rubocop-1.29.1 lib/rubocop/cop/style/class_check.rb
rubocop-1.29.0 lib/rubocop/cop/style/class_check.rb
rubocop-1.28.2 lib/rubocop/cop/style/class_check.rb
rubocop-1.28.1 lib/rubocop/cop/style/class_check.rb
rubocop-1.28.0 lib/rubocop/cop/style/class_check.rb
rubocop-1.27.0 lib/rubocop/cop/style/class_check.rb
rubocop-1.26.1 lib/rubocop/cop/style/class_check.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/class_check.rb
rubocop-1.26.0 lib/rubocop/cop/style/class_check.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_check.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_check.rb
rubocop-1.25.1 lib/rubocop/cop/style/class_check.rb
rubocop-1.25.0 lib/rubocop/cop/style/class_check.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_check.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/class_check.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/class_check.rb
rubocop-1.24.1 lib/rubocop/cop/style/class_check.rb
rubocop-1.24.0 lib/rubocop/cop/style/class_check.rb