Skip to content

Commit ce69ef7

Browse files
authored
Merge pull request #1416 from ydakuka/fix/typeerror_in_the_root_pathname_methods_cop
[Fix #1389] Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/RootPathnameMethods` cop
2 parents e5dce3a + 334e5cc commit ce69ef7

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed
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/RootPathnameMethods` cop. ([@ydakuka][])

lib/rubocop/cop/rails/root_pathname_methods.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ def build_path_replacement(path, method, args)
237237
end
238238

239239
replacement = "#{path_replacement}.#{method}"
240-
replacement += "(#{args.map(&:source).join(', ')})" unless args.empty?
240+
241+
if args.any?
242+
formatted_args = args.map { |arg| arg.array_type? ? "*#{arg.source}" : arg.source }
243+
replacement += "(#{formatted_args.join(', ')})"
244+
end
245+
241246
replacement
242247
end
243248

spec/rubocop/cop/rails/root_pathname_methods_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,70 @@
210210
file = Rails.root.join('docs', 'invoice.pdf').open
211211
RUBY
212212
end
213+
214+
it 'registers an offense when using only [] syntax' do
215+
expect_offense(<<~RUBY)
216+
File.join(Rails.root, ['app', 'models'])
217+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*['app', 'models'])`.
218+
RUBY
219+
220+
expect_correction(<<~RUBY)
221+
Rails.root.join(*['app', 'models'])
222+
RUBY
223+
end
224+
225+
it 'registers an offense when using a leading string and an array using [] syntax' do
226+
expect_offense(<<~RUBY)
227+
File.join(Rails.root, "app", ["models", "goober"])
228+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join("app", *["models", "goober"])`.
229+
RUBY
230+
231+
expect_correction(<<~RUBY)
232+
Rails.root.join("app", *["models", "goober"])
233+
RUBY
234+
end
235+
236+
it 'registers an offense when using an array using [] syntax and a trailing string' do
237+
expect_offense(<<~RUBY)
238+
File.join(Rails.root, ["app", "models"], "goober")
239+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*["app", "models"], "goober")`.
240+
RUBY
241+
242+
expect_correction(<<~RUBY)
243+
Rails.root.join(*["app", "models"], "goober")
244+
RUBY
245+
end
246+
247+
it 'registers an offense when using only %w[] syntax' do
248+
expect_offense(<<~RUBY)
249+
File.join(Rails.root, %w[app models])
250+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*%w[app models])`.
251+
RUBY
252+
253+
expect_correction(<<~RUBY)
254+
Rails.root.join(*%w[app models])
255+
RUBY
256+
end
257+
258+
it 'registers an offense when using a leading string and an array using %w[] syntax' do
259+
expect_offense(<<~RUBY)
260+
File.join(Rails.root, "app", %w[models goober])
261+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join("app", *%w[models goober])`.
262+
RUBY
263+
264+
expect_correction(<<~RUBY)
265+
Rails.root.join("app", *%w[models goober])
266+
RUBY
267+
end
268+
269+
it 'registers an offense when using an array using %w[] syntax and a trailing string' do
270+
expect_offense(<<~RUBY)
271+
File.join(Rails.root, %w[app models], "goober")
272+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*%w[app models], "goober")`.
273+
RUBY
274+
275+
expect_correction(<<~RUBY)
276+
Rails.root.join(*%w[app models], "goober")
277+
RUBY
278+
end
213279
end

0 commit comments

Comments
 (0)