Skip to content

Commit

Permalink
Fix Rails/FilePath cop error with rescued Rails.root
Browse files Browse the repository at this point in the history
```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'
```
  • Loading branch information
viralpraxis committed Dec 13, 2024
1 parent 0bbd218 commit bb03d6b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1392](https://github.com/rubocop/rubocop-rails/pull/1392): Fix `Rails/FilePath` cop error with rescued `Rails.root`. ([@viralpraxis][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def check_for_slash_after_rails_root_in_dstr(node)
rails_root_index = find_rails_root_index(node)
slash_node = node.children[rails_root_index + 1]
return unless slash_node&.str_type? && slash_node.source.start_with?(File::SEPARATOR)
return if node.children[rails_root_index].each_descendant(:rescue).any?

register_offense(node, require_to_s: false) do |corrector|
autocorrect_slash_after_rails_root_in_dstr(corrector, node, rails_root_index)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,13 @@
RUBY
end
end

context 'when rescued concat Rails.root' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root rescue '.'}/config"
RUBY
end
end
end
end

0 comments on commit bb03d6b

Please sign in to comment.