Skip to content

Commit

Permalink
Merge pull request #1416 from ydakuka/fix/typeerror_in_the_root_pathn…
Browse files Browse the repository at this point in the history
…ame_methods_cop

[Fix #1389] Handle `TypeError` caused by passing array literals as arguments to `File` methods in `Rails/RootPathnameMethods` cop
  • Loading branch information
koic authored Jan 20, 2025
2 parents e5dce3a + 334e5cc commit ce69ef7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +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][])
7 changes: 6 additions & 1 deletion lib/rubocop/cop/rails/root_pathname_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ def build_path_replacement(path, method, args)
end

replacement = "#{path_replacement}.#{method}"
replacement += "(#{args.map(&:source).join(', ')})" unless args.empty?

if args.any?
formatted_args = args.map { |arg| arg.array_type? ? "*#{arg.source}" : arg.source }
replacement += "(#{formatted_args.join(', ')})"
end

replacement
end

Expand Down
66 changes: 66 additions & 0 deletions spec/rubocop/cop/rails/root_pathname_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,70 @@
file = Rails.root.join('docs', 'invoice.pdf').open
RUBY
end

it 'registers an offense when using only [] syntax' do
expect_offense(<<~RUBY)
File.join(Rails.root, ['app', 'models'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*['app', 'models'])`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join(*['app', 'models'])
RUBY
end

it 'registers an offense when using a leading string and an array using [] syntax' do
expect_offense(<<~RUBY)
File.join(Rails.root, "app", ["models", "goober"])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join("app", *["models", "goober"])`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join("app", *["models", "goober"])
RUBY
end

it 'registers an offense when using an array using [] syntax and a trailing string' do
expect_offense(<<~RUBY)
File.join(Rails.root, ["app", "models"], "goober")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*["app", "models"], "goober")`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join(*["app", "models"], "goober")
RUBY
end

it 'registers an offense when using only %w[] syntax' do
expect_offense(<<~RUBY)
File.join(Rails.root, %w[app models])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*%w[app models])`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join(*%w[app models])
RUBY
end

it 'registers an offense when using a leading string and an array using %w[] syntax' do
expect_offense(<<~RUBY)
File.join(Rails.root, "app", %w[models goober])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join("app", *%w[models goober])`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join("app", *%w[models goober])
RUBY
end

it 'registers an offense when using an array using %w[] syntax and a trailing string' do
expect_offense(<<~RUBY)
File.join(Rails.root, %w[app models], "goober")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Rails.root` is a `Pathname`, so you can use `Rails.root.join(*%w[app models], "goober")`.
RUBY

expect_correction(<<~RUBY)
Rails.root.join(*%w[app models], "goober")
RUBY
end
end

0 comments on commit ce69ef7

Please sign in to comment.