Skip to content

Danger: Don't suggest `match_array` for single-element arrays

Peter Leitzen requested to merge pl-danger-specs-match-single into master

What does this MR do and why?

This MR adjust danger plugin specs to not suggest to use match_array on a single-element array literal.

Doing so is useless and confusing to the reader as the intent of the expectation is not clear.

Danger only suggest to use match_array if the array literal contains a comma (,) indicating a non-single-element list.

Screenshots or screen recordings

Verified on this MR
Screenshot_from_2023-01-23_21-29-05

RuboCop - a potential follow-up?

Using the 👮 (see code below) we are seeing 914 offenses where match_array is used but could be a plain eq.

14086 files inspected, 914 offenses detected, 914 offenses autocorrectable
Click to see code
# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      class MatchArraySingle < RuboCop::Cop::Base
        extend RuboCop::Cop::AutoCorrector

        MSG = 'Prefer `eq` over `match_array` on a single-element array literal.'

        RESTRICT_ON_SEND = %i[match_array]

        def_node_matcher :match_array, <<~PATTERN
          (send nil? :match_array $(array ...) ...)
        PATTERN

        def on_send(node)
          match_array(node) do |param|
            next unless param.values.size == 1

            add_offense(node.loc.selector) do |corrector|
              corrector.replace(node.loc.selector, 'eq')
            end
          end
        end
      end
    end
  end
end

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Peter Leitzen

Merge request reports