Class: RuboCop::Cop::Style::HashTransformKeys

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

Overview

This cop looks for uses of `_.each_with_object({}) …`, `_.map ….to_h`, and `Hash[_.map …]` that are actually just transforming the keys of a hash, and tries to use a simpler & faster call to `transform_keys` instead.

This can produce false positives if we are transforming an enumerable of key-value-like pairs that isn't actually a hash, e.g.: `[[k1, v1], [k2, v2], …]`

This cop should only be enabled on Ruby version 2.5 or newer (`transform_keys` was added in Ruby 2.5.)

Examples:

# bad
{a: 1, b: 2}.each_with_object({}) { |(k, v), h| h[foo(k)] = v }
Hash[{a: 1, b: 2}.collect { |k, v| [foo(k), v] }]
{a: 1, b: 2}.map { |k, v| [k.to_s, v] }.to_h
{a: 1, b: 2}.to_h { |k, v| [k.to_s, v] }

# good
{a: 1, b: 2}.transform_keys { |k| foo(k) }
{a: 1, b: 2}.transform_keys { |k| k.to_s }

Constant Summary

Constants included from HashTransformMethod

HashTransformMethod::RESTRICT_ON_SEND

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from HashTransformMethod

#array_receiver?, #on_block, #on_csend, #on_send

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, 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?, #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

#on_bad_each_with_object(node) ⇒ Object


33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/style/hash_transform_keys.rb', line 33

def_node_matcher :on_bad_each_with_object, <<~PATTERN
  (block
    ({send csend} !#array_receiver? :each_with_object (hash))
    (args
      (mlhs
        (arg $_)
        (arg _val))
      (arg _memo))
    ({send csend} (lvar _memo) :[]= $!`_memo $(lvar _val)))
PATTERN

#on_bad_hash_brackets_map(node) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/style/hash_transform_keys.rb', line 45

def_node_matcher :on_bad_hash_brackets_map, <<~PATTERN
  (send
    (const _ :Hash)
    :[]
    (block
      ({send csend} !#array_receiver? {:map :collect})
      (args
        (arg $_)
        (arg _val))
      (array $_ $(lvar _val))))
PATTERN

#on_bad_map_to_h(node) ⇒ Object


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

def_node_matcher :on_bad_map_to_h, <<~PATTERN
  ({send csend}
    (block
      ({send csend} !#array_receiver? {:map :collect})
      (args
        (arg $_)
        (arg _val))
      (array $_ $(lvar _val)))
    :to_h)
PATTERN

#on_bad_to_h(node) ⇒ Object


70
71
72
73
74
75
76
77
# File 'lib/rubocop/cop/style/hash_transform_keys.rb', line 70

def_node_matcher :on_bad_to_h, <<~PATTERN
  (block
    ({send csend} !#array_receiver? :to_h)
    (args
      (arg $_)
      (arg _val))
    (array $_ $(lvar _val)))
PATTERN