Sha256: 38e03f021e5308502b4652fd3aab7de9df80ad1baf07423e5046823bc05feea9

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 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)

          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

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rails-2.16.0 lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb