Class: Gitlab::Styles::Rubocop::Cop::Style::HashTransformation
- Inherits:
-
RuboCop::Cop::Cop
- Object
- RuboCop::Cop::Cop
- Gitlab::Styles::Rubocop::Cop::Style::HashTransformation
- 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
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) = (node, call) add_offense(node, location: range, message: ) end end |