Skip to content

Commit

Permalink
[Fix #11974] Fix an error for Style/RedundantCurrentDirectoryInPath
Browse files Browse the repository at this point in the history
Fixes #11974.

This PR fixes an error for `Style/RedundantCurrentDirectoryInPath`
when using string interpolation in `require_relative`.
  • Loading branch information
koic authored and bbatsov committed Jun 24, 2023
1 parent 5a38fe3 commit 8d2c6ae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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][])
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/style/redundant_current_directory_in_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'
Expand Down

0 comments on commit 8d2c6ae

Please sign in to comment.