Skip to content
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

Merged

Conversation

koic
Copy link
Member

@koic koic commented Sep 15, 2021

Fixes #10084.

This PR adds new Lint/RequireRelativeSelfPath cop.
It checks for uses of require_relative with self file path argument.

# bad

# foo.rb
require_relative 'foo'
require_relative 'bar'

# good

# foo.rb
require_relative 'bar'

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

config/default.yml Outdated Show resolved Hide resolved
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'
```
@koic koic force-pushed the add_new_lint_require_relative_self_path_cop branch from 8ce8bcf to 4216374 Compare September 15, 2021 15:41
Copy link
Member

@dvandersluis dvandersluis left a 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.

@koic
Copy link
Member Author

koic commented Sep 15, 2021

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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# require_relative 'bar'

I would suggest removing this line because bar does not raise offense, only foo does.

Copy link
Member Author

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
Copy link
Contributor

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?

Copy link
Member Author

@koic koic Sep 16, 2021

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.

@bbatsov bbatsov merged commit d1cf26a into rubocop:master Sep 16, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 16, 2021

Nicely done!

@koic koic deleted the add_new_lint_require_relative_self_path_cop branch September 16, 2021 05:57
@ShockwaveNN
Copy link
Contributor

Thank you for fastest implementation of idea )

@koic
Copy link
Member Author

koic commented Sep 16, 2021

@ShockwaveNN You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cop idea: mark self-require_relative as bad code
5 participants