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[args] method of converting enumerables and sequences of values to hashes.

Correction code from splat argument (Hash[*ary]) is not simply determined. For example, Hash[*ary] 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

Constants inherited from Base

Base::DEPARTMENT_SEVERITIES

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source, #project_index

Class Method Summary collapse

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?, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, #disabled_autocorrect?, 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, #preview?, #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

cop_dir_for, #exclude_limit, read_limits

Methods included from AutocorrectLogic

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

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

emulate_write_read_cycle, replace_file_contents, silence_warnings

Constructor Details

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

Class Method Details

.autocorrect_incompatible_with ⇒ Object

A hash literal as the first argument needs the parentheses of the call, which Style/MethodCallWithArgsParentheses removes in the same pass.



58
59
60
# File 'lib/rubocop/cop/style/hash_conversion.rb', line 58

def self.autocorrect_incompatible_with
  [Style::MethodCallWithArgsParentheses]
end

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



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubocop/cop/style/hash_conversion.rb', line 62

def on_send(node)
  return if part_of_ignored_node?(node) || !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.one? ? single_argument(node) : multi_argument(node)
  ignore_node(node)
end