Skip to content

Commit

Permalink
Merge pull request #10121 from dvandersluis/lint/require-relative-sel…
Browse files Browse the repository at this point in the history
…f-path

Fix false positive for `Lint/RequireRelativeSelfPath` when requiring  file with the same name but different extension
  • Loading branch information
koic committed Sep 24, 2021
2 parents aafead6 + ec0bf40 commit 05494a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rubocop/cop/lint/require_relative_self_path.rb
Expand Up @@ -27,7 +27,7 @@ class RequireRelativeSelfPath < Base

def on_send(node)
return unless (required_feature = node.first_argument)
return unless remove_ext(processed_source.file_path) == remove_ext(required_feature.value)
return unless same_file?(processed_source.file_path, required_feature.value)

add_offense(node) do |corrector|
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
Expand All @@ -36,6 +36,10 @@ def on_send(node)

private

def same_file?(file_path, required_feature)
file_path == required_feature || remove_ext(file_path) == required_feature
end

def remove_ext(file_path)
File.basename(file_path, File.extname(file_path))
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/lint/require_relative_self_path_spec.rb
Expand Up @@ -36,4 +36,10 @@
require_relative
RUBY
end

it 'does not register an offense when the filename is the same but the extension does not match' do
expect_no_offenses(<<~RUBY, 'foo.rb')
require_relative 'foo.racc'
RUBY
end
end

0 comments on commit 05494a4

Please sign in to comment.