Sha256: 8f918f681435a1682d7823c5cd80e4d1542a1ee948231929583f24ab5fc4e14a

Contents?: true

Size: 1.07 KB

Versions: 15

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Enforces the use of `collection.exclude?(obj)`
      # over `!collection.include?(obj)`.
      #
      # @safety
      #   This cop is unsafe because false positive will occur for
      #   receiver objects that do not have an `exclude?` method. (e.g. `IPAddr`)
      #
      # @example
      #   # bad
      #   !array.include?(2)
      #   !hash.include?(:key)
      #
      #   # good
      #   array.exclude?(2)
      #   hash.exclude?(:key)
      #
      class NegateInclude < Base
        extend AutoCorrector

        MSG = 'Use `.exclude?` and remove the negation part.'
        RESTRICT_ON_SEND = %i[!].freeze

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

        def on_send(node)
          return unless (receiver, obj = negate_include_call?(node))

          add_offense(node) do |corrector|
            corrector.replace(node, "#{receiver.source}.exclude?(#{obj.source})")
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/negate_include.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/negate_include.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/negate_include.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/negate_include.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.17.4 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.17.3 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.17.2 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.17.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.17.0 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.16.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.16.0 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.15.2 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.15.1 lib/rubocop/cop/rails/negate_include.rb
rubocop-rails-2.15.0 lib/rubocop/cop/rails/negate_include.rb