-
-
Notifications
You must be signed in to change notification settings - Fork 0
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/ReverseFirst
cop
- Loading branch information
1 parent
c2ddd57
commit 2e1f494
Showing
7 changed files
with
151 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
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
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,78 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# This cop identifies places where `reverse.first(n)` and `reverse.first` | ||
# can be replaced by `last(n).reverse` and `last`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# array.reverse.first(5) | ||
# array.reverse.first | ||
# | ||
# # good | ||
# array.last(5).reverse | ||
# array.last | ||
# | ||
class ReverseFirst < Cop | ||
include RangeHelp | ||
|
||
MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.' | ||
|
||
def_node_matcher :reverse_first_candidate?, <<~PATTERN | ||
(send $(send _ :reverse) :first (int _)?) | ||
PATTERN | ||
|
||
def on_send(node) | ||
reverse_first_candidate?(node) do |receiver| | ||
range = correction_range(receiver, node) | ||
message = build_message(node) | ||
|
||
add_offense(node, location: range, message: message) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
reverse_first_candidate?(node) do |receiver| | ||
range = correction_range(receiver, node) | ||
replacement = build_good_method(node) | ||
|
||
lambda do |corrector| | ||
corrector.replace(range, replacement) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def correction_range(receiver, node) | ||
range_between(receiver.loc.selector.begin_pos, node.loc.expression.end_pos) | ||
end | ||
|
||
def build_message(node) | ||
good_method = build_good_method(node) | ||
bad_method = build_bad_method(node) | ||
format(MSG, good_method: good_method, bad_method: bad_method) | ||
end | ||
|
||
def build_good_method(node) | ||
if node.arguments? | ||
"last(#{node.arguments.first.source}).reverse" | ||
else | ||
'last' | ||
end | ||
end | ||
|
||
def build_bad_method(node) | ||
if node.arguments? | ||
"reverse.first(#{node.arguments.first.source})" | ||
else | ||
'reverse.first' | ||
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
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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::ReverseFirst do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and corrects when using `#reverse.first(5)`' do | ||
expect_offense(<<~RUBY) | ||
array.reverse.first(5) | ||
^^^^^^^^^^^^^^^^ Use `last(5).reverse` instead of `reverse.first(5)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.last(5).reverse | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `#reverse.first`' do | ||
expect_offense(<<~RUBY) | ||
array.reverse.first | ||
^^^^^^^^^^^^^ Use `last` instead of `reverse.first`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.last | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when `#reverse` is not followed by `#first`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.reverse | ||
RUBY | ||
|
||
expect_no_offenses(<<~RUBY) | ||
array.reverse.last(5) | ||
RUBY | ||
end | ||
end |