Module: RuboCop::Cop::HashTransformMethod

Extended by:
Macros
Included in:
Style::HashTransformKeys, Style::HashTransformValues
Defined in:
lib/rubocop/cop/mixin/hash_transform_method.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

#array_receiver?(node) ⇒ Object



13
14
15
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 13

def_node_matcher :array_receiver?, <<~PATTERN
  {(array ...) (send _ :each_with_index) (send _ :with_index _ ?) (send _ :zip ...)}
PATTERN

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 17

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  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



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

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

#on_send(node) ⇒ Object



27
28
29
30
31
32
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 27

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