@@ -32,13 +32,28 @@ module Rails
32
32
# Rails.root.join('db', 'schema.rb').write(content)
33
33
# Rails.root.join('db', 'schema.rb').binwrite(content)
34
34
#
35
- class RootPathnameMethods < Base
35
+ class RootPathnameMethods < Base # rubocop:disable Metrics/ClassLength
36
36
extend AutoCorrector
37
37
include RangeHelp
38
38
39
39
MSG = '`%<rails_root>s` is a `Pathname` so you can just append `#%<method>s`.'
40
40
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
42
57
43
58
FILE_METHODS = %i[
44
59
atime
@@ -134,7 +149,8 @@ class RootPathnameMethods < Base
134
149
135
150
RESTRICT_ON_SEND = ( DIR_METHODS + FILE_METHODS + FILE_TEST_METHODS + FILE_UTILS_METHODS ) . to_set . freeze
136
151
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
138
154
{
139
155
(send (const {nil? cbase} :Dir) $DIR_METHODS $_ $...)
140
156
(send (const {nil? cbase} {:IO :File}) $FILE_METHODS $_ $...)
@@ -143,6 +159,16 @@ class RootPathnameMethods < Base
143
159
}
144
160
PATTERN
145
161
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
+
146
172
def_node_matcher :dir_glob? , <<~PATTERN
147
173
(send
148
174
(const {cbase nil?} :Dir) :glob ...)
@@ -183,6 +209,14 @@ def evidence(node)
183
209
yield ( method , path , args , rails_root )
184
210
end
185
211
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
+
186
220
def build_path_glob_replacement ( path , method )
187
221
receiver = range_between ( path . source_range . begin_pos , path . children . first . loc . selector . end_pos ) . source
188
222
0 commit comments