Class: RuboCop::Cop::Style::HashConversion

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

Overview

Checks the usage of pre-2.1 ‘Hash` method of converting enumerables and sequences of values to hashes.

Correction code from splat argument (‘Hash`) is not simply determined. For example, `Hash` can be replaced with `ary.each_slice(2).to_h` but it will be complicated. So, `AllowSplatArgument` option is true by default to allow splat argument for simple code.

Examples:

# bad
Hash[ary]

# good
ary.to_h

# bad
Hash[key1, value1, key2, value2]

# good
{key1 => value1, key2 => value2}

AllowSplatArgument: true (default)

# good
Hash[*ary]

AllowSplatArgument: false

# bad
Hash[*ary]

Constant Summary collapse

MSG_TO_H =
'Prefer ary.to_h to Hash[ary].'
MSG_LITERAL_MULTI_ARG =
'Prefer literal hash to Hash[arg1, arg2, ...].'
MSG_LITERAL_HASH_ARG =
'Prefer literal hash to Hash[key: value, ...].'
MSG_SPLAT =
'Prefer array_of_pairs.to_h to Hash[*array].'
RESTRICT_ON_SEND =
%i[[]].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

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_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

#hash_from_array?(node) ⇒ Object



54
# File 'lib/rubocop/cop/style/hash_conversion.rb', line 54

def_node_matcher :hash_from_array?, '(send (const {nil? cbase} :Hash) :[] ...)'

#on_send(node) ⇒ Object



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

def on_send(node)
  return unless hash_from_array?(node)

  # There are several cases:
  # If there is one argument:
  #   Hash[ary] => ary.to_h
  #   Hash[*ary] => don't suggest corrections
  # If there is 0 or 2+ arguments:
  #   Hash[a1, a2, a3, a4] => {a1 => a2, a3 => a4}
  #   ...but don't suggest correction if there is odd number of them (it is a bug)
  node.arguments.count == 1 ? single_argument(node) : multi_argument(node)
end