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

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

Overview

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 }

Cop Safety Information:

  • This cop is unsafe, as it 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], …​]

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 TargetRubyVersion

minimum_target_ruby_version, required_minimum_ruby_version, support_target_ruby_version?

Methods included from HashTransformMethod

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

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, 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, #inspect, 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?, #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

#on_bad_each_with_object(node) ⇒ Object



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

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

#on_bad_hash_brackets_map(node) ⇒ Object



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

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

#on_bad_map_to_h(node) ⇒ Object



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

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

#on_bad_to_h(node) ⇒ Object



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

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