Skip to content

Commit

Permalink
Support Ruby 2.7's numbered parameter for Style/RedundantSort
Browse files Browse the repository at this point in the history
This PR supports Ruby 2.7's numbered parameter for `Style/RedundantSort`
  • Loading branch information
koic authored and bbatsov committed Mar 19, 2020
1 parent 84d6c57 commit dccf875
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#7793](https://github.com/rubocop-hq/rubocop/pull/7793): Prefer `include?` over `member?` in `Style/CollectionMethods`. ([@dmolesUC][])
* [#7654](https://github.com/rubocop-hq/rubocop/issues/7654): Support `with_fixed_indentation` option for `Layout/ArrayAlignment` cop. ([@nikitasakov][])
* [#7783](https://github.com/rubocop-hq/rubocop/pull/7783): Support Ruby 2.7's numbered parameter for `Style/RedundantSort`. ([@koic][])

### Bug fixes

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/style/redundant_sort.rb
Expand Up @@ -63,9 +63,9 @@ class RedundantSort < Cop
(send $(send _ $:sort_by _) ${:last :first})
(send $(send _ $:sort_by _) ${:[] :at :slice} {(int 0) (int -1)})
(send (block $(send _ ${:sort_by :sort}) ...) ${:last :first})
(send ({block numblock} $(send _ ${:sort_by :sort}) ...) ${:last :first})
(send
(block $(send _ ${:sort_by :sort}) ...)
({block numblock} $(send _ ${:sort_by :sort}) ...)
${:[] :at :slice} {(int 0) (int -1)}
)
}
Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cop/style/redundant_sort_spec.rb
Expand Up @@ -178,4 +178,42 @@
expect(new_source).to eq('foo.max_by { |x| x.something }')
end
end

context '>= Ruby 2.7', :ruby27 do
context 'when using numbered parameter' do
it 'registers an offense and corrects when last is called on sort with ' \
'comparator' do
expect_offense(<<~RUBY)
foo.sort { _2 <=> _1 }.last
^^^^^^^^^^^^^^^^^^^^^^^ Use `max` instead of `sort...last`.
RUBY

expect_correction(<<~RUBY)
foo.max { _2 <=> _1 }
RUBY
end

it 'registers an offense and corrects when first is called on sort_by' do
expect_offense(<<~RUBY)
[1, 2, 3].sort_by { _1.length }.first
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `min_by` instead of `sort_by...first`.
RUBY

expect_correction(<<~RUBY)
[1, 2, 3].min_by { _1.length }
RUBY
end

it 'registers an offense and corrects when at(0) is called on sort_by' do
expect_offense(<<~RUBY)
[1, 2, 3].sort_by { _1.foo }.at(0)
^^^^^^^^^^^^^^^^^^^^^^^^ Use `min_by` instead of `sort_by...at(0)`.
RUBY

expect_correction(<<~RUBY)
[1, 2, 3].min_by { _1.foo }
RUBY
end
end
end
end

0 comments on commit dccf875

Please sign in to comment.