From e0a85ccbdeed1d87b6984147ae0ad2de5a9f61b9 Mon Sep 17 00:00:00 2001 From: Yauheni Dakuka Date: Mon, 27 Jan 2025 03:20:19 +0400 Subject: [PATCH] [Fix rubocop#1071] Fix an error occurring in the Rails/FilePath cop when File.join is used with a variable --- .rubocop_todo.yml | 2 +- ..._when_file_join_is_used_with_a_variable.md | 1 + lib/rubocop/cop/rails/file_path.rb | 3 +- spec/rubocop/cop/rails/file_path_spec.rb | 66 +++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_an_error_occurring_in_the_rails_file_path_cop_when_file_join_is_used_with_a_variable.md diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 098642cde0..4c9f429a3b 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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. diff --git a/changelog/fix_an_error_occurring_in_the_rails_file_path_cop_when_file_join_is_used_with_a_variable.md b/changelog/fix_an_error_occurring_in_the_rails_file_path_cop_when_file_join_is_used_with_a_variable.md new file mode 100644 index 0000000000..66dccaaea4 --- /dev/null +++ b/changelog/fix_an_error_occurring_in_the_rails_file_path_cop_when_file_join_is_used_with_a_variable.md @@ -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][]) diff --git a/lib/rubocop/cop/rails/file_path.rb b/lib/rubocop/cop/rails/file_path.rb index 375f43e7b0..9846de0de7 100644 --- a/lib/rubocop/cop/rails/file_path.rb +++ b/lib/rubocop/cop/rails/file_path.rb @@ -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 @@ -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? diff --git a/spec/rubocop/cop/rails/file_path_spec.rb b/spec/rubocop/cop/rails/file_path_spec.rb index 6bcdd88dea..7e1072e6fd 100644 --- a/spec/rubocop/cop/rails/file_path_spec.rb +++ b/spec/rubocop/cop/rails/file_path_spec.rb @@ -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