Class: RuboCop::Cop::Performance::SelectMap
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
TargetRubyVersion,
Base
|
|
Instance Chain:
self,
RangeHelp,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/select_map.rb |
Overview
In Ruby 2.7, Enumerable#filter_map
has been added.
This cop identifies places where select.map
can be replaced by filter_map
.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/performance/select_map.rb', line 24'Use `filter_map` instead of `%<method_name>s.map`.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/select_map.rb', line 25%i[select filter].freeze
Instance Method Summary
-
#on_csend(node)
Alias for #on_send.
- #on_send(node) (also: #on_csend)
- #map_method_candidate(node) private
- #offense_range(node, map_method) private
Instance Method Details
#map_method_candidate(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/select_map.rb', line 41
def map_method_candidate(node) return unless (parent = node.parent) if parent.block_type? && parent.parent&.call_type? parent.parent elsif parent.call_type? parent end end
#offense_range(node, map_method) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/select_map.rb', line 51
def offense_range(node, map_method) range_between(node.loc.selector.begin_pos, map_method.source_range.end_pos) end
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/performance/select_map.rb', line 37
alias on_csend on_send
#on_send(node) Also known as: #on_csend
[ GitHub ]# File 'lib/rubocop/cop/performance/select_map.rb', line 27
def on_send(node) return if (first_argument = node.first_argument) && !first_argument.block_pass_type? return unless (send_node = map_method_candidate(node)) return unless send_node.method?(:map) map_method = send_node.parent&.block_type? ? send_node.parent : send_node range = offense_range(node, map_method) add_offense(range, message: format(MSG, method_name: node.method_name)) end