Sha256: 30d506867cf91524344d187b4da6552b6aef5cfa785c8f38bf78c2d9706b2cc3

Contents?: true

Size: 1.79 KB

Versions: 7146

Compression:

Stored size: 1.79 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, location: :selector)
        end

        def autocorrect(node)
          ->(corrector) { corrector.replace(node.loc.selector, 'size') }
        end

        private

        def eligible_node?(node)
          return false unless node.method?(:count) && !node.arguments?

          eligible_receiver?(node.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)
          return true if node.array_type?
          return false unless node.send_type?

          _, constant = *node.receiver

          constant == :Array || node.method_name == :to_a
        end

        def hash?(node)
          return true if node.hash_type?
          return false unless node.send_type?

          _, constant = *node.receiver

          constant == :Hash || node.method_name == :to_h
        end
      end
    end
  end
end

Version data entries

7,146 entries across 7,140 versions & 27 rubygems

Version Path
cybrid_api_id_ruby-0.123.310 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_bank_ruby-0.123.310 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_organization_ruby-0.123.310 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_organization_ruby-0.123.309 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_bank_ruby-0.123.309 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_id_ruby-0.123.309 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_bank_ruby-0.123.307 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_organization_ruby-0.123.307 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_id_ruby-0.123.307 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_bank_ruby-0.123.306 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_id_ruby-0.123.306 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_organization_ruby-0.123.306 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_bank_ruby-0.123.305 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_id_ruby-0.123.305 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_organization_ruby-0.123.305 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_organization_ruby-0.123.303 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_bank_ruby-0.123.303 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_id_ruby-0.123.303 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_bank_ruby-0.123.302 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb
cybrid_api_organization_ruby-0.123.302 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/performance/size.rb