-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
sort!
and minmax
to Performance/CompareWithBlock
- Loading branch information
1 parent
63f7c15
commit 57b3b09
Showing
3 changed files
with
36 additions
and
24 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 @@ | ||
* [#357](https://github.com/rubocop/rubocop-performance/pull/357): Add `sort!` and `minmax` to `Performance/CompareWithBlock`. ([@vlad-pisanov][]) |
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 |
---|---|---|
@@ -1,61 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::CompareWithBlock, :config do | ||
shared_examples 'compare with block' do |method| | ||
shared_examples 'compare with block' do |method, replacement| | ||
it "registers an offense and corrects for #{method}" do | ||
expect_offense(<<~RUBY, method: method) | ||
array.#{method} { |a, b| a.foo <=> b.foo } | ||
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method}_by(&:foo)` instead of `#{method} { |a, b| a.foo <=> b.foo }`. | ||
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{replacement}(&:foo)` instead of `#{method} { |a, b| a.foo <=> b.foo }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.#{method}_by(&:foo) | ||
array.#{replacement}(&:foo) | ||
RUBY | ||
end | ||
|
||
it "registers an offense and corrects for #{method} with [:foo]" do | ||
expect_offense(<<~RUBY, method: method) | ||
array.#{method} { |a, b| a[:foo] <=> b[:foo] } | ||
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method}_by { |a| a[:foo] }` instead of `#{method} { |a, b| a[:foo] <=> b[:foo] }`. | ||
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{replacement} { |a| a[:foo] }` instead of `#{method} { |a, b| a[:foo] <=> b[:foo] }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.#{method}_by { |a| a[:foo] } | ||
array.#{replacement} { |a| a[:foo] } | ||
RUBY | ||
end | ||
|
||
it "registers an offense and corrects for #{method} with ['foo']" do | ||
expect_offense(<<~RUBY, method: method) | ||
array.#{method} { |a, b| a['foo'] <=> b['foo'] } | ||
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method}_by { |a| a['foo'] }` instead of `#{method} { |a, b| a['foo'] <=> b['foo'] }`. | ||
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{replacement} { |a| a['foo'] }` instead of `#{method} { |a, b| a['foo'] <=> b['foo'] }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.#{method}_by { |a| a['foo'] } | ||
array.#{replacement} { |a| a['foo'] } | ||
RUBY | ||
end | ||
|
||
it "registers an offense and corrects for #{method} with [1]" do | ||
expect_offense(<<~RUBY, method: method) | ||
array.#{method} { |a, b| a[1] <=> b[1] } | ||
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method}_by { |a| a[1] }` instead of `#{method} { |a, b| a[1] <=> b[1] }`. | ||
^{method}^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{replacement} { |a| a[1] }` instead of `#{method} { |a, b| a[1] <=> b[1] }`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
array.#{method}_by { |a| a[1] } | ||
array.#{replacement} { |a| a[1] } | ||
RUBY | ||
end | ||
|
||
it "accepts valid #{method} usage" do | ||
expect_no_offenses("array.#{method} { |a, b| b <=> a }") | ||
end | ||
|
||
it "accepts #{method}_by" do | ||
expect_no_offenses("array.#{method}_by { |a| a.baz }") | ||
it "accepts #{replacement}" do | ||
expect_no_offenses("array.#{replacement} { |a| a.baz }") | ||
end | ||
end | ||
|
||
include_examples 'compare with block', 'sort' | ||
include_examples 'compare with block', 'max' | ||
include_examples 'compare with block', 'min' | ||
include_examples 'compare with block', 'sort', 'sort_by' | ||
include_examples 'compare with block', 'sort!', 'sort_by!' | ||
include_examples 'compare with block', 'max', 'max_by' | ||
include_examples 'compare with block', 'min', 'min_by' | ||
include_examples 'compare with block', 'minmax', 'minmax_by' | ||
end |