Class: RuboCop::Cop::Sevencop::AutoloadOrdered

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

Overview

Sort autoload in alphabetical order.

Examples:

# bad
autoload :B, 'b'
autoload :A, 'a'

# good
autoload :A, 'a'
autoload :B, 'b'

# good
autoload :A, 'a'
autoload :D, 'd'

autoload :B, 'b'
autoload :C, 'c'

Constant Summary collapse

MSG =
'Sort `autoload` in alphabetical order.'
RESTRICT_ON_SEND =
%i[
  autoload
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ void Also known as: on_csend

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


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

def on_send(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
    )
  end
end