Class: Hone::Patterns::UniqBy
- Defined in:
- lib/hone/patterns/uniq_by.rb
Overview
Pattern: array.map { }.uniq -> consider array.uniq { }
map { }.uniq creates an intermediate array from map, then deduplicates. If deduplicating on the transformed value, uniq { } avoids the intermediate.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#add_finding, inherited, #initialize, scan_file
Constructor Details
This class inherits a constructor from Hone::Patterns::Base
Instance Method Details
#visit_call_node(node) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/hone/patterns/uniq_by.rb', line 13 def visit_call_node(node) super # Look for: .uniq where receiver is .map with a block return unless node.name == :uniq return unless node.arguments.nil? && !block_attached?(node) receiver = node.receiver return unless receiver.is_a?(Prism::CallNode) return unless receiver.name == :map && block_attached?(receiver) add_finding( node, message: "Consider `.uniq { }` instead of `.map { }.uniq` if deduping on transform", speedup: "May avoid intermediate array if deduping on transform" ) end |