Skip to content

Commit 81de91b

Browse files
committed
Fix a false-positive for Rails/RootPathnameMethods on Ruby 2.4 or lower
1 parent 24ff064 commit 81de91b

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1004](https://github.com/rubocop/rubocop-rails/pull/1004): Fix a false-positive for `Rails/RootPathnameMethods` on Ruby 2.4 or lower. ([@r7kamura][])

lib/rubocop/cop/rails/root_pathname_methods.rb

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,28 @@ module Rails
3232
# Rails.root.join('db', 'schema.rb').write(content)
3333
# Rails.root.join('db', 'schema.rb').binwrite(content)
3434
#
35-
class RootPathnameMethods < Base
35+
class RootPathnameMethods < Base # rubocop:disable Metrics/ClassLength
3636
extend AutoCorrector
3737
include RangeHelp
3838

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

41-
DIR_METHODS = %i[children delete each_child empty? entries exist? glob mkdir open rmdir unlink].to_set.freeze
41+
DIR_GLOB_METHODS = %i[glob].to_set.freeze
42+
43+
DIR_NON_GLOB_METHODS = %i[
44+
children
45+
delete
46+
each_child
47+
empty?
48+
entries
49+
exist?
50+
mkdir
51+
open
52+
rmdir
53+
unlink
54+
].to_set.freeze
55+
56+
DIR_METHODS = (DIR_GLOB_METHODS + DIR_NON_GLOB_METHODS).freeze
4257

4358
FILE_METHODS = %i[
4459
atime
@@ -134,7 +149,8 @@ class RootPathnameMethods < Base
134149

135150
RESTRICT_ON_SEND = (DIR_METHODS + FILE_METHODS + FILE_TEST_METHODS + FILE_UTILS_METHODS).to_set.freeze
136151

137-
def_node_matcher :pathname_method, <<~PATTERN
152+
# @!method pathname_method_for_ruby_2_5_or_higher(node)
153+
def_node_matcher :pathname_method_for_ruby_2_5_or_higher, <<~PATTERN
138154
{
139155
(send (const {nil? cbase} :Dir) $DIR_METHODS $_ $...)
140156
(send (const {nil? cbase} {:IO :File}) $FILE_METHODS $_ $...)
@@ -143,6 +159,16 @@ class RootPathnameMethods < Base
143159
}
144160
PATTERN
145161

162+
# @!method pathname_method_for_ruby_2_4_or_lower(node)
163+
def_node_matcher :pathname_method_for_ruby_2_4_or_lower, <<~PATTERN
164+
{
165+
(send (const {nil? cbase} :Dir) $DIR_NON_GLOB_METHODS $_ $...)
166+
(send (const {nil? cbase} {:IO :File}) $FILE_METHODS $_ $...)
167+
(send (const {nil? cbase} :FileTest) $FILE_TEST_METHODS $_ $...)
168+
(send (const {nil? cbase} :FileUtils) $FILE_UTILS_METHODS $_ $...)
169+
}
170+
PATTERN
171+
146172
def_node_matcher :dir_glob?, <<~PATTERN
147173
(send
148174
(const {cbase nil?} :Dir) :glob ...)
@@ -183,6 +209,14 @@ def evidence(node)
183209
yield(method, path, args, rails_root)
184210
end
185211

212+
def pathname_method(node)
213+
if target_ruby_version >= 2.5
214+
pathname_method_for_ruby_2_5_or_higher(node)
215+
else
216+
pathname_method_for_ruby_2_4_or_lower(node)
217+
end
218+
end
219+
186220
def build_path_glob_replacement(path, method)
187221
receiver = range_between(path.source_range.begin_pos, path.children.first.loc.selector.end_pos).source
188222

spec/rubocop/cop/rails/root_pathname_methods_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@
4646
end
4747
end
4848

49-
context 'when using `Dir.glob`' do
49+
context 'when using `Dir.glob` on Ruby 2.4 or lower', :ruby24 do
50+
it 'does not registers an offense' do
51+
expect_no_offenses(<<~RUBY)
52+
Dir.glob(Rails.root.join('**/*.rb'))
53+
RUBY
54+
end
55+
end
56+
57+
context 'when using `Dir.glob` on Ruby 2.5 or higher', :ruby25 do
5058
it "registers an offense when using `Dir.glob(Rails.root.join('**/*.rb'))`" do
5159
expect_offense(<<~RUBY)
5260
Dir.glob(Rails.root.join('**/*.rb'))

0 commit comments

Comments
 (0)