Sha256: 4259a69c4cecd62ec9f60b604305f285d3efa2e87af97d9864430605df677880

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Performance
      # This cop is used to identify usages of `count` on an
      # `Array` and `Hash` and change them to `size`.
      #
      # @example
      #   # bad
      #   [1, 2, 3].count
      #
      #   # bad
      #   {a: 1, b: 2, c: 3}.count
      #
      #   # good
      #   [1, 2, 3].size
      #
      #   # good
      #   {a: 1, b: 2, c: 3}.size
      #
      #   # good
      #   [1, 2, 3].count { |e| e > 2 }
      # TODO: Add advanced detection of variables that could
      # have been assigned to an array or a hash.
      class Size < Cop
        MSG = 'Use `size` instead of `count`.'

        def on_send(node)
          receiver, method = *node

          return if receiver.nil?
          return unless method == :count
          return unless array?(receiver) || hash?(receiver)
          return if node.parent && node.parent.block_type?

          add_offense(node, node.loc.selector)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            corrector.replace(node.loc.selector, 'size')
          end
        end

        private

        def array?(node)
          receiver, method = *node
          _, constant = *receiver

          node.array_type? || constant == :Array || method == :to_a
        end

        def hash?(node)
          receiver, method = *node
          _, constant = *receiver

          node.hash_type? || constant == :Hash || method == :to_h
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.30.1 lib/rubocop/cop/performance/size.rb
rubocop-0.30.0 lib/rubocop/cop/performance/size.rb