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
[Fix #10084] Add new Lint/RequireRelativeSelfPath
cop
#10085
Conversation
Fixes rubocop#10084. This PR adds new `Lint/RequireRelativeSelfPath` cop. It checks for uses of `require_relative` with self file path argument. ```ruby # bad # foo.rb require_relative 'foo' require_relative 'bar' # good # foo.rb require_relative 'bar' ```
8ce8bcf
to
4216374
Compare
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.
This looks good to me! I'm wondering if there are more edge cases we should care about (since require_relative
can have paths with .
and ..
in them) but it might make this overly complicated for no tangible benefit.
Yeah, I have the same idea. It doesn't complicate the implementation because of the edge cases that user probably rarely encounter. |
# | ||
# # foo.rb | ||
# require_relative 'foo' | ||
# require_relative 'bar' |
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.
# require_relative 'bar' |
I would suggest removing this line because bar
does not raise offense, only foo
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 be bar
. I'd like to keep this for contrasted with the good case.
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
This will always raise ArgumentError
and hence should be detected by rubocop. Maybe we change the cop name to Lint/RequireRelativePath
and accomodate this case too?
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.
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.
Nicely done! |
Thank you for fastest implementation of idea ) |
@ShockwaveNN You're welcome! |
Fixes #10084.
This PR adds new
Lint/RequireRelativeSelfPath
cop.It checks for uses of
require_relative
with self file path argument.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.