Skip to content

Commit bb03d6b

Browse files
committed
Fix Rails/FilePath cop error with rescued Rails.root
```console echo '"#{Rails.root rescue "."}/config"' | rubocop --stdin bug.rb -d An error occurred while Rails/FilePath cop was inspecting bug.rb:1:0. undefined method `method?' for an instance of RuboCop::AST::RescueNode lib/rubocop/cop/rails/file_path.rb:156:in `autocorrect_slash_after_rails_root_in_dstr' lib/rubocop/cop/rails/file_path.rb:81:in `block in check_for_slash_after_rails_root_in_dstr' ```
1 parent 0bbd218 commit bb03d6b

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)