Sha256: 194eae6487dcea05a46f49f3f488431879bbd9b9102ae770fdae2330a0da8dc6

Contents?: true

Size: 1.01 KB

Versions: 41

Compression:

Stored size: 1.01 KB

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

        # @!method join_candidate?(node)
        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

41 entries across 41 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/array_join.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/array_join.rb
rubocop-1.29.1 lib/rubocop/cop/style/array_join.rb
rubocop-1.29.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.28.2 lib/rubocop/cop/style/array_join.rb
rubocop-1.28.1 lib/rubocop/cop/style/array_join.rb
rubocop-1.28.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.27.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.26.1 lib/rubocop/cop/style/array_join.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/array_join.rb
rubocop-1.26.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.25.1 lib/rubocop/cop/style/array_join.rb
rubocop-1.25.0 lib/rubocop/cop/style/array_join.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/array_join.rb
rubocop-1.24.1 lib/rubocop/cop/style/array_join.rb
rubocop-1.24.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.23.0 lib/rubocop/cop/style/array_join.rb
rubocop-1.22.3 lib/rubocop/cop/style/array_join.rb
rubocop-1.22.2 lib/rubocop/cop/style/array_join.rb
rubocop-1.22.1 lib/rubocop/cop/style/array_join.rb