Class: RuboCop::Cop::Style::OrderedConstants

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/style/ordered_constants.rb

Overview

Requires a run of consecutive constant assignments to be sorted alphabetically by name. A lone constant is never flagged; a run is broken by any non-constant statement. Flags without autocorrecting — a constant may reference one declared above it, so a human decides the order. Experimental: if it flags more than it helps, drop it.

Constant Summary collapse

MSG =
'Sort constant assignments alphabetically (`%<name>s` should come before `%<previous>s`).'

Instance Method Summary collapse

Instance Method Details

#on_begin(node) ⇒ Object



16
17
18
19
20
# File 'lib/rubocop/cop/style/ordered_constants.rb', line 16

def on_begin(node)
  node.children.chunk { |child| constant_assignment?(child) }.each do |constants, run|
    flag_unsorted(run) if constants
  end
end