Module: RuboCop::Cop::HashTransformMethod

Extended by:
Macros
Included in:
Style::HashTransformKeys, Style::HashTransformValues
Defined in:
lib/rubocop/cop/mixin/hash_transform_method.rb,
lib/rubocop/cop/mixin/hash_transform_method/autocorrection.rb

Overview

Common functionality for Style/HashTransformKeys and Style/HashTransformValues

Defined Under Namespace

Classes: Autocorrection, Captures

Constant Summary collapse

RESTRICT_ON_SEND =
%i[[] to_h].freeze

Instance Method Summary collapse

Instance Method Details

#hash_receiver?(node) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 33

def_node_matcher :hash_receiver?, <<~PATTERN
  {(hash ...)
   (send _ {:to_h :to_hash :merge :merge! :update :invert :except :tally} ...)
   (block (send _ {:group_by :to_h :tally :transform_keys :transform_keys!
                   :transform_values :transform_values!}) ...)
   (block (send _ :each_with_object (hash)) ...)}
PATTERN

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler



41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 41

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
  on_bad_each_with_object(node) do |*match|
    handle_possible_offense(node, match, 'each_with_object')
  end

  return if target_ruby_version < 2.6

  on_bad_to_h(node) { |*match| handle_possible_offense(node, match, 'to_h {...}') }
end

#on_csend(node) ⇒ Object



58
59
60
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 58

def on_csend(node)
  on_bad_map_to_h(node) { |*match| handle_possible_offense(node, match, 'map {...}.to_h') }
end

#on_send(node) ⇒ Object



51
52
53
54
55
56
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 51

def on_send(node)
  on_bad_hash_brackets_map(node) do |*match|
    handle_possible_offense(node, match, 'Hash[_.map {...}]')
  end
  on_bad_map_to_h(node) { |*match| handle_possible_offense(node, match, 'map {...}.to_h') }
end