From ec0bf4065ad8b389b7af5e74ea82658e95c3d09b Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Thu, 23 Sep 2021 14:31:53 -0400 Subject: [PATCH] Fix false positive for `Lint/RequireRelativeSelfPath` when requiring a file with the same name but different extension. --- lib/rubocop/cop/lint/require_relative_self_path.rb | 6 +++++- spec/rubocop/cop/lint/require_relative_self_path_spec.rb | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/rubocop/cop/lint/require_relative_self_path.rb b/lib/rubocop/cop/lint/require_relative_self_path.rb index 9a135ecb3a7..ad0a877cac8 100644 --- a/lib/rubocop/cop/lint/require_relative_self_path.rb +++ b/lib/rubocop/cop/lint/require_relative_self_path.rb @@ -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)) @@ -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 diff --git a/spec/rubocop/cop/lint/require_relative_self_path_spec.rb b/spec/rubocop/cop/lint/require_relative_self_path_spec.rb index 5eb8225fe7e..5f080e10126 100644 --- a/spec/rubocop/cop/lint/require_relative_self_path_spec.rb +++ b/spec/rubocop/cop/lint/require_relative_self_path_spec.rb @@ -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