Sha256: 204eaed9cc2dfe1e589e807b311957d5da9eec411854bb057a9203d6b602b726

Contents?: true

Size: 1.85 KB

Versions: 32

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Looks for uses of `each_with_object({}) { ... }`,
      # `map { ... }.to_h`, and `Hash[map { ... }]` that are transforming
      # an enumerable into a hash where the keys are the original elements.
      # Rails provides the `index_with` method for this purpose.
      #
      # @example
      #   # bad
      #   [1, 2, 3].each_with_object({}) { |el, h| h[el] = foo(el) }
      #   [1, 2, 3].to_h { |el| [el, foo(el)] }
      #   [1, 2, 3].map { |el| [el, foo(el)] }.to_h
      #   Hash[[1, 2, 3].collect { |el| [el, foo(el)] }]
      #
      #   # good
      #   [1, 2, 3].index_with { |el| foo(el) }
      class IndexWith < Base
        extend AutoCorrector
        extend TargetRailsVersion
        include IndexMethod

        minimum_target_rails_version 6.0

        def_node_matcher :on_bad_each_with_object, <<~PATTERN
          (block
            (call _ :each_with_object (hash))
            (args (arg $_el) (arg _memo))
            (call (lvar _memo) :[]= (lvar _el) $!`_memo))
        PATTERN

        def_node_matcher :on_bad_to_h, <<~PATTERN
          (block
            (call _ :to_h)
            (args (arg $_el))
            (array (lvar _el) $_))
        PATTERN

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

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

        private

        def new_method_name
          'index_with'
        end
      end
    end
  end
end

Version data entries

32 entries across 31 versions & 6 rubygems

Version Path
rubocop-rails-2.27.0 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/index_with.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/index_with.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/index_with.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/index_with.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.22.0 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.21.2 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.21.1 lib/rubocop/cop/rails/index_with.rb
rubocop-rails-2.21.0 lib/rubocop/cop/rails/index_with.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/cop/rails/index_with.rb