Skip to content

Commit c6f869b

Browse files
authored
Merge pull request #1392 from viralpraxis/fix-rails-file-path-cop-error-with-rescued-rails-root
Fix `Rails/FilePath` cop error with rescued `Rails.root`
2 parents 0bbd218 + bb03d6b commit c6f869b

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1392](https://github.com/rubocop/rubocop-rails/pull/1392): Fix `Rails/FilePath` cop error with rescued `Rails.root`. ([@viralpraxis][])

lib/rubocop/cop/rails/file_path.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def check_for_slash_after_rails_root_in_dstr(node)
7676
rails_root_index = find_rails_root_index(node)
7777
slash_node = node.children[rails_root_index + 1]
7878
return unless slash_node&.str_type? && slash_node.source.start_with?(File::SEPARATOR)
79+
return if node.children[rails_root_index].each_descendant(:rescue).any?
7980

8081
register_offense(node, require_to_s: false) do |corrector|
8182
autocorrect_slash_after_rails_root_in_dstr(corrector, node, rails_root_index)

spec/rubocop/cop/rails/file_path_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,5 +406,13 @@
406406
RUBY
407407
end
408408
end
409+
410+
context 'when rescued concat Rails.root' do
411+
it 'does not register an offense' do
412+
expect_no_offenses(<<~'RUBY')
413+
"#{Rails.root rescue '.'}/config"
414+
RUBY
415+
end
416+
end
409417
end
410418
end

0 commit comments

Comments
 (0)