Rubocop: Cop idea: check for alphabetically sorted `require` statements

Created on 28 Nov 2016  路  4Comments  路  Source: rubocop-hq/rubocop

I am curious to know what the community thinks of this.

I find it useful when require statements are alphabetically sorted. It makes it easier to check for a specific library, and I think it looks more aesthetically pleasing.

Something that could complicate this: are require statements inside of methods frowned upon? Regardless of library size/frequency of use, should they always be placed at the beginning of a file? If interspersed require statements are valid, this cop could just check that any "grouped" require statements are alphabetical.

Related but probably a different cop, rubocop does not check for redundant require statements (even on subsequent lines).

RuboCop version

$ rubocop -V
0.45.0 (using Parser 2.3.1.4, running on ruby 2.3.1 x86_64-linux)
feature request stale

Most helpful comment

After giving this some thought, this may not be the best idea since some calls require might depend on other classes that do not precede it alphabetically. I still like the idea of alphabetizing when possible. Maybe this cop should be disabled by default since it is likely to break code.

From RuboCop's own source code (lib/rubocop.rb)

54 require 'rubocop/cop/mixin/configurable_max'
55 require 'rubocop/cop/mixin/code_length' # relies on configurable_max
56 require 'rubocop/cop/mixin/classish_length' # relies on code_length
57 require 'rubocop/cop/mixin/configurable_enforced_style'

All 4 comments

The sorting of require statements could also be extended to load.

If interspersed require statements are valid, this cop could just check that any "grouped" require statements are alphabetical.

We could probably combine require "groups" for a user and the majority of the time everything would work fine. In practice, code and style can get very weird so I agree that it wouldn't be worth trying to combine groups. I think this is the same sort of functionality that was introduced with Bundler/OrderedGems.

Related but probably a different cop, rubocop does not check for redundant require statements (even on subsequent lines).

Correct, as far as I know of, there is not a cop taht checks for this.

After giving this some thought, this may not be the best idea since some calls require might depend on other classes that do not precede it alphabetically. I still like the idea of alphabetizing when possible. Maybe this cop should be disabled by default since it is likely to break code.

From RuboCop's own source code (lib/rubocop.rb)

54 require 'rubocop/cop/mixin/configurable_max'
55 require 'rubocop/cop/mixin/code_length' # relies on configurable_max
56 require 'rubocop/cop/mixin/classish_length' # relies on code_length
57 require 'rubocop/cop/mixin/configurable_enforced_style'

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

Was this page helpful?
0 / 5 - 0 ratings