Sha256: acee2ae6263b672d828efd5534319af837125879a06e1db779dd524804393779

Contents?: true

Size: 1.39 KB

Versions: 38

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Identifies top-level `HashWithIndifferentAccess`.
      # This has been soft-deprecated since Rails 5.1.
      #
      # @example
      #   # bad
      #   HashWithIndifferentAccess.new(foo: 'bar')
      #
      #   # good
      #   ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar')
      #
      class TopLevelHashWithIndifferentAccess < Base
        extend AutoCorrector
        extend TargetRailsVersion

        minimum_target_rails_version 5.1

        MSG = 'Avoid top-level `HashWithIndifferentAccess`.'

        # @!method top_level_hash_with_indifferent_access?(node)
        #   @param [RuboCop::AST::ConstNode] node
        #   @return [Boolean]
        def_node_matcher :top_level_hash_with_indifferent_access?, <<~PATTERN
          (const {nil? cbase} :HashWithIndifferentAccess)
        PATTERN

        # @param [RuboCop::AST::ConstNode] node
        def on_const(node)
          return unless top_level_hash_with_indifferent_access?(node)
          return if node.parent&.class_type? && node.parent.ancestors.any?(&:module_type?)

          add_offense(node) do |corrector|
            autocorrect(corrector, node)
          end
        end

        private

        def autocorrect(corrector, node)
          corrector.insert_before(node.location.name, 'ActiveSupport::')
        end
      end
    end
  end
end

Version data entries

38 entries across 37 versions & 6 rubygems

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