Skip to content

Commit

Permalink
Change Rails/RootPathnameMethods to detect offenses on Dir.[]
Browse files Browse the repository at this point in the history
  • Loading branch information
r7kamura committed May 10, 2023
1 parent 24ff064 commit d57ed94
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1003](https://github.com/rubocop/rubocop-rails/pull/1003): Change `Rails/RootPathnameMethods` to detect offenses on `Dir.[]`. ([@r7kamura][])
27 changes: 21 additions & 6 deletions lib/rubocop/cop/rails/root_pathname_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,28 @@ module Rails
# Rails.root.join('db', 'schema.rb').write(content)
# Rails.root.join('db', 'schema.rb').binwrite(content)
#
class RootPathnameMethods < Base
class RootPathnameMethods < Base # rubocop:disable Metrics/ClassLength
extend AutoCorrector
include RangeHelp

MSG = '`%<rails_root>s` is a `Pathname` so you can just append `#%<method>s`.'

DIR_METHODS = %i[children delete each_child empty? entries exist? glob mkdir open rmdir unlink].to_set.freeze
DIR_GLOB_METHODS = %i[[] glob].to_set.freeze

DIR_NON_GLOB_METHODS = %i[
children
delete
each_child
empty?
entries
exist?
mkdir
open
rmdir
unlink
].to_set.freeze

DIR_METHODS = (DIR_GLOB_METHODS + DIR_NON_GLOB_METHODS).freeze

FILE_METHODS = %i[
atime
Expand Down Expand Up @@ -145,7 +160,7 @@ class RootPathnameMethods < Base

def_node_matcher :dir_glob?, <<~PATTERN
(send
(const {cbase nil?} :Dir) :glob ...)
(const {cbase nil?} :Dir) DIR_GLOB_METHODS ...)
PATTERN

def_node_matcher :rails_root_pathname?, <<~PATTERN
Expand All @@ -164,7 +179,7 @@ def on_send(node)
evidence(node) do |method, path, args, rails_root|
add_offense(node, message: format(MSG, method: method, rails_root: rails_root.source)) do |corrector|
replacement = if dir_glob?(node)
build_path_glob_replacement(path, method)
build_path_glob_replacement(path)
else
build_path_replacement(path, method, args)
end
Expand All @@ -183,12 +198,12 @@ def evidence(node)
yield(method, path, args, rails_root)
end

def build_path_glob_replacement(path, method)
def build_path_glob_replacement(path)
receiver = range_between(path.source_range.begin_pos, path.children.first.loc.selector.end_pos).source

argument = path.arguments.one? ? path.first_argument.source : join_arguments(path.arguments)

"#{receiver}.#{method}(#{argument})"
"#{receiver}.glob(#{argument})"
end

def build_path_replacement(path, method, args)
Expand Down
17 changes: 14 additions & 3 deletions spec/rubocop/cop/rails/root_pathname_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

RSpec.describe RuboCop::Cop::Rails::RootPathnameMethods, :config do
{
Dir: described_class::DIR_METHODS,
Dir: described_class::DIR_NON_GLOB_METHODS,
File: described_class::FILE_METHODS,
FileTest: described_class::FILE_TEST_METHODS,
FileUtils: described_class::FILE_UTILS_METHODS,
IO: described_class::FILE_METHODS
}.each do |receiver, methods|
methods.each do |method|
next if method == :glob

it "registers an offense when using `#{receiver}.#{method}(Rails.public_path)` (if arity exists)" do
expect_offense(<<~RUBY, receiver: receiver, method: method)
%{receiver}.%{method}(Rails.public_path)
Expand Down Expand Up @@ -46,6 +44,19 @@
end
end

context 'when using Dir.[]' do
it 'registers offense when using `Dir[Rails.root.join(...)]`' do
expect_offense(<<~RUBY)
Dir[Rails.root.join('spec/support/**/*.rb')]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#[]`.
RUBY

expect_correction(<<~RUBY)
Rails.root.glob('spec/support/**/*.rb')
RUBY
end
end

context 'when using `Dir.glob`' do
it "registers an offense when using `Dir.glob(Rails.root.join('**/*.rb'))`" do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit d57ed94

Please sign in to comment.