-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
142 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,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# This cop identifies places where `sort { |a, b| b <=> a }` | ||
# can be replaced by a faster `sort.reverse`. | ||
# | ||
# @example | ||
# # bad | ||
# array.sort { |a, b| b <=> a } | ||
# | ||
# # good | ||
# array.sort.reverse | ||
# | ||
class SortReverse < Cop | ||
include RangeHelp | ||
|
||
MSG = 'Use `sort.reverse` instead of `%<bad_method>s`.' | ||
|
||
def_node_matcher :sort_with_block?, <<~PATTERN | ||
(block | ||
$(send _ :sort) | ||
(args (arg $_a) (arg $_b)) | ||
$send) | ||
PATTERN | ||
|
||
def_node_matcher :replaceable_body?, <<~PATTERN | ||
(send (lvar %1) :<=> (lvar %2)) | ||
PATTERN | ||
|
||
def on_block(node) | ||
sort_with_block?(node) do |send, var_a, var_b, body| | ||
replaceable_body?(body, var_b, var_a) do | ||
range = sort_range(send, node) | ||
|
||
add_offense( | ||
node, | ||
location: range, | ||
message: message(var_a, var_b) | ||
) | ||
end | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
sort_with_block?(node) do |send, _var_a, _var_b, _body| | ||
lambda do |corrector| | ||
range = sort_range(send, node) | ||
replacement = 'sort.reverse' | ||
corrector.replace(range, replacement) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def sort_range(send, node) | ||
range_between(send.loc.selector.begin_pos, node.loc.end.end_pos) | ||
end | ||
|
||
def message(var_a, var_b) | ||
bad_method = "sort { |#{var_a}, #{var_b}| #{var_b} <=> #{var_a} }" | ||
format(MSG, bad_method: bad_method) | ||
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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::SortReverse do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) do | ||
# Suppress ChainArrayAllocation offences | ||
RuboCop::Config.new('Performance/ChainArrayAllocation' => { 'Enabled' => false }) | ||
end | ||
|
||
it 'registers an offense and corrects when sorting in reverse order' do | ||
expect_offense(<<~RUBY) | ||
array.sort { |a, b| b <=> a } | ||
^^^^^^^^^^^^^^^^^^^^^^^ Use `sort.reverse` instead of `sort { |a, b| b <=> a }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.sort.reverse | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when sorting in direct order' do | ||
expect_no_offenses(<<~RUBY) | ||
array.sort { |a, b| a <=> b } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when sorting in reverse order by some property' do | ||
expect_no_offenses(<<~RUBY) | ||
array.sort { |a, b| b.x <=> a.x } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `sort.reverse`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.sort.reverse | ||
RUBY | ||
end | ||
end |