Class: RuboCop::Cop::Rails::TopLevelHashWithIndifferentAccess

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRailsVersion
Defined in:
lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb

Overview

Identifies top-level ‘HashWithIndifferentAccess`. This has been soft-deprecated since Rails 5.1.

Examples:

# bad
HashWithIndifferentAccess.new(foo: 'bar')

# good
ActiveSupport::HashWithIndifferentAccess.new(foo: 'bar')

Constant Summary collapse

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

Constants included from TargetRailsVersion

TargetRailsVersion::TARGET_GEM_NAME, TargetRailsVersion::USES_REQUIRES_GEM_API

Instance Method Summary collapse

Methods included from TargetRailsVersion

minimum_target_rails_version, support_target_rails_version?

Instance Method Details

#on_const(node) ⇒ Object

Parameters:

  • node (RuboCop::AST::ConstNode)


32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb', line 32

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

#top_level_hash_with_indifferent_access?(node) ⇒ Boolean

Parameters:

  • node (RuboCop::AST::ConstNode)

Returns:

  • (Boolean)


27
28
29
# File 'lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb', line 27

def_node_matcher :top_level_hash_with_indifferent_access?, <<~PATTERN
  (const {nil? cbase} :HashWithIndifferentAccess)
PATTERN