New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fix #10084] Add new Lint/RequireRelativeSelfPath
cop
#10085
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#10084](https://github.com/rubocop/rubocop/issues/10084): Add new `Lint/RequireRelativeSelfPath` cop. ([@koic][]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# Checks for uses a file requiring itself with `require_relative`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# | ||
# # foo.rb | ||
# require_relative 'foo' | ||
# require_relative 'bar' | ||
# | ||
# # good | ||
# | ||
# # foo.rb | ||
# require_relative 'bar' | ||
# | ||
class RequireRelativeSelfPath < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove the `require_relative` that requires itself.' | ||
RESTRICT_ON_SEND = %i[require_relative].freeze | ||
|
||
def on_send(node) | ||
return unless (required_feature = node.first_argument) | ||
return unless remove_ext(processed_source.file_path) == remove_ext(required_feature.value) | ||
|
||
add_offense(node) do |corrector| | ||
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true)) | ||
end | ||
end | ||
|
||
private | ||
|
||
def remove_ext(file_path) | ||
File.basename(file_path, File.extname(file_path)) | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::RequireRelativeSelfPath, :config do | ||
it 'registers an offense when using `require_relative` with self file path argument' do | ||
expect_offense(<<~RUBY, 'foo.rb') | ||
require_relative 'foo' | ||
^^^^^^^^^^^^^^^^^^^^^^ Remove the `require_relative` that requires itself. | ||
require_relative 'bar' | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
require_relative 'bar' | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `require_relative` with self file path argument (with ext)' do | ||
expect_offense(<<~RUBY, 'foo.rb') | ||
require_relative 'foo.rb' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Remove the `require_relative` that requires itself. | ||
require_relative 'bar' | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
require_relative 'bar' | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `require_relative` without self file path argument' do | ||
expect_no_offenses(<<~RUBY, 'foo.rb') | ||
require_relative 'bar' | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `require_relative` without argument' do | ||
expect_no_offenses(<<~RUBY, 'foo.rb') | ||
require_relative | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will always raise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case has confirmed that it does not cause an error with a valid Ruby syntax. And I think it should do method argument checking with RBS instead of RuboCop, so the cop name and role will remain the same. |
||
RUBY | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest removing this line because
bar
does not raise offense, onlyfoo
does.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would not be correct for
foo
to bebar
. I'd like to keep this for contrasted with the good case.