From 815d1198f96762082c23823bd9a74cac049f4a85 Mon Sep 17 00:00:00 2001 From: viralpraxis Date: Sat, 21 Dec 2024 16:06:42 +0300 Subject: [PATCH] Fix `Rails/FilePath` cop error on `join` method with implicit receiver ```console echo 'join(Rails.root, path)' | rubocop --only Rails/FilePath --stdin bug.rb -d n error occurred while Rails/FilePath cop was inspecting bug.rb:1:0. undefined method `each_node' for nil lib/rubocop/cop/rails/file_path.rb:54:in `rails_root_nodes?' lib/rubocop/cop/rails/file_path.rb:60:in `rails_root_join_nodes?' lib/rubocop/cop/rails/file_path.rb:108:in `check_for_rails_root_join_with_string_arguments' lib/rubocop/cop/rails/file_path.rb:70:in `on_send' ``` --- changelog/fix_merge_pull_request_1392_from.md | 1 + lib/rubocop/cop/rails/file_path.rb | 2 ++ spec/rubocop/cop/rails/file_path_spec.rb | 16 ++++++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 changelog/fix_merge_pull_request_1392_from.md diff --git a/changelog/fix_merge_pull_request_1392_from.md b/changelog/fix_merge_pull_request_1392_from.md new file mode 100644 index 0000000000..887a3d16e5 --- /dev/null +++ b/changelog/fix_merge_pull_request_1392_from.md @@ -0,0 +1 @@ +* [#1397](https://github.com/rubocop/rubocop-rails/pull/1397): Fix `Rails/FilePath` cop error on `join` method with implicit receiver. ([@viralpraxis][]) diff --git a/lib/rubocop/cop/rails/file_path.rb b/lib/rubocop/cop/rails/file_path.rb index 4bbbc92f6f..9c19c5e525 100644 --- a/lib/rubocop/cop/rails/file_path.rb +++ b/lib/rubocop/cop/rails/file_path.rb @@ -66,6 +66,8 @@ def on_dstr(node) def on_send(node) check_for_file_join_with_rails_root(node) + return unless node.receiver + check_for_rails_root_join_with_slash_separated_path(node) check_for_rails_root_join_with_string_arguments(node) end diff --git a/spec/rubocop/cop/rails/file_path_spec.rb b/spec/rubocop/cop/rails/file_path_spec.rb index 27db8640c4..04d4cfd4e9 100644 --- a/spec/rubocop/cop/rails/file_path_spec.rb +++ b/spec/rubocop/cop/rails/file_path_spec.rb @@ -236,6 +236,14 @@ RUBY end end + + context 'with `join` method with implicit receiver' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + join(Rails.root, path) + RUBY + end + end end context 'when EnforcedStyle is `arguments`' do @@ -420,5 +428,13 @@ RUBY end end + + context 'with `join` method with implicit receiver' do + it 'does not register an offense' do + expect_no_offenses(<<~RUBY) + join(Rails.root, path) + RUBY + end + end end end