Sha256: e9c4d433b18c0734011f71451375579029715f59c35fc54b3c6773d0ee0652fd

Contents?: true

Size: 1.65 KB

Versions: 37

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks where `match_array` is used.
      #
      # This cop checks for the following:
      # - Prefer `contain_exactly` when matching an array with values.
      # - Prefer `eq` when using `match_array` with an empty array literal.
      #
      # @example
      #   # bad
      #   it { is_expected.to match_array([content1, content2]) }
      #
      #   # good
      #   it { is_expected.to contain_exactly(content1, content2) }
      #
      #   # good
      #   it { is_expected.to match_array([content] + array) }
      #
      #   # good
      #   it { is_expected.to match_array(%w(tremble in fear foolish mortals)) }
      #
      class MatchArray < Base
        extend AutoCorrector

        MSG = 'Prefer `contain_exactly` when matching an array literal.'
        RESTRICT_ON_SEND = %i[match_array].freeze

        # @!method match_array_with_empty_array?(node)
        def_node_matcher :match_array_with_empty_array?, <<~PATTERN
          (send nil? :match_array (array))
        PATTERN

        def on_send(node)
          return unless node.first_argument&.array_type?
          return if match_array_with_empty_array?(node)

          check_populated_array(node)
        end

        private

        def check_populated_array(node)
          return if node.first_argument.percent_literal?

          add_offense(node) do |corrector|
            array_contents = node.arguments.flat_map(&:to_a)
            corrector.replace(
              node,
              "contain_exactly(#{array_contents.map(&:source).join(', ')})"
            )
          end
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 7 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/match_array.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/match_array.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/match_array.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/match_array.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/match_array.rb