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

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

Overview

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. It 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 TargetRubyVersion

maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_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, #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

#on_bad_each_with_object(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/style/hash_transform_keys.rb', line 36

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

#on_bad_hash_brackets_map(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/style/hash_transform_keys.rb', line 48

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

#on_bad_map_to_h(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/style/hash_transform_keys.rb', line 61

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

#on_bad_to_h(node) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/style/hash_transform_keys.rb', line 73

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