Class: RuboCop::Cop::SortedMethodsByCall::Waterfall

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/sorted_methods_by_call/waterfall.rb

Overview

RuboCop::Cop::SortedMethodsByCall::Waterfall enforces “waterfall” ordering: define a method after any method that calls it (within the same scope).

  • Scopes: class/module/sclass (top-level can be enabled in config)

  • Offense: when a callee is defined above its caller

  • Autocorrect: UNSAFE; reorders methods within a contiguous visibility section

Example (good):

def call
  foo
  bar
end

private

def bar
  method123
end

def method123
  foo
end

def foo
  123
end

Example (bad):

def foo
  123
end

def call
  foo
end

Autocorrect (unsafe, opt-in via SafeAutoCorrect: false): topologically sorts the contiguous block of defs to satisfy edges (caller -> callee). Skips cycles and non-contiguous groups.

Constant Summary collapse

MSG =

RuboCop::Cop::SortedMethodsByCall::Waterfall::MSG -> String

Template message for offenses.

'Define %<callee>s after its caller %<caller>s (waterfall order).'

Instance Method Summary collapse

Instance Method Details

#on_begin(node) ⇒ void

This method returns an undefined value.

RuboCop::Cop::SortedMethodsByCall::Waterfall#on_begin -> void

Entry point for root :begin nodes (top-level). Whether it is analyzed depends on configuration (e.g., CheckTopLevel). By default, only class/module scopes are analyzed.

Parameters:

  • node (RuboCop::AST::Node)


60
61
62
# File 'lib/rubocop/cop/sorted_methods_by_call/waterfall.rb', line 60

def on_begin(node)
  analyze_scope(node)
end

#on_class(node) ⇒ void

This method returns an undefined value.

RuboCop::Cop::SortedMethodsByCall::Waterfall#on_class -> void

Entry point for class scopes.

Parameters:

  • node (RuboCop::AST::Node)


70
71
72
# File 'lib/rubocop/cop/sorted_methods_by_call/waterfall.rb', line 70

def on_class(node)
  analyze_scope(node)
end

#on_module(node) ⇒ void

This method returns an undefined value.

RuboCop::Cop::SortedMethodsByCall::Waterfall#on_module -> void

Entry point for module scopes.

Parameters:

  • node (RuboCop::AST::Node)


80
81
82
# File 'lib/rubocop/cop/sorted_methods_by_call/waterfall.rb', line 80

def on_module(node)
  analyze_scope(node)
end

#on_sclass(node) ⇒ void

This method returns an undefined value.

RuboCop::Cop::SortedMethodsByCall::Waterfall#on_sclass -> void

Entry point for singleton class scopes (class << self).

Parameters:

  • node (RuboCop::AST::Node)


90
91
92
# File 'lib/rubocop/cop/sorted_methods_by_call/waterfall.rb', line 90

def on_sclass(node)
  analyze_scope(node)
end