Check that methods are defined alphabetically per access modifier block (class, public, private, protected). Includes autocorrection.
# bad
def self.b_class; end
def self.a_class; end
def b_public; end
def a_public; end
private
def b_private; end
def a_private; end
# good
def self.a_class; end
def self.b_class; end
def a_public; end
def b_public; end
private
def a_private; end
def b_private; end
Add this line to your application's Gemfile:
gem 'rubocop-ordered_methods'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rubocop-ordered_methods
You need to tell RuboCop to load the OrderedMethods extension. There are two ways to do this:
Put this into your .rubocop.yml
.
require: rubocop-ordered_methods
Now you can run rubocop
and it will automatically load the RuboCop OrderedMethods
cops together with the standard cops.
rubocop --require rubocop-ordered_methods
Name | Default value | Configurable values |
---|---|---|
EnforcedStyle | 'alphabetical' |
'alphabetical' |
IgnoredMethods | ['initialize'] |
Array |
MethodQualifiers | [] |
Array |
Signature | nil |
'sorbet' , nil |
# .rubocop.yml
Layout/OrderedMethods:
EnforcedStyle: alphabetical
IgnoredMethods:
- initialize
MethodQualifiers:
- memoize
Signature: sorbet
The corrector will attempt to order methods based on the EnforcedStyle
. It attempts to
include surrounding comments and the qualifiers (e.g., aliases) listed in
::RuboCop::Cop::OrderedMethodsCorrector::QUALIFIERS
. The following (monstrous)
source is able to be correctly ordered:
# Long
# Preceding
# Comment
# class_b
def self.class_b; end
private_class_method :class_b
def self.class_a; end
# Long
# Succeeding
# Comment
# class_a
public_class_method :class_a
# Preceding comment for instance_b
def instance_b; end
# Long
# Succeeding
# Comment
# instance_b
alias_method :orig_instance_b, :instance_b
module_function :instance_b
private :instance_b
protected :instance_b
public :instance_b
# Long
# Preceding
# Comment
# instance_a
def instance_a; end
# Succeeding comment for instance_a
alias :new_instance_a :instance_a
alias_method :orig_instance_a, :instance_a
module_function :instance_a
private :instance_a
protected :instance_a
public :instance_a
Some gems (like memery
, memoist
, etc.) provide a DSL that modifies the method (e.g. for memoization).
Those DSL methods can be added to the MethodQualifiers
configuration, and they will be respected.
E.g. the following source can be correctly ordered:
def b; end;
memoize def a;end
Support for (Sorbet) method signatures was added to the corrector by
#7.
It is off by default due to performance concerns (not yet benchmarked). Enable
with Signature: sorbet
.
-
The corrector will warn and refuse to order a method if it were to be defined before its alias
-
If there's ambiguity about which method a comment or qualifier belongs to, the corrector might fail to order correctly. For example, in the following, the corrector would incorrectly order the comment as a comment of
a
:def b; end # Comment b def a; end
Development is ongoing. There might be extended periods between releases. The maintenance goal is to add features as needed and to maintain compatibility with Ruby and rubocop updates.
bundle install
bundle exec rake
Bug reports and pull requests are welcome on GitHub at https://github.com/shanecav84/rubocop-ordered_methods. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the RuboCop OrderedMethods project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.