Skip to content

Commit

Permalink
Fix Rails/FilePath cop error on join method with implicit receiver
Browse files Browse the repository at this point in the history
```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'
```
  • Loading branch information
viralpraxis committed Dec 23, 2024
1 parent fecead8 commit 815d119
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_merge_pull_request_1392_from.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1397](https://github.com/rubocop/rubocop-rails/pull/1397): Fix `Rails/FilePath` cop error on `join` method with implicit receiver. ([@viralpraxis][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/rails/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/rails/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 815d119

Please sign in to comment.