Skip to content

Commit 98a8c99

Browse files
authored
Merge pull request #1003 from r7kamura/root-pathname-methods-index
Change `Rails/RootPathnameMethods` to detect offenses on `Dir.[]`
2 parents 5617426 + d876323 commit 98a8c99

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1003](https://github.com/rubocop/rubocop-rails/pull/1003): Change `Rails/RootPathnameMethods` to detect offenses on `Dir.[]`. ([@r7kamura][])

lib/rubocop/cop/rails/root_pathname_methods.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class RootPathnameMethods < Base # rubocop:disable Metrics/ClassLength
3838

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

41-
DIR_GLOB_METHODS = %i[glob].to_set.freeze
41+
DIR_GLOB_METHODS = %i[[] glob].to_set.freeze
4242

4343
DIR_NON_GLOB_METHODS = %i[
4444
children
@@ -171,7 +171,7 @@ class RootPathnameMethods < Base # rubocop:disable Metrics/ClassLength
171171

172172
def_node_matcher :dir_glob?, <<~PATTERN
173173
(send
174-
(const {cbase nil?} :Dir) :glob ...)
174+
(const {cbase nil?} :Dir) DIR_GLOB_METHODS ...)
175175
PATTERN
176176

177177
def_node_matcher :rails_root_pathname?, <<~PATTERN
@@ -190,7 +190,7 @@ def on_send(node)
190190
evidence(node) do |method, path, args, rails_root|
191191
add_offense(node, message: format(MSG, method: method, rails_root: rails_root.source)) do |corrector|
192192
replacement = if dir_glob?(node)
193-
build_path_glob_replacement(path, method)
193+
build_path_glob_replacement(path)
194194
else
195195
build_path_replacement(path, method, args)
196196
end
@@ -217,12 +217,12 @@ def pathname_method(node)
217217
end
218218
end
219219

220-
def build_path_glob_replacement(path, method)
220+
def build_path_glob_replacement(path)
221221
receiver = range_between(path.source_range.begin_pos, path.children.first.loc.selector.end_pos).source
222222

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

225-
"#{receiver}.#{method}(#{argument})"
225+
"#{receiver}.glob(#{argument})"
226226
end
227227

228228
def build_path_replacement(path, method, args)

spec/rubocop/cop/rails/root_pathname_methods_spec.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
RSpec.describe RuboCop::Cop::Rails::RootPathnameMethods, :config do
44
{
5-
Dir: described_class::DIR_METHODS,
5+
Dir: described_class::DIR_NON_GLOB_METHODS,
66
File: described_class::FILE_METHODS,
77
FileTest: described_class::FILE_TEST_METHODS,
88
FileUtils: described_class::FILE_UTILS_METHODS,
99
IO: described_class::FILE_METHODS
1010
}.each do |receiver, methods|
1111
methods.each do |method|
12-
next if method == :glob
13-
1412
it "registers an offense when using `#{receiver}.#{method}(Rails.public_path)` (if arity exists)" do
1513
expect_offense(<<~RUBY, receiver: receiver, method: method)
1614
%{receiver}.%{method}(Rails.public_path)
@@ -143,6 +141,19 @@
143141
end
144142
end
145143

144+
context 'when using Dir.[] on Ruby 2.5 or higher', :ruby25 do
145+
it 'registers offense when using `Dir[Rails.root.join(...)]`' do
146+
expect_offense(<<~RUBY)
147+
Dir[Rails.root.join('spec/support/**/*.rb')]
148+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname` so you can just append `#[]`.
149+
RUBY
150+
151+
expect_correction(<<~RUBY)
152+
Rails.root.glob('spec/support/**/*.rb')
153+
RUBY
154+
end
155+
end
156+
146157
# This is handled by `Rails/RootJoinChain`
147158
it 'does not register an offense when using `File.read(Rails.root.join(...).join(...))`' do
148159
expect_no_offenses(<<~RUBY)

0 commit comments

Comments
 (0)