Sha256: 3cdc30cb2b0b455654a916e78c65dbbcdbab788154aa47a0192ab0a9b5c2b45e

Contents?: true

Size: 1.49 KB

Versions: 9

Compression:

Stored size: 1.49 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop checks for .times.map calls.
      # In most cases such calls can be replaced
      # with an explicit array creation.
      #
      # @example
      #
      #   @bad
      #   9.times.map do |i|
      #     i.to_s
      #   end
      #
      #   @good
      #   Array.new(9) do |i|
      #     i.to_s
      #   end
      class TimesMap < Cop
        MSG = 'Use `Array.new` with a block instead of `.times.%s`.'.freeze

        def on_send(node)
          check(node)
        end

        def on_block(node)
          check(node)
        end

        private

        def check(node)
          times_map_call(node) do |map_or_collect|
            add_offense(node, :expression, format(MSG, map_or_collect))
          end
        end

        def_node_matcher :times_map_call, <<-END
          {(block (send (send !nil :times) ${:map :collect}) ...)
           (send (send !nil :times) ${:map :collect} (block_pass ...))}
        END

        def autocorrect(node)
          send_node = node.send_type? ? node : node.each_descendant(:send).first

          receiver, _method_name, *args = *send_node
          count, = *receiver

          replacement = "Array.new(#{count.source}" \
                        "#{args.map { |arg| ", #{arg.source}" }.join})"

          lambda do |corrector|
            corrector.replace(send_node.loc.expression, replacement)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 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/times_map.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/performance/times_map.rb
rubocop-0.43.0 lib/rubocop/cop/performance/times_map.rb
rubocop-0.42.0 lib/rubocop/cop/performance/times_map.rb
rubocop-0.41.2 lib/rubocop/cop/performance/times_map.rb
rubocop-0.41.1 lib/rubocop/cop/performance/times_map.rb
rubocop-0.41.0 lib/rubocop/cop/performance/times_map.rb
rubocop-0.40.0 lib/rubocop/cop/performance/times_map.rb
rubocop-0.39.0 lib/rubocop/cop/performance/times_map.rb