Sha256: af630b61e910117a91fbecdd0d257bf4dfa5c1a27c7cf874e4fbff876cbee608

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 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.
      class ArrayJoin < Cop
        MSG = 'Favor `Array#join` over `Array#*`.'.freeze

        def on_send(node)
          receiver_node, method_name, *arg_nodes = *node
          return unless receiver_node && receiver_node.array_type? &&
                        method_name == :* && arg_nodes.first.str_type?

          add_offense(node, :selector)
        end

        def autocorrect(node)
          receiver_node, _method_name, *arg_nodes = *node
          array = receiver_node.source
          join_arg = arg_nodes.first.source

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.45.0 lib/rubocop/cop/style/array_join.rb
rubocop-0.44.1 lib/rubocop/cop/style/array_join.rb
rubocop-0.44.0 lib/rubocop/cop/style/array_join.rb