diff --git a/changelog/fix_rails_file_path_cop_error_on_extra_operations_in_rails_root_interpolation.md b/changelog/fix_rails_file_path_cop_error_on_extra_operations_in_rails_root_interpolation.md new file mode 100644 index 0000000000..5d8f66c438 --- /dev/null +++ b/changelog/fix_rails_file_path_cop_error_on_extra_operations_in_rails_root_interpolation.md @@ -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][]) diff --git a/lib/rubocop/cop/rails/file_path.rb b/lib/rubocop/cop/rails/file_path.rb index 95aece36be..4bbbc92f6f 100644 --- a/lib/rubocop/cop/rails/file_path.rb +++ b/lib/rubocop/cop/rails/file_path.rb @@ -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) diff --git a/rubocop-rails-2.27.0.gem b/rubocop-rails-2.27.0.gem new file mode 100644 index 0000000000..f668612480 Binary files /dev/null and b/rubocop-rails-2.27.0.gem differ diff --git a/spec/rubocop/cop/rails/file_path_spec.rb b/spec/rubocop/cop/rails/file_path_spec.rb index 2a21a5d0e9..27db8640c4 100644 --- a/spec/rubocop/cop/rails/file_path_spec.rb +++ b/spec/rubocop/cop/rails/file_path_spec.rb @@ -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