Sha256: e125509a728a84a0004c294aa0435d50e783671f31f11cc4ab7160215f6b58cc

Contents?: true

Size: 1.09 KB

Versions: 17

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Prefer using `be_empty` when checking for an empty array.
      #
      # @example
      #   # bad
      #   expect(array).to contain_exactly
      #   expect(array).to match_array([])
      #
      #   # good
      #   expect(array).to be_empty
      #
      class BeEmpty < Base
        extend AutoCorrector

        MSG = 'Use `be_empty` matchers for checking an empty array.'
        RESTRICT_ON_SEND = %i[contain_exactly match_array].freeze

        # @!method expect_array_matcher?(node)
        def_node_matcher :expect_array_matcher?, <<~PATTERN
          (send
            (send nil? :expect _)
            #Runners.all
            ${
              (send nil? :match_array (array))
              (send nil? :contain_exactly)
            }
          )
        PATTERN

        def on_send(node)
          expect_array_matcher?(node.parent) do |expect|
            add_offense(expect) do |corrector|
              corrector.replace(expect, 'be_empty')
            end
          end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.26.1/lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.26.1 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.26.0 lib/rubocop/cop/rspec/be_empty.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/be_empty.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_empty.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_empty.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_empty.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/be_empty.rb