Sha256: 98b6b431f85e99256633da41bb7308ab745bb3326a94585ea3843c38a3ebe722
Contents?: true
Size: 1.73 KB
Versions: 13
Compression:
Stored size: 1.73 KB
Contents
# frozen_string_literal: true 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`.'.freeze def on_send(node) return unless eligible_node?(node) add_offense(node, node.loc.selector) end private def autocorrect(node) ->(corrector) { corrector.replace(node.loc.selector, 'size') } end def eligible_node?(node) receiver, method, args = *node return false unless method == :count return false if args eligible_receiver?(receiver) && !allowed_parent?(node.parent) end def eligible_receiver?(node) return false unless node array?(node) || hash?(node) end def allowed_parent?(node) node && node.block_type? end 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
13 entries across 13 versions & 2 rubygems