Sha256: 0066f07061124f95b3f93c05b91618ad3f858a5c687e7a91a8a4ffd68e69928d

Contents?: true

Size: 1.06 KB

Versions: 9

Compression:

Stored size: 1.06 KB

Contents

# encoding: utf-8

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#*`.'

        def on_send(node)
          receiver_node, method_name, *arg_nodes = *node
          return unless receiver_node && receiver_node.type == :array &&
                        method_name == :* && arg_nodes[0].type == :str

          add_offense(node, :selector)
        end

        def autocorrect(node)
          receiver_node, _method_name, *arg_nodes = *node
          expr = node.loc.expression
          array = receiver_node.loc.expression.source
          join_arg  = arg_nodes[0].loc.expression.source

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/array_join.rb
rubocop-0.35.0 lib/rubocop/cop/style/array_join.rb
rubocop-0.34.2 lib/rubocop/cop/style/array_join.rb
rubocop-0.34.1 lib/rubocop/cop/style/array_join.rb
rubocop-0.34.0 lib/rubocop/cop/style/array_join.rb
rubocop-0.33.0 lib/rubocop/cop/style/array_join.rb
rubocop-0.32.1 lib/rubocop/cop/style/array_join.rb
rubocop-0.32.0 lib/rubocop/cop/style/array_join.rb
rubocop-0.31.0 lib/rubocop/cop/style/array_join.rb