Sha256: 7ce0afd97e19fb2d18af66875f990cbd0237b66044f2f4755edf2b6850b6d4be

Contents?: true

Size: 1.1 KB

Versions: 21

Compression:

Stored size: 1.1 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

21 entries across 21 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/be_empty.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/be_empty.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/be_empty.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/be_empty.rb