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
Add Style/RequireOrder
cop
#11235
Add Style/RequireOrder
cop
#11235
Conversation
I love this suggestion and the thoughtful analysis you've submitted alongside it! I'm only wondering if this cop should also suggest to keep |
# require_relative 'a' | ||
# require_relative 'b' | ||
# | ||
# # good |
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.
You might mention here that a blank line introduces a new section or something like this.
a0925f5
to
5559a18
Compare
I guess it would be difficult to enforce that, since there would be case where you want to write |
@r7kamura @bbatsov This cop fails for code like: def load_account
@account = Account.find(params.require(:account_id))
end Trace:
|
Follow up rubocop#11235 (comment). This PR fixes an error for `Style/RequireOrder` when `require` is a method argument.
Many projects have
require
andrequire_relative
in alphabetical order by convention, the rubocop gem itself being one example. I thought it would be good to have a cop to provide such consistency.As a good example, rubocop-rspec inspects this with tests. Such checks are replaced by this cop.
Like
Bundler/OrderedGems
, code separated by a blank line is considered a different group, so if there are obvious dependencies but you really don't want to writerequire_relative
in each file, it is better to separate the groups. lib/rubocop/formatter.rb is a good example of that.Unfortunately, the dependencies written in lib/rubocop.rb are too complex, so I gave up on organizing this at the time of introducing this cop and decided to ignore it from this cop. Personally, if one file A depends on another file B, I prefer to indicate that fact by writing
require_relative
in file A, but I don't think that is the practice in the rubocop gem, so I don't want to go into this issue here.Since this cop enforces the rule "always sort them in alphabetical order within a group", I don't think false-positive will occur, but applying autocorrection obviously changes the behavior from the original code, so I have set
SafeAutoCorrect: false
.Ideally, I believe it is a good manner that the code to always be written in such a way that it does not depend on the order of
require
, but I setEnabled: false
because this cop has a large impact and may or may not be acceptable depending on the preference of the project. I think this cop is rather opinionated, so I would be glad to get your opinion on the policy.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.