Class: Gitlab::Styles::Rubocop::Cop::Style::HashTransformation

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Includes:
RuboCop::Cop::RangeHelp
Defined in:
lib/gitlab/styles/rubocop/cop/style/hash_transformation.rb

Overview

This cop identifies places where ‘map { … }.to_h` or `Hash[map { … }]` can be replaced with `to_h { … }`, saving an intermediate array allocation.

Full credit: github.com/eugeneius/rubocop-performance/blob/hash_transformation/lib/rubocop/cop/performance/hash_transformation.rb

Examples:

# bad
hash.map { |k, v| [v.upcase, k.downcase] }.to_h
hash.collect { |k, v| [v.upcase, k.downcase] }.to_h
Hash[hash.map { |k, v| [v.upcase, k.downcase] }]
Hash[hash.collect { |k, v| [v.upcase, k.downcase] }]
array.map { |x| [x, x + 1] }.to_h

# good
hash.to_h { |k, v| [v.upcase, k.downcase] }
array.to_h { |x| [x, x + 1] }

Constant Summary collapse

MSG =
'Use `to_h { ... }` instead of `%<current>s`.'

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/gitlab/styles/rubocop/cop/style/hash_transformation.rb', line 47

def autocorrect(node)
  block, call = to_h_candidate?(node)

  lambda do |corrector|
    corrector.remove(after_block(node, block))
    corrector.replace(call.loc.selector, 'to_h')
    corrector.remove(before_block(node, block))
  end
end

#on_send(node) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/gitlab/styles/rubocop/cop/style/hash_transformation.rb', line 39

def on_send(node)
  to_h_candidate?(node) do |_block, call|
    range = offense_range(node, call)
    message = message(node, call)
    add_offense(node, location: range, message: message)
  end
end