Skip to content

Commit

Permalink
Merge pull request rubocop#1399 from viralpraxis/fix-rails-file-path-…
Browse files Browse the repository at this point in the history
…cop-error-on-extra-operations-in-rails-root-interpolation

Fix `Rails/FilePath` cop error in case of extra operations in `Rails.root` interpolation
  • Loading branch information
koic authored Dec 23, 2024
2 parents c6f869b + e4aee6b commit fecead8
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 fecead8

Please sign in to comment.