Class: Hone::Patterns::FlattenOnce

Inherits:
Base
  • Object
show all
Defined in:
lib/hone/patterns/flatten_once.rb

Overview

Pattern: array.flatten -> consider array.flatten(1)

flatten without an argument flattens all levels recursively. If you only need one level, flatten(1) is faster.

Instance Attribute Summary

Attributes inherited from Base

#findings

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
# File 'lib/hone/patterns/flatten_once.rb', line 13

def visit_call_node(node)
  super

  # Look for: .flatten without arguments
  return unless node.name == :flatten
  return unless node.arguments.nil?

  add_finding(
    node,
    message: "Consider `.flatten(1)` if only one level of flattening is needed",
    speedup: "Faster when you only need one level"
  )
end