Skip to content

Commit 3677f7b

Browse files
committed
[Fix #1389] Handle TypeError caused by an array in Rails/FilePath cop
1 parent 158cd38 commit 3677f7b

File tree

4 files changed

+180
-3
lines changed

4 files changed

+180
-3
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ InternalAffairs/NodeDestructuring:
2525
# Offense count: 10
2626
# Configuration parameters: CountComments, CountAsOne.
2727
Metrics/ClassLength:
28-
Max: 163
28+
Max: 179
2929

3030
# Offense count: 41
3131
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1389](https://github.com/rubocop/rubocop-rails/issues/1389): Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/FilePath` cop. ([@ydakuka][])

lib/rubocop/cop/rails/file_path.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,17 @@ def autocorrect_extension_after_rails_root_join_in_dstr(corrector, node, rails_r
173173
end
174174

175175
def autocorrect_file_join(corrector, node)
176+
replace_receiver_with_rails_root(corrector, node)
177+
remove_first_argument_with_comma(corrector, node)
178+
process_arguments(corrector, node.arguments)
179+
append_to_string_conversion(corrector, node)
180+
end
181+
182+
def replace_receiver_with_rails_root(corrector, node)
176183
corrector.replace(node.receiver, 'Rails.root')
184+
end
185+
186+
def remove_first_argument_with_comma(corrector, node)
177187
corrector.remove(
178188
range_with_surrounding_space(
179189
range_with_surrounding_comma(
@@ -183,9 +193,19 @@ def autocorrect_file_join(corrector, node)
183193
side: :right
184194
)
185195
)
186-
node.arguments.filter(&:str_type?).each do |argument|
187-
corrector.replace(argument, argument.value.delete_prefix('/').inspect)
196+
end
197+
198+
def process_arguments(corrector, arguments)
199+
arguments.each do |argument|
200+
if argument.str_type?
201+
corrector.replace(argument, argument.value.delete_prefix('/').inspect)
202+
elsif argument.array_type?
203+
corrector.replace(argument, "*#{argument.source}")
204+
end
188205
end
206+
end
207+
208+
def append_to_string_conversion(corrector, node)
189209
corrector.insert_after(node, '.to_s')
190210
end
191211

spec/rubocop/cop/rails/file_path_spec.rb

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,84 @@
244244
RUBY
245245
end
246246
end
247+
248+
context 'when using only [] syntax' do
249+
it 'registers an offense once' do
250+
expect_offense(<<~RUBY)
251+
File.join(Rails.root, ['app', 'models'])
252+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
253+
RUBY
254+
255+
expect_correction(<<~RUBY)
256+
Rails.root.join(*['app', 'models']).to_s
257+
RUBY
258+
end
259+
end
260+
261+
context 'with a leading string and an array using [] syntax' do
262+
it 'registers an offense once' do
263+
expect_offense(<<~RUBY)
264+
File.join(Rails.root, "app", ["models", "goober"])
265+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
266+
RUBY
267+
268+
expect_correction(<<~RUBY)
269+
Rails.root.join("app", *["models", "goober"]).to_s
270+
RUBY
271+
end
272+
end
273+
274+
context 'with an array using [] syntax and a trailing string' do
275+
it 'registers an offense once' do
276+
expect_offense(<<~RUBY)
277+
File.join(Rails.root, ["app", "models"], "goober")
278+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
279+
RUBY
280+
281+
expect_correction(<<~RUBY)
282+
Rails.root.join(*["app", "models"], "goober").to_s
283+
RUBY
284+
end
285+
end
286+
287+
context 'when using only %w[] syntax' do
288+
it 'registers an offense once' do
289+
expect_offense(<<~RUBY)
290+
File.join(Rails.root, %w[app models])
291+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
292+
RUBY
293+
294+
expect_correction(<<~RUBY)
295+
Rails.root.join(*%w[app models]).to_s
296+
RUBY
297+
end
298+
end
299+
300+
context 'with a leading string and an array using %w[] syntax' do
301+
it 'registers an offense once' do
302+
expect_offense(<<~RUBY)
303+
File.join(Rails.root, "app", %w[models goober])
304+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
305+
RUBY
306+
307+
expect_correction(<<~RUBY)
308+
Rails.root.join("app", *%w[models goober]).to_s
309+
RUBY
310+
end
311+
end
312+
313+
context 'with an array using %w[] syntax and a trailing string' do
314+
it 'registers an offense once' do
315+
expect_offense(<<~RUBY)
316+
File.join(Rails.root, %w[app models], "goober")
317+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
318+
RUBY
319+
320+
expect_correction(<<~RUBY)
321+
Rails.root.join(*%w[app models], "goober").to_s
322+
RUBY
323+
end
324+
end
247325
end
248326

249327
context 'when EnforcedStyle is `arguments`' do
@@ -436,5 +514,83 @@
436514
RUBY
437515
end
438516
end
517+
518+
context 'when using only [] syntax' do
519+
it 'registers an offense once' do
520+
expect_offense(<<~RUBY)
521+
File.join(Rails.root, ['app', 'models'])
522+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
523+
RUBY
524+
525+
expect_correction(<<~RUBY)
526+
Rails.root.join(*['app', 'models']).to_s
527+
RUBY
528+
end
529+
end
530+
531+
context 'with a leading string and an array using [] syntax' do
532+
it 'registers an offense once' do
533+
expect_offense(<<~RUBY)
534+
File.join(Rails.root, "app", ["models", "goober"])
535+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
536+
RUBY
537+
538+
expect_correction(<<~RUBY)
539+
Rails.root.join("app", *["models", "goober"]).to_s
540+
RUBY
541+
end
542+
end
543+
544+
context 'with an array using [] syntax and a trailing string' do
545+
it 'registers an offense once' do
546+
expect_offense(<<~RUBY)
547+
File.join(Rails.root, ["app", "models"], "goober")
548+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
549+
RUBY
550+
551+
expect_correction(<<~RUBY)
552+
Rails.root.join(*["app", "models"], "goober").to_s
553+
RUBY
554+
end
555+
end
556+
557+
context 'when using only %w[] syntax' do
558+
it 'registers an offense once' do
559+
expect_offense(<<~RUBY)
560+
File.join(Rails.root, %w[app models])
561+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
562+
RUBY
563+
564+
expect_correction(<<~RUBY)
565+
Rails.root.join(*%w[app models]).to_s
566+
RUBY
567+
end
568+
end
569+
570+
context 'with a leading string and an array using %w[] syntax' do
571+
it 'registers an offense once' do
572+
expect_offense(<<~RUBY)
573+
File.join(Rails.root, "app", %w[models goober])
574+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
575+
RUBY
576+
577+
expect_correction(<<~RUBY)
578+
Rails.root.join("app", *%w[models goober]).to_s
579+
RUBY
580+
end
581+
end
582+
583+
context 'with an array using %w[] syntax and a trailing string' do
584+
it 'registers an offense once' do
585+
expect_offense(<<~RUBY)
586+
File.join(Rails.root, %w[app models], "goober")
587+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path', 'to').to_s`.
588+
RUBY
589+
590+
expect_correction(<<~RUBY)
591+
Rails.root.join(*%w[app models], "goober").to_s
592+
RUBY
593+
end
594+
end
439595
end
440596
end

0 commit comments

Comments
 (0)