Sha256: 8b7b206c3bb921cdc69a6ef1ac55181a664c89961941390bacb34ce588ae0251

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop is used to identify usages of
      #
      # @example
      #   # bad
      #   [].push(*a)
      #
      #   # good
      #   [].concat(a)
      class PushSplat < Cop
        include Parentheses

        MSG = 'Use `concat` instead of `push(*)`.'.freeze

        def_node_matcher :push_splat, <<-END
          (send _ :push (splat ...))
        END

        def on_send(node)
          push_splat(node) do
            add_offense(node, :expression, MSG)
          end
        end

        def autocorrect(node)
          _receiver, _method, splat = *node
          body, = *splat
          lambda do |corrector|
            corrector.replace(node.location.selector, 'concat')

            source = if parens_required?(splat)
                       "(#{body.source})"
                     else
                       body.source
                     end
            corrector.replace(splat.loc.expression, source)
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/performance/push_splat.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/performance/push_splat.rb
rubocop-0.42.0 lib/rubocop/cop/performance/push_splat.rb
rubocop-0.41.2 lib/rubocop/cop/performance/push_splat.rb
rubocop-0.41.1 lib/rubocop/cop/performance/push_splat.rb
rubocop-0.41.0 lib/rubocop/cop/performance/push_splat.rb