-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new `Performance/ZipForArrayWrapping` cop that checks patterns like `.map { |id| [id] }` or `.map { [_1] }` and can safely replace them with `.zip` This is a Performance Cop for the more efficient way to generate an Array of Arrays. * Performs 40-90% faster than `.map` to iteratively wrap array contents. * Performs 5 - 55% faster on ranges, depending on size.
- Loading branch information
1 parent
4e147b0
commit 0668ee0
Showing
5 changed files
with
467 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#462](https://github.com/rubocop/rubocop-performance/pull/462): Add new `Performance/ZipForArrayWrapping` cop that checks patterns like `.map { |id| [id] }` or `.map { [_1] }` and can safely replace them with `.zip`. ([@corsonknowles][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# Checks for `.map { |id| [id] }` and suggests replacing it with `.zip`. | ||
# | ||
# @example | ||
# # bad | ||
# [1, 2, 3].map { |id| [id] } | ||
# | ||
# # bad | ||
# [1, 2, 3].map { [_1] } | ||
# | ||
# # good | ||
# [1, 2, 3].zip | ||
# | ||
# @example | ||
# # good (no offense) | ||
# [1, 2, 3].map { |id| id } | ||
# | ||
# @example | ||
# # good (no offense) | ||
# [1, 2, 3].map { |id| [id, id] } | ||
# | ||
# @safety | ||
# This cop is unsafe for novel definitions of `map` and `collect` | ||
# on non-Enumerable objects that do not respond to `zip`. | ||
# To make your object enumerable, define an `each` method | ||
# as described in https://ruby-doc.org/core/Enumerable.html | ||
class ZipForArrayWrapping < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `zip` instead of `%<original_code>s`.' | ||
RESTRICT_ON_SEND = Set.new(%i[map collect]).freeze | ||
|
||
# @!method map_with_array?(node) | ||
def_node_matcher :map_with_array?, <<~PATTERN | ||
{ | ||
(block ({send csend} _ RESTRICT_ON_SEND) (args (arg _id)) (array (lvar _id))) | ||
(numblock ({send csend} _ RESTRICT_ON_SEND) 1 (array (lvar _))) | ||
} | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless map_with_array?(node.parent) | ||
return if node.receiver.nil? | ||
|
||
register_offense(node) | ||
end | ||
alias on_csend on_send | ||
|
||
private | ||
|
||
def register_offense(node) | ||
offense_range = offense_range(node.parent) | ||
add_offense(offense_range, message: message(node)) do |corrector| | ||
corrector.replace(offense_range, 'zip') | ||
end | ||
end | ||
|
||
def message(node) | ||
format(MSG, original_code: offense_range(node).source.lines.first.chomp) | ||
end | ||
|
||
def offense_range(node) | ||
@offense_range ||= node.children.first.loc.selector.join(node.loc.end) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.