Class: RuboCop::Cop::Style::CompactModuleNesting

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/style/compact_module_nesting.rb

Overview

Enforces a compact outer module namespace with a separately nested innermost class.

When the innermost definition in a wrapper chain is a ‘class`, the outer modules are collapsed into a single compact `module A::B` wrapping the class. When the chain is entirely modules, every segment is collapsed into a single compact `module A::B::C` wrapping the body directly.

The canonical form is one ‘module A::B::C` line wrapping a single nested innermost `class D`:

module A::B::C
  class D
  end
end

Examples:

# bad
module A
  module B
    class C
    end
  end
end

# bad
class A::B::C
end

# good
module A::B
  class C
  end
end

# good (no namespace)
class Foo
end

Constant Summary collapse

MSG_NESTING =
'Use compact outer module nesting: `%<canonical>s`.'
MSG_MULTIPLE_ROOTS =
'Only one top-level module or class is allowed per file.'

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



55
56
57
# File 'lib/rubocop/cop/style/compact_module_nesting.rb', line 55

def on_class(node)
  handle(node)
end

#on_module(node) ⇒ Object



51
52
53
# File 'lib/rubocop/cop/style/compact_module_nesting.rb', line 51

def on_module(node)
  handle(node)
end

#on_new_investigationObject



59
60
61
# File 'lib/rubocop/cop/style/compact_module_nesting.rb', line 59

def on_new_investigation
  check_multiple_top_level_definitions
end