Skip to content

Commit 4a00257

Browse files
authored
Merge pull request #1326 from Earlopain/wrong-autocorrect-for-rails-file-path
Fix wrong autocorrect for `Rails/FilePath` when passing an array to `File.join`
2 parents 38cec18 + 4827792 commit 4a00257

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1326](https://github.com/rubocop/rubocop-rails/pull/1326): Fix wrong autocorrect for `Rails/FilePath` when passing an array to `File.join`. ([@earlopain][])

lib/rubocop/cop/rails/file_path.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def check_for_file_join_with_rails_root(node)
9797
return unless node.arguments.any? { |e| rails_root_nodes?(e) }
9898

9999
register_offense(node, require_to_s: true) do |corrector|
100-
autocorrect_file_join(corrector, node)
100+
autocorrect_file_join(corrector, node) unless node.first_argument.array_type?
101101
end
102102
end
103103

spec/rubocop/cop/rails/file_path_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,26 @@
114114
end
115115
end
116116

117+
context 'when using File.join with an array' do
118+
it 'registers an offense' do
119+
expect_offense(<<~RUBY)
120+
File.join([Rails.root, 'foo'])
121+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
122+
RUBY
123+
124+
expect_no_corrections
125+
end
126+
127+
it 'registers an offense for nested arrays' do
128+
expect_offense(<<~RUBY)
129+
File.join([Rails.root, 'foo', ['bar']])
130+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `Rails.root.join('path/to').to_s`.
131+
RUBY
132+
133+
expect_no_corrections
134+
end
135+
end
136+
117137
context 'when using Rails.root.join with slash separated path string' do
118138
it 'does not register an offense' do
119139
expect_no_offenses("Rails.root.join('app/models/goober')")

0 commit comments

Comments
 (0)