@@ -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
0 commit comments