Sha256: 6cf3c38a263ed84312c9368c349c7760b6a0feb7013ee7e4fd18c5724a16c1c1

Contents?: true

Size: 998 Bytes

Versions: 30

Compression:

Stored size: 998 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 < Base
        extend AutoCorrector

        MSG = 'Favor `Array#join` over `Array#*`.'
        RESTRICT_ON_SEND = %i[*].freeze

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

        def on_send(node)
          return unless (array, join_arg = join_candidate?(node))

          add_offense(node.loc.selector) do |corrector|
            corrector.replace(node, "#{array.source}.join(#{join_arg.source})")
          end
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/array_join.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/array_join.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/array_join.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/array_join.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/array_join.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/array_join.rb
rubocop-1.10.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.9.1 lib/rubocop/cop/style/array_join.rb
rubocop-1.9.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.8.1 lib/rubocop/cop/style/array_join.rb
rubocop-1.8.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.7.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.6.1 lib/rubocop/cop/style/array_join.rb
rubocop-1.6.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.5.2 lib/rubocop/cop/style/array_join.rb
rubocop-1.5.1 lib/rubocop/cop/style/array_join.rb
rubocop-1.5.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.4.2 lib/rubocop/cop/style/array_join.rb
rubocop-1.4.1 lib/rubocop/cop/style/array_join.rb
rubocop-1.4.0 lib/rubocop/cop/style/array_join.rb