Class: RuboCop::Cop::Sevencop::MethodDefinitionOrdered

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp, Sevencop::CopConcerns::Ordered, VisibilityHelp
Defined in:
lib/rubocop/cop/sevencop/method_definition_ordered.rb

Overview

Sort method definition in alphabetical order.

Examples:

# bad
def b; end
def a; end

# good
def a; end
def b; end

# good
def b; end
def c; end
private
def a; end
def d; end

# good
def initialize; end
def a; end

Constant Summary collapse

MSG =
'Sort method definition in alphabetical order.'

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ void Also known as: on_defs

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::DefNode)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/sevencop/method_definition_ordered.rb', line 39

def on_def(node)
  previous_older_sibling = find_previous_older_sibling(node)
  return unless previous_older_sibling

  add_offense(node) do |corrector|
    swap(
      range_with_comments_and_lines(previous_older_sibling),
      range_with_comments_and_lines(node),
      corrector: corrector,
      newline: true
    )
  end
end