-
-
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.
Merge pull request #135 from fatkodima/redundant_sort_block-cop
Add new `Performance/RedundantSortBlock` cop
- Loading branch information
Showing
9 changed files
with
151 additions
and
16 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,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Common functionality for cops checking `Enumerable#sort` blocks. | ||
module SortBlock | ||
extend NodePattern::Macros | ||
include RangeHelp | ||
|
||
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 | ||
|
||
private | ||
|
||
def sort_range(send, node) | ||
range_between(send.loc.selector.begin_pos, node.loc.end.end_pos) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# This cop identifies places where `sort { |a, b| a <=> b }` | ||
# can be replaced with `sort`. | ||
# | ||
# @example | ||
# # bad | ||
# array.sort { |a, b| a <=> b } | ||
# | ||
# # good | ||
# array.sort | ||
# | ||
class RedundantSortBlock < Cop | ||
include SortBlock | ||
|
||
MSG = 'Use `sort` instead of `%<bad_method>s`.' | ||
|
||
def on_block(node) | ||
sort_with_block?(node) do |send, var_a, var_b, body| | ||
replaceable_body?(body, var_a, var_b) 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) | ||
corrector.replace(range, 'sort') | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def message(var_a, var_b) | ||
bad_method = "sort { |#{var_a}, #{var_b}| #{var_a} <=> #{var_b} }" | ||
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
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::RedundantSortBlock do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and corrects when sorting in direct order' do | ||
expect_offense(<<~RUBY) | ||
array.sort { |a, b| a <=> b } | ||
^^^^^^^^^^^^^^^^^^^^^^^ Use `sort` instead of `sort { |a, b| a <=> b }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.sort | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when sorting in reverse order' do | ||
expect_no_offenses(<<~RUBY) | ||
array.sort { |a, b| b <=> a } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when sorting in direct order by some property' do | ||
expect_no_offenses(<<~RUBY) | ||
array.sort { |a, b| a.x <=> b.x } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `sort`' do | ||
expect_no_offenses(<<~RUBY) | ||
array.sort | ||
RUBY | ||
end | ||
end |