123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Performance::FlatMap

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, AutoCorrector, Base
Instance Chain:
self, RangeHelp, Base
Inherits: Base
  • Object
Defined in: lib/rubocop/cop/performance/flat_map.rb

Overview

Identifies usages of map { …​ }.flatten and change them to use flat_map { …​ } instead.

Examples:

# bad
[1, 2, 3, 4].map { |e| [e, e] }.flatten(1)
[1, 2, 3, 4].collect { |e| [e, e] }.flatten(1)

# good
[1, 2, 3, 4].flat_map { |e| [e, e] }
[1, 2, 3, 4].map { |e| [e, e] }.flatten
[1, 2, 3, 4].collect { |e| [e, e] }.flatten

Constant Summary

Instance Method Summary

Instance Method Details

#autocorrect(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/flat_map.rb', line 73

def autocorrect(corrector, node)
  map_node, _first_method, _flatten, params = flat_map_candidate?(node)
  flatten_level, = *params.first

  return unless flatten_level

  map_send_node = map_node.block_type? ? map_node.send_node : map_node
  range = range_between(map_node.source_range.end_pos, node.source_range.end_pos)

  corrector.remove(range)
  corrector.replace(map_send_node.loc.selector, 'flat_map')
end

#offense_for_levels(node, map_node, first_method, flatten) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/flat_map.rb', line 53

def offense_for_levels(node, map_node, first_method, flatten)
  message = MSG + FLATTEN_MULTIPLE_LEVELS

  register_offense(node, map_node, first_method, flatten, message)
end

#offense_for_method(node, map_node, first_method, flatten) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/flat_map.rb', line 59

def offense_for_method(node, map_node, first_method, flatten)
  register_offense(node, map_node, first_method, flatten, MSG)
end

#on_csend(node)

Alias for #on_send.

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/flat_map.rb', line 49

alias on_csend on_send

#on_send(node) Also known as: #on_csend

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/flat_map.rb', line 39

def on_send(node)
  flat_map_candidate?(node) do |map_node, first_method, flatten, params|
    flatten_level, = *params.first
    if cop_config['EnabledForFlattenWithoutParams'] && !flatten_level
      offense_for_levels(node, map_node, first_method, flatten)
    elsif flatten_level == 1
      offense_for_method(node, map_node, first_method, flatten)
    end
  end
end

#register_offense(node, map_node, first_method, flatten, message) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/performance/flat_map.rb', line 63

def register_offense(node, map_node, first_method, flatten, message)
  map_send_node = map_node.block_type? ? map_node.send_node : map_node
  range = range_between(map_send_node.loc.selector.begin_pos, node.source_range.end_pos)
  message = format(message, method: first_method, flatten: flatten)

  add_offense(range, message: message) do |corrector|
    autocorrect(corrector, node)
  end
end