Class: RuboCop::Cop::Elegant::NoClassInModule

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/elegant/no_class_in_module.rb

Overview

Forbids declaring a class inside a module declaration. Instead, the class must be declared with the compact namespace syntax, like class Foo::Bar. An empty module module Foo; end, a module that contains only other modules, methods, or constants, and a class nested inside another class are all allowed; only a class whose nearest enclosing scope is a module is rejected.

Constant Summary collapse

MSG =
'Class %<name>s must use compact namespace syntax, not be nested inside a module'

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



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

def on_class(node)
  owner = node.each_ancestor(:module, :class).first
  return if owner.nil? || !owner.module_type?
  add_offense(node, message: format(MSG, name: label(node)))
end