Sha256: 5cda906276a52efc272549442807f559a476555a5af8f4a3331ff466cb99fd7f

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # This cop looks for uses of `each_with_object({}) { ... }`,
      # `map { ... }.to_h`, and `Hash[map { ... }]` that are transforming
      # an enumerable into a hash where the values are the original elements.
      # Rails provides the `index_by` method for this purpose.
      #
      # @example
      #   # bad
      #   [1, 2, 3].each_with_object({}) { |el, h| h[foo(el)] = el }
      #   [1, 2, 3].map { |el| [foo(el), el] }.to_h
      #   Hash[[1, 2, 3].collect { |el| [foo(el), el] }]
      #
      #   # good
      #   [1, 2, 3].index_by { |el| foo(el) }
      class IndexBy < Cop
        include IndexMethod

        def_node_matcher :on_bad_each_with_object, <<~PATTERN
          (block
            ({send csend} _ :each_with_object (hash))
            (args (arg $_el) (arg _memo))
            ({send csend} (lvar _memo) :[]= $_ (lvar _el)))
        PATTERN

        def_node_matcher :on_bad_map_to_h, <<~PATTERN
          ({send csend}
            (block
              ({send csend} _ {:map :collect})
              (args (arg $_el))
              (array $_ (lvar _el)))
            :to_h)
        PATTERN

        def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
          (send
            (const _ :Hash)
            :[]
            (block
              ({send csend} _ {:map :collect})
              (args (arg $_el))
              (array $_ (lvar _el))))
        PATTERN

        private

        def new_method_name
          'index_by'
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-rails-2.7.1 lib/rubocop/cop/rails/index_by.rb
rubocop-rails-2.7.0 lib/rubocop/cop/rails/index_by.rb
rubocop-rails-2.6.0 lib/rubocop/cop/rails/index_by.rb
rubocop-rails-2.5.2 lib/rubocop/cop/rails/index_by.rb
rubocop-rails-2.5.1 lib/rubocop/cop/rails/index_by.rb
rubocop-rails-2.5.0 lib/rubocop/cop/rails/index_by.rb