-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Lint/RedundantDirGlobSort
cop
Sort globbed results by default in Ruby 3.0. https://bugs.ruby-lang.org/issues/8709 This PR adds new `Lint/RedundantDirGlobSort` cop. It checks for redundant `sort` method to `Dir.glob` and `Dir[]`. ```ruby # bad Dir.glob('./lib/**/*.rb').sort.each do |file| end Dir['./lib/**/*.rb'].sort.each do |file| end # good Dir.glob('./lib/**/*.rb').each do |file| end Dir['./lib/**/*.rb'].each do |file| end # good - Respect intent if `sort` keyword option is specified. Dir.glob('./lib/**/*.rb', sort: false).each do |file| end ``` Related PR #9300.
- Loading branch information
Showing
5 changed files
with
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#9301](https://github.com/rubocop-hq/rubocop/pull/9301): Add new `Lint/RedundantDirGlobSort` cop. ([@koic][]) |
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,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# Sort globbed results by default in Ruby 3.0. | ||
# This cop checks for redundant `sort` method to `Dir.glob` and `Dir[]`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# Dir.glob('./lib/**/*.rb').sort.each do |file| | ||
# end | ||
# | ||
# Dir['./lib/**/*.rb'].sort.each do |file| | ||
# end | ||
# | ||
# # good | ||
# Dir.glob('./lib/**/*.rb').each do |file| | ||
# end | ||
# | ||
# Dir['./lib/**/*.rb'].each do |file| | ||
# end | ||
# | ||
class RedundantDirGlobSort < Base | ||
extend AutoCorrector | ||
extend TargetRubyVersion | ||
|
||
minimum_target_ruby_version 3.0 | ||
|
||
MSG = 'Remove redundant `sort`.' | ||
RESTRICT_ON_SEND = %i[sort].freeze | ||
GLOB_METHODS = %i[glob []].freeze | ||
|
||
def on_send(node) | ||
return unless (receiver = node.receiver) | ||
return unless receiver.receiver.const_type? && receiver.receiver.short_name == :Dir | ||
return unless GLOB_METHODS.include?(receiver.method_name) | ||
|
||
add_offense(node.loc.selector) do |corrector| | ||
corrector.remove(node.loc.selector) | ||
corrector.remove(node.loc.dot) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::RedundantDirGlobSort, :config do | ||
context 'when Ruby 3.0 or higher', :ruby30 do | ||
it 'registers an offense and correction when using `Dir.glob.sort`' do | ||
expect_offense(<<~RUBY) | ||
Dir.glob(Rails.root.join('test', '*.rb')).sort.each(&method(:require)) | ||
^^^^ Remove redundant `sort`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Dir.glob(Rails.root.join('test', '*.rb')).each(&method(:require)) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and correction when using `::Dir.glob.sort`' do | ||
expect_offense(<<~RUBY) | ||
::Dir.glob(Rails.root.join('test', '*.rb')).sort.each(&method(:require)) | ||
^^^^ Remove redundant `sort`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
::Dir.glob(Rails.root.join('test', '*.rb')).each(&method(:require)) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and correction when using `Dir[].sort.each do`' do | ||
expect_offense(<<~RUBY) | ||
Dir['./lib/**/*.rb'].sort.each do |file| | ||
^^^^ Remove redundant `sort`. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Dir['./lib/**/*.rb'].each do |file| | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and correction when using `Dir[].sort.each(&do_something)`' do | ||
expect_offense(<<~RUBY) | ||
Dir['./lib/**/*.rb'].sort.each(&method(:require)) | ||
^^^^ Remove redundant `sort`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Dir['./lib/**/*.rb'].each(&method(:require)) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when not using `sort` with `sort: false` option for `Dir`' do | ||
expect_no_offenses(<<~RUBY) | ||
Dir.glob(Rails.root.join('test', '*.rb'), sort: false).each do | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when Ruby 2.7 or lower', :ruby27 do | ||
it 'does not register an offense and correction when using `Dir.glob.sort`' do | ||
expect_no_offenses(<<~RUBY) | ||
Dir.glob(Rails.root.join('test', '*.rb')).sort.each(&method(:require)) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense and correction when using `::Dir.glob.sort`' do | ||
expect_no_offenses(<<~RUBY) | ||
::Dir.glob(Rails.root.join('test', '*.rb')).sort.each(&method(:require)) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense and correction when using `Dir[].sort.each do`' do | ||
expect_no_offenses(<<~RUBY) | ||
Dir['./lib/**/*.rb'].sort.each do |file| | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense and correction when using `Dir[].sort.each(&do_something)`' do | ||
expect_no_offenses(<<~RUBY) | ||
Dir['./lib/**/*.rb'].sort.each(&method(:require)) | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when not using `sort` for `Dir`' do | ||
expect_no_offenses(<<~RUBY) | ||
Dir['./lib/**/*.rb'].each do |file| | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `sort` without a receiver' do | ||
expect_no_offenses(<<~RUBY) | ||
sort.do_something | ||
RUBY | ||
end | ||
end |