Sha256: f51b4ae1c4bf1928768f45693992493f02e43549bae3d9c13b70effcf8671ae5

Contents?: true

Size: 1.19 KB

Versions: 42

Compression:

Stored size: 1.19 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 < Cop
        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)

            add_offense(node, message: format(MSG, type: node_type))
          end
        end

        def autocorrect(node)
          receiver, node_type = node_type_check(node)
          range = Parser::Source::Range.new(node.source_range.source_buffer,
                                            receiver.loc.expression.end_pos + 1,
                                            node.loc.expression.end_pos)

          lambda do |corrector|
            corrector.replace(range, "#{node_type}_type?")
          end
        end
      end
    end
  end
end

Version data entries

42 entries across 31 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.86.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.85.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.85.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.84.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.83.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.82.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.81.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.80.1 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.80.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.79.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
rubocop-0.78.0 lib/rubocop/cop/internal_affairs/node_type_predicate.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/internal_affairs/node_type_predicate.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/internal_affairs/node_type_predicate.rb