Class: RuboCop::Cop::Obsession::MethodOrder
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Obsession::MethodOrder
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp, Helpers, VisibilityHelp
- Defined in:
- lib/rubocop/cop/obsession/method_order.rb
Overview
This cop checks for private/protected methods that are not ordered correctly. It supports autocorrect.
Code should read from top to bottom: methods should be defined in the same order as the order when they are first mentioned. Private/protected methods should follow that rule.
Note 1: public methods do not have to follow that rule, and can be defined in any order the developer wants, like by order of importance. This is because they are usually called outside of the class and often not called within the class at all. If possible though, developers should still try to order their public methods from top to bottom when it makes sense.
Note 2: method order cannot be computed for methods called by ‘send`, metaprogramming, private methods called by superclasses or modules, etc. This cop’s suggestions and autocorrections may be slightly off for these kinds of edge cases.
Note 3: for more information on this style of method ordering, see Robert C. Martin’s “Clean Code” book > “Chapter 3: Functions” > “One level of abstraction per function” > “Reading Code from Top to Bottom: The Stepdown Rule” chapter.
Defined Under Namespace
Classes: Node
Constant Summary collapse
- MSG =
'Method `%<after>s` should appear below `%<previous>s`.'
Constants included from Helpers
Instance Method Summary collapse
Methods included from Helpers
Instance Method Details
#on_class(class_node) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rubocop/cop/obsession/method_order.rb', line 86 def on_class(class_node) @class_node = class_node find_private_node || return build_methods || return build_callback_methods build_method_call_tree build_ordered_private_methods build_private_methods verify_private_methods_order end |
#on_module(module_node) ⇒ Object
99 100 101 |
# File 'lib/rubocop/cop/obsession/method_order.rb', line 99 def on_module(module_node) on_class(module_node) end |