@@ -21,7 +21,7 @@ module Rails
21
21
# "#{Rails.root}/app/models/goober"
22
22
#
23
23
# # good
24
- # Rails.root.join('app/models/goober').to_s
24
+ # Rails.root.join('app/models/goober').to_path
25
25
#
26
26
# @example EnforcedStyle: arguments
27
27
# # bad
@@ -35,16 +35,16 @@ module Rails
35
35
# "#{Rails.root}/app/models/goober"
36
36
#
37
37
# # good
38
- # Rails.root.join('app', 'models', 'goober').to_s
38
+ # Rails.root.join('app', 'models', 'goober').to_path
39
39
#
40
40
class FilePath < Base
41
41
extend AutoCorrector
42
42
43
43
include ConfigurableEnforcedStyle
44
44
include RangeHelp
45
45
46
- MSG_SLASHES = 'Prefer `Rails.root.join(\'path/to\')%<to_s >s`.'
47
- MSG_ARGUMENTS = 'Prefer `Rails.root.join(\'path\', \'to\')%<to_s >s`.'
46
+ MSG_SLASHES = 'Prefer `Rails.root.join(\'path/to\')%<to_path >s`.'
47
+ MSG_ARGUMENTS = 'Prefer `Rails.root.join(\'path\', \'to\')%<to_path >s`.'
48
48
RESTRICT_ON_SEND = %i[ join ] . freeze
49
49
50
50
def_node_matcher :file_join_nodes? , <<~PATTERN
@@ -83,7 +83,7 @@ def check_for_slash_after_rails_root_in_dstr(node)
83
83
return unless slash_node &.str_type? && slash_node . source . start_with? ( File ::SEPARATOR )
84
84
return unless node . children [ rails_root_index ] . children . first . send_type?
85
85
86
- register_offense ( node , require_to_s : false ) do |corrector |
86
+ register_offense ( node , require_to_path : false ) do |corrector |
87
87
autocorrect_slash_after_rails_root_in_dstr ( corrector , node , rails_root_index )
88
88
end
89
89
end
@@ -93,7 +93,7 @@ def check_for_extension_after_rails_root_join_in_dstr(node)
93
93
extension_node = node . children [ rails_root_index + 1 ]
94
94
return unless extension_node? ( extension_node )
95
95
96
- register_offense ( node , require_to_s : false ) do |corrector |
96
+ register_offense ( node , require_to_path : false ) do |corrector |
97
97
autocorrect_extension_after_rails_root_join_in_dstr ( corrector , node , rails_root_index , extension_node )
98
98
end
99
99
end
@@ -102,7 +102,7 @@ def check_for_file_join_with_rails_root(node)
102
102
return unless file_join_nodes? ( node )
103
103
return unless valid_arguments_for_file_join_with_rails_root? ( node . arguments )
104
104
105
- register_offense ( node , require_to_s : true ) do |corrector |
105
+ register_offense ( node , require_to_path : true ) do |corrector |
106
106
autocorrect_file_join ( corrector , node ) unless node . first_argument . array_type?
107
107
end
108
108
end
@@ -113,7 +113,7 @@ def check_for_rails_root_join_with_string_arguments(node)
113
113
return unless rails_root_join_nodes? ( node )
114
114
return unless valid_string_arguments_for_rails_root_join? ( node . arguments )
115
115
116
- register_offense ( node , require_to_s : false ) do |corrector |
116
+ register_offense ( node , require_to_path : false ) do |corrector |
117
117
autocorrect_rails_root_join_with_string_arguments ( corrector , node )
118
118
end
119
119
end
@@ -124,7 +124,7 @@ def check_for_rails_root_join_with_slash_separated_path(node)
124
124
return unless rails_root_join_nodes? ( node )
125
125
return unless valid_slash_separated_path_for_rails_root_join? ( node . arguments )
126
126
127
- register_offense ( node , require_to_s : false ) do |corrector |
127
+ register_offense ( node , require_to_path : false ) do |corrector |
128
128
autocorrect_rails_root_join_with_slash_separated_path ( corrector , node )
129
129
end
130
130
end
@@ -160,20 +160,20 @@ def string_with_leading_slash?(node)
160
160
node . str_type? && node . value . start_with? ( File ::SEPARATOR )
161
161
end
162
162
163
- def register_offense ( node , require_to_s :, &block )
163
+ def register_offense ( node , require_to_path :, &block )
164
164
line_range = node . loc . column ...node . loc . last_column
165
165
source_range = source_range ( processed_source . buffer , node . first_line , line_range )
166
166
167
- message = build_message ( require_to_s )
167
+ message = build_message ( require_to_path )
168
168
169
169
add_offense ( source_range , message : message , &block )
170
170
end
171
171
172
- def build_message ( require_to_s )
172
+ def build_message ( require_to_path )
173
173
message_template = style == :arguments ? MSG_ARGUMENTS : MSG_SLASHES
174
- to_s = require_to_s ? '.to_s ' : ''
174
+ to_path = require_to_path ? '.to_path ' : ''
175
175
176
- format ( message_template , to_s : to_s )
176
+ format ( message_template , to_path : to_path )
177
177
end
178
178
179
179
def dstr_separated_by_colon? ( node )
@@ -205,7 +205,7 @@ def autocorrect_file_join(corrector, node)
205
205
replace_receiver_with_rails_root ( corrector , node )
206
206
remove_first_argument_with_comma ( corrector , node )
207
207
process_arguments ( corrector , node . arguments )
208
- append_to_string_conversion ( corrector , node )
208
+ append_to_pathtring_conversion ( corrector , node )
209
209
end
210
210
211
211
def replace_receiver_with_rails_root ( corrector , node )
@@ -234,8 +234,8 @@ def process_arguments(corrector, arguments)
234
234
end
235
235
end
236
236
237
- def append_to_string_conversion ( corrector , node )
238
- corrector . insert_after ( node , '.to_s ' )
237
+ def append_to_pathtring_conversion ( corrector , node )
238
+ corrector . insert_after ( node , '.to_path ' )
239
239
end
240
240
241
241
def autocorrect_rails_root_join_with_string_arguments ( corrector , node )
0 commit comments