From 74892f1cfa880a4e3a2d49e303ca669dd0e94696 Mon Sep 17 00:00:00 2001 From: viralpraxis Date: Thu, 12 Dec 2024 21:18:32 +0300 Subject: [PATCH] Fix `Rails/FilePath` cop error with rescued `Rails.root` --- lib/rubocop/cop/rails/file_path.rb | 1 + spec/rubocop/cop/rails/file_path_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/lib/rubocop/cop/rails/file_path.rb b/lib/rubocop/cop/rails/file_path.rb index 02459031a9..95aece36be 100644 --- a/lib/rubocop/cop/rails/file_path.rb +++ b/lib/rubocop/cop/rails/file_path.rb @@ -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) diff --git a/spec/rubocop/cop/rails/file_path_spec.rb b/spec/rubocop/cop/rails/file_path_spec.rb index 3bec9ded2c..2a21a5d0e9 100644 --- a/spec/rubocop/cop/rails/file_path_spec.rb +++ b/spec/rubocop/cop/rails/file_path_spec.rb @@ -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