Sha256: 18d7f80f6c6c74fbcbcdde1c6774f36996d3de229ac86bfd5fbbf41d8714c486

Contents?: true

Size: 959 Bytes

Versions: 4

Compression:

Stored size: 959 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop enforces the use of `collection.exclude?(obj)`
      # over `!collection.include?(obj)`.
      #
      # @example
      #   # bad
      #   !array.include?(2)
      #   !hash.include?(:key)
      #
      #   # good
      #   array.exclude?(2)
      #   hash.exclude?(:key)
      #
      class NegateInclude < Cop
        MSG = 'Use `.exclude?` and remove the negation part.'

        def_node_matcher :negate_include_call?, <<~PATTERN
          (send (send $_ :include? $_) :!)
        PATTERN

        def on_send(node)
          add_offense(node) if negate_include_call?(node)
        end

        def autocorrect(node)
          negate_include_call?(node) do |receiver, obj|
            lambda do |corrector|
              corrector.replace(node, "#{receiver.source}.exclude?(#{obj.source})")
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-rails-2.8.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.8.0 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.7.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.7.0 lib/rubocop/cop/rails/negate_include.rb