Skip to content

Commit

Permalink
[Fix #1071] Fix an error occurring in the Rails/FilePath cop when Fil…
Browse files Browse the repository at this point in the history
…e.join is used with a variable
  • Loading branch information
ydakuka committed Feb 2, 2025
1 parent 4bec0c3 commit e0a85cc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ InternalAffairs/OnSendWithoutOnCSend:
# Offense count: 10
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 179
Max: 163

# Offense count: 41
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1071](https://github.com/rubocop/rubocop-rails/issues/1071): Fix an error occurring in the `Rails/FilePath` cop when `File.join` is used with a variable. ([@ydakuka][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/rails/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module Rails
# # good
# Rails.root.join('app', 'models', 'goober').to_s
#
class FilePath < Base
class FilePath < Base # rubocop:disable Metrics/ClassLength
extend AutoCorrector

include ConfigurableEnforcedStyle
Expand Down Expand Up @@ -98,6 +98,7 @@ def check_for_extension_after_rails_root_join_in_dstr(node)
def check_for_file_join_with_rails_root(node)
return unless file_join_nodes?(node)
return unless node.arguments.any? { |e| rails_root_nodes?(e) }
return if node.arguments.any?(&:variable?)

register_offense(node, require_to_s: true) do |corrector|
autocorrect_file_join(corrector, node) unless node.first_argument.array_type?
Expand Down
66 changes: 66 additions & 0 deletions spec/rubocop/cop/rails/file_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,72 @@
RUBY
end
end

context 'when using File.join with a local variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
default_path = '/models'
File.join(Rails.root, 'app', default_path)
RUBY
end
end

context 'when using File.join with an instance variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app', @default_path)
RUBY
end
end

context 'when using File.join with a class variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app', @@default_path)
RUBY
end
end

context 'when using File.join with a global variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
File.join(Rails.root, 'app', $default_path)
RUBY
end
end

context 'when using Rails.root.join with a local variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
default_path = '/models'
Rails.root.join(Rails.root, 'app', default_path)
RUBY
end
end

context 'when using Rails.root.join with an instance variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app', @default_path)
RUBY
end
end

context 'when using Rails.root.join with a class variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app', @@default_path)
RUBY
end
end

context 'when using Rails.root.join with a global variable' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Rails.root.join(Rails.root, 'app', $default_path)
RUBY
end
end
end

context 'when EnforcedStyle is `arguments`' do
Expand Down

0 comments on commit e0a85cc

Please sign in to comment.