Skip to content

Commit e4aee6b

Browse files
committed
Fix Rails/FilePath cop error in case of extra operations in Rails.root interpolation
Follow-up to #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.
1 parent c6f869b commit e4aee6b

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/rails/file_path.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +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?
79+
return unless node.children[rails_root_index].children.first.send_type?
8080

8181
register_offense(node, require_to_s: false) do |corrector|
8282
autocorrect_slash_after_rails_root_in_dstr(corrector, node, rails_root_index)

rubocop-rails-2.27.0.gem

110 KB
Binary file not shown.

spec/rubocop/cop/rails/file_path_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,14 @@
407407
end
408408
end
409409

410-
context 'when rescued concat Rails.root' do
411-
it 'does not register an offense' do
410+
context 'when interpolation with `Rails.root` contains other operations' do
411+
it 'does not register an offense for boolean method' do
412+
expect_no_offenses(<<~'RUBY')
413+
"#{Rails.root || '.'}/config"
414+
RUBY
415+
end
416+
417+
it 'does not register an offense for `rescue`' do
412418
expect_no_offenses(<<~'RUBY')
413419
"#{Rails.root rescue '.'}/config"
414420
RUBY

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy