Sha256: 6ea09206548fd14c4fa89ba3463ece618cb0a52d75b74a7c4e0e7d2f34febbed

Contents?: true

Size: 1.09 KB

Versions: 74

Compression:

Stored size: 1.09 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.loc.expression.with(begin_pos: receiver.loc.expression.end_pos + 1)
              corrector.replace(range, "#{node_type}_type?")
            end
          end
        end
      end
    end
  end
end

Version data entries

74 entries across 68 versions & 8 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.46.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.45.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.45.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.44.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.44.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.43.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.42.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.41.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb