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
|
Defined in: | lib/rubocop/cop/performance/flat_map.rb |
Overview
Identifies usages of map { … }.flatten
and
change them to use flat_map { … }
instead.
Constant Summary
-
FLATTEN_MULTIPLE_LEVELS =
# File 'lib/rubocop/cop/performance/flat_map.rb', line 24' Beware, `flat_map` only flattens 1 level ' \ 'and `flatten` can be used to flatten ' \ 'multiple levels.'
-
MSG =
# File 'lib/rubocop/cop/performance/flat_map.rb', line 22'Use `flat_map` instead of `%<method>s...%<flatten>s`.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/flat_map.rb', line 23%i[flatten flatten!].freeze
Instance Method Summary
-
#on_csend(node)
Alias for #on_send.
- #on_send(node) (also: #on_csend)
- #autocorrect(corrector, node) private
- #offense_for_levels(node, map_node, first_method, flatten) private
- #offense_for_method(node, map_node, first_method, flatten) private
- #register_offense(node, map_node, first_method, flatten, message) private
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) = MSG + FLATTEN_MULTIPLE_LEVELS register_offense(node, map_node, first_method, flatten, ) 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.
# 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, ) 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) = format(, method: first_method, flatten: flatten) add_offense(range, message: ) do |corrector| autocorrect(corrector, node) end end