Class: RuboCop::Cop::Style::HashTransformValues

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

Overview

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

Examples:

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

# good
{a: 1, b: 2}.transform_values { |v| foo(v) }
{a: 1, b: 2}.transform_values { |v| v * v }

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


31
32
33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/style/hash_transform_values.rb', line 31

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

#on_bad_hash_brackets_map(node) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/style/hash_transform_values.rb', line 43

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

#on_bad_map_to_h(node) ⇒ Object


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

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

#on_bad_to_h(node) ⇒ Object


68
69
70
71
72
73
74
75
# File 'lib/rubocop/cop/style/hash_transform_values.rb', line 68

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