From 8d2c6ae75c898a4a8d55d19f3b2e72a48026d949 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 23 Jun 2023 19:40:14 +0900 Subject: [PATCH] [Fix #11974] Fix an error for `Style/RedundantCurrentDirectoryInPath` Fixes #11974. This PR fixes an error for `Style/RedundantCurrentDirectoryInPath` when using string interpolation in `require_relative`. --- ...style_redundant_current_directory_in_path.md | 1 + .../redundant_current_directory_in_path.rb | 2 +- .../redundant_current_directory_in_path_spec.rb | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_an_error_for_style_redundant_current_directory_in_path.md diff --git a/changelog/fix_an_error_for_style_redundant_current_directory_in_path.md b/changelog/fix_an_error_for_style_redundant_current_directory_in_path.md new file mode 100644 index 000000000000..1837681d5f6b --- /dev/null +++ b/changelog/fix_an_error_for_style_redundant_current_directory_in_path.md @@ -0,0 +1 @@ +* [#11974](https://github.com/rubocop/rubocop/issues/11974): Fix an error for `Style/RedundantCurrentDirectoryInPath` when using string interpolation in `require_relative`. ([@koic][]) diff --git a/lib/rubocop/cop/style/redundant_current_directory_in_path.rb b/lib/rubocop/cop/style/redundant_current_directory_in_path.rb index fc6c4651f0a3..d5ba11a6033d 100644 --- a/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +++ b/lib/rubocop/cop/style/redundant_current_directory_in_path.rb @@ -22,7 +22,7 @@ class RedundantCurrentDirectoryInPath < Base def on_send(node) return unless node.method?(:require_relative) - return unless node.first_argument.str_content.start_with?(CURRENT_DIRECTORY_PATH) + return unless node.first_argument.str_content&.start_with?(CURRENT_DIRECTORY_PATH) return unless (index = node.first_argument.source.index(CURRENT_DIRECTORY_PATH)) begin_pos = node.first_argument.source_range.begin.begin_pos + index diff --git a/spec/rubocop/cop/style/redundant_current_directory_in_path_spec.rb b/spec/rubocop/cop/style/redundant_current_directory_in_path_spec.rb index b4f5ded686de..95053a8f2546 100644 --- a/spec/rubocop/cop/style/redundant_current_directory_in_path_spec.rb +++ b/spec/rubocop/cop/style/redundant_current_directory_in_path_spec.rb @@ -12,6 +12,17 @@ RUBY end + it "registers an offense when using a current directory path with string interpolation in `require_relative '...'`" do + expect_offense(<<~'RUBY') + require_relative './path/#{to}/feature' + ^^ Remove the redundant current directory path. + RUBY + + expect_correction(<<~'RUBY') + require_relative 'path/#{to}/feature' + RUBY + end + it 'registers an offense when using a current directory path in `require_relative %q(...)`' do expect_offense(<<~RUBY) require_relative %q(./path/to/feature) @@ -35,6 +46,12 @@ RUBY end + it 'does not register an offense when not using a current directory path with string interpolation in `require_relative`' do + expect_no_offenses(<<~'RUBY') + require_relative "path/#{to}/feature" + RUBY + end + it 'does not register an offense when not using a current directory path in not `require_relative`' do expect_no_offenses(<<~RUBY) do_something './path/to/feature'