Skip to content

Commit

Permalink
Fix Rails/FilePath cop error in case of extra operations in `Rails.…
Browse files Browse the repository at this point in the history
…root` interpolation

Follow-up to rubocop#1392.
Previous PR didn't fix cases like this

```
"#{Rails.root || '.'}/config"
```

(it makes sense in real code since `Rails.root` [can be nil](https://github.com/rails/rails/blob/main/railties/lib/rails.rb#L65-L67))

I think it's safe to simply check if the entire expression is of `send` type.
  • Loading branch information
viralpraxis committed Dec 21, 2024
1 parent c6f869b commit 940c4fa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1398](https://github.com/rubocop/rubocop-rails/pull/1398): Fix `Rails/FilePath` cop error in case of extra operations in `Rails.root` interpolation. ([@viralpraxis][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +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?
return unless node.children[rails_root_index].children.first.send_type?

register_offense(node, require_to_s: false) do |corrector|
autocorrect_slash_after_rails_root_in_dstr(corrector, node, rails_root_index)
Expand Down
10 changes: 9 additions & 1 deletion spec/rubocop/cop/rails/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,20 @@
end
end

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

context 'with addition operations inside `Rails.root` interpolation expression' do
it 'does not register an offense' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root || '.'}/config"
RUBY
end
end
end
end

0 comments on commit 940c4fa

Please sign in to comment.