Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #1389] Handle TypeError caused by passing array literals as arguments to File methods in Rails/RootPathnameMethods cop #1416

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading