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


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

def on_block(node)
  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) do |*match|
    handle_possible_offense(node, match, 'to_h {...}')
  end
end

#on_csend(node) ⇒ Object


38
39
40
41
42
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 38

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

#on_send(node) ⇒ Object


29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 29

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) do |*match|
    handle_possible_offense(node, match, 'map {...}.to_h')
  end
end