File tree 3 files changed +28
-5
lines changed 3 files changed +28
-5
lines changed Original file line number Diff line number Diff line change
1
+ * [ #989 ] ( https://github.com/rubocop/rubocop-rails/pull/989 ) : Fix ` Rails/FilePath ` to detect offenses from complex string interpolation. ([ @r7kamura ] [ ] )
Original file line number Diff line number Diff line change @@ -56,11 +56,8 @@ class FilePath < Base
56
56
57
57
def on_dstr ( node )
58
58
return unless rails_root_nodes? ( node )
59
- return unless node . children . last . str_type?
60
-
61
- last_child_source = node . children . last . source
62
- return unless last_child_source . start_with? ( '.' ) || last_child_source . include? ( File ::SEPARATOR )
63
- return if last_child_source . start_with? ( ':' )
59
+ return if dstr_separated_by_colon? ( node )
60
+ return unless dstr_ending_with_file_extension? ( node ) || dstr_including_file_separator? ( node )
64
61
65
62
register_offense ( node , require_to_s : true )
66
63
end
@@ -119,6 +116,22 @@ def build_message(require_to_s)
119
116
120
117
format ( message_template , to_s : to_s )
121
118
end
119
+
120
+ def dstr_ending_with_file_extension? ( node )
121
+ node . children . last . str_type? && node . children . last . source . start_with? ( '.' )
122
+ end
123
+
124
+ def dstr_including_file_separator? ( node )
125
+ node . children . any? do |child |
126
+ child . str_type? && child . source . include? ( File ::SEPARATOR )
127
+ end
128
+ end
129
+
130
+ def dstr_separated_by_colon? ( node )
131
+ node . children [ 1 ..] . any? do |child |
132
+ child . str_type? && child . source . start_with? ( ':' )
133
+ end
134
+ end
122
135
end
123
136
end
124
137
end
Original file line number Diff line number Diff line change 88
88
end
89
89
end
90
90
91
+ context 'when using Rails.root called by double quoted string that ends with string interpolation' do
92
+ it 'registers an offense' do
93
+ expect_offense ( <<~'RUBY' )
94
+ "#{Rails.root}/a/#{b}"
95
+ ^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to')`.
96
+ RUBY
97
+ end
98
+ end
99
+
91
100
context 'when concat Rails.root and file separator using string interpolation' do
92
101
it 'registers an offense' do
93
102
expect_offense ( <<~'RUBY' )
You can’t perform that action at this time.
0 commit comments