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 : false )
66
63
end
@@ -118,6 +115,22 @@ def build_message(require_to_s)
118
115
119
116
format ( message_template , to_s : to_s )
120
117
end
118
+
119
+ def dstr_ending_with_file_extension? ( node )
120
+ node . children . last . str_type? && node . children . last . source . start_with? ( '.' )
121
+ end
122
+
123
+ def dstr_including_file_separator? ( node )
124
+ node . children . any? do |child |
125
+ child . str_type? && child . source . include? ( File ::SEPARATOR )
126
+ end
127
+ end
128
+
129
+ def dstr_separated_by_colon? ( node )
130
+ node . children [ 1 ..] . any? do |child |
131
+ child . str_type? && child . source . start_with? ( ':' )
132
+ end
133
+ end
121
134
end
122
135
end
123
136
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