Sha256: a974c832609fe80b8bfc7f87499159c3a6820c39f5ffb989848fc1285b6681dd

Contents?: true

Size: 1.08 KB

Versions: 150

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks that node types are checked using the predicate helpers.
      #
      # @example
      #
      #   # bad
      #   node.type == :send
      #
      #   # good
      #   node.send_type?
      #
      class NodeTypePredicate < Base
        extend AutoCorrector

        MSG = 'Use `#%<type>s_type?` to check node type.'
        RESTRICT_ON_SEND = %i[==].freeze

        # @!method node_type_check(node)
        def_node_matcher :node_type_check, <<~PATTERN
          (send (send $_ :type) :== (sym $_))
        PATTERN

        def on_send(node)
          node_type_check(node) do |receiver, node_type|
            return unless Parser::Meta::NODE_TYPES.include?(node_type)

            message = format(MSG, type: node_type)
            add_offense(node, message: message) do |corrector|
              range = node.source_range.with(begin_pos: receiver.source_range.end_pos + 1)
              corrector.replace(range, "#{node_type}_type?")
            end
          end
        end
      end
    end
  end
end

Version data entries

150 entries across 149 versions & 15 rubygems

Version Path
rubocop-1.68.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.67.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.63.2 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.63.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.63.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.62.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.62.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.61.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/internal_affairs/node_type_predicate.rb