Sha256: 94bf55441c74a1f209537c07da43262fb9ffa3c35d87a5324b3d92ff1a20e1d4

Contents?: true

Size: 1.08 KB

Versions: 30

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

        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

30 entries across 30 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.10.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.9.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.9.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.8.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.8.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.7.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.6.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.6.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.5.2 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.5.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.5.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.4.2 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.4.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-1.4.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb