Sha256: 4a7a2e38688fac0e4aaaaf64d78a8b0404e23b278769106e1994578ab99d3c26

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 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.'

        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

8 entries across 8 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.90.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.89.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.89.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.88.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.87.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.87.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb