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 22, 2024
1 parent c6f869b commit e4aee6b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 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
Binary file added rubocop-rails-2.27.0.gem
Binary file not shown.
10 changes: 8 additions & 2 deletions spec/rubocop/cop/rails/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,14 @@
end
end

context 'when rescued concat Rails.root' do
it 'does not register an offense' do
context 'when interpolation with `Rails.root` contains other operations' do
it 'does not register an offense for boolean method' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root || '.'}/config"
RUBY
end

it 'does not register an offense for `rescue`' do
expect_no_offenses(<<~'RUBY')
"#{Rails.root rescue '.'}/config"
RUBY
Expand Down

0 comments on commit e4aee6b

Please sign in to comment.