Sha256: 5f647ea21a4b65a72c9a94b7dc343764ce5da16c13072df96519fcd2e155954f

Contents?: true

Size: 1006 Bytes

Versions: 3

Compression:

Stored size: 1006 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of "\*" as a substitute for *join*.
      #
      # Not all cases can reliably checked, due to Ruby's dynamic
      # types, so we consider only cases when the first argument is an
      # array literal or the second is a string literal.
      #
      # @example
      #
      #   # bad
      #   %w(foo bar baz) * ","
      #
      #   # good
      #   %w(foo bar baz).join(",")
      #
      class ArrayJoin < Cop
        MSG = 'Favor `Array#join` over `Array#*`.'

        def_node_matcher :join_candidate?, '(send $array :* $str)'

        def on_send(node)
          join_candidate?(node) { add_offense(node, location: :selector) }
        end

        def autocorrect(node)
          array, join_arg = join_candidate?(node).map(&:source)

          lambda do |corrector|
            corrector.replace(node, "#{array}.join(#{join_arg})")
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.84.0 lib/rubocop/cop/style/array_join.rb
rubocop-0.83.0 lib/rubocop/cop/style/array_join.rb
rubocop-0.82.0 lib/rubocop/cop/style/array_join.rb