Class: RuboCop::Cop::Style::HashFetchChain

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRubyVersion
Defined in:
lib/rubocop/cop/style/hash_fetch_chain.rb

Overview

Use ‘Hash#dig` instead of chaining potentially null `fetch` calls.

When ‘fetch(identifier, nil)` calls are chained on a hash, the expectation is that each step in the chain returns either `nil` or another hash, and in both cases, these can be simplified with a single call to `dig` with multiple arguments.

If the 2nd parameter is ‘{}` or `Hash.new`, an offense will also be registered, as long as the final call in the chain is a nil value. If a non-nil value is given, the chain will not be registered as an offense, as the default value cannot be safely given with `dig`.

NOTE: See ‘Style/DigChain` for replacing chains of `dig` calls with a single method call.

Examples:

# bad
hash.fetch('foo', nil)&.fetch('bar', nil)

# bad
# earlier members of the chain can return `{}` as long as the final `fetch`
# has `nil` as a default value
hash.fetch('foo', {}).fetch('bar', nil)

# good
hash.dig('foo', 'bar')

# ok - not handled by the cop since the final `fetch` value is non-nil
hash.fetch('foo', {}).fetch('bar', {})

Constant Summary collapse

MSG =
'Use `%<replacement>s` instead.'
RESTRICT_ON_SEND =
%i[fetch].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from TargetRubyVersion

maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#diggable?(node) ⇒ Object



50
51
52
# File 'lib/rubocop/cop/style/hash_fetch_chain.rb', line 50

def_node_matcher :diggable?, <<~PATTERN
  (call _ :fetch $_arg {nil (hash) (send (const {nil? cbase} :Hash) :new)})
PATTERN

#on_send(node) ⇒ Object Also known as: on_csend



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubocop/cop/style/hash_fetch_chain.rb', line 54

def on_send(node)
  return if ignored_node?(node)
  return if last_fetch_non_nil?(node)

  last_replaceable_node, arguments = inspect_chain(node)
  return unless last_replaceable_node
  return unless arguments.size > 1

  range = last_replaceable_node.selector.join(node.loc.end)
  replacement = replacement(arguments)
  message = format(MSG, replacement: replacement)

  add_offense(range, message: message) do |corrector|
    corrector.replace(range, replacement)
  end
end