Class: RuboCop::Cop::Performance::ZipWithoutBlock
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/zip_without_block.rb |
Overview
Checks for map { |id| [id] }
and suggests replacing it with zip
.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/performance/zip_without_block.rb', line 23'Use `zip` without a block argument instead.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/zip_without_block.rb', line 24Set.new(%i[map collect]).freeze
Instance Method Summary
- #map_with_array?(node)
-
#on_csend(node)
Alias for #on_send.
- #on_send(node) (also: #on_csend)
- #offense_range(node) private
- #register_offense(node) private
Instance Method Details
#map_with_array?(node)
[ GitHub ]# File 'lib/rubocop/cop/performance/zip_without_block.rb', line 27
def_node_matcher :map_with_array?, <<~PATTERN { (block (call !nil? RESTRICT_ON_SEND) (args (arg _)) (array (lvar _))) (numblock (call !nil? RESTRICT_ON_SEND) 1 (array (lvar _))) } PATTERN
#offense_range(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/zip_without_block.rb', line 50
def offense_range(node) node.loc.selector.join(node.parent.loc.end) end
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/performance/zip_without_block.rb', line 39
alias on_csend on_send
#on_send(node) Also known as: #on_csend
[ GitHub ]# File 'lib/rubocop/cop/performance/zip_without_block.rb', line 34
def on_send(node) return unless map_with_array?(node.parent) register_offense(node) end
#register_offense(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/zip_without_block.rb', line 43
def register_offense(node) offense_range = offense_range(node) add_offense(offense_range) do |corrector| corrector.replace(offense_range, 'zip') end end