Class: RuboCop::Cop::Style::Documentation

Inherits:
Base
  • Object
show all
Includes:
DocumentationComment, RangeHelp
Defined in:
lib/rubocop/cop/style/documentation.rb

Overview

Checks for missing top-level documentation of classes and modules. Classes with no body are exempt from the check and so are namespace modules - modules that have nothing in their bodies except classes, other modules, constant definitions or constant visibility declarations.

The documentation requirement is annulled if the class or module has a ‘#:nodoc:` comment next to it. Likewise, `#:nodoc: all` does the same for all its children.

Examples:

# bad
class Person
  # ...
end

module Math
end

# good
# Description/Explanation of Person class
class Person
  # ...
end

# allowed
# Class without body
class Person
end

# Namespace - A namespace can be a class or a module
# Containing a class
module Namespace
  # Description/Explanation of Person class
  class Person
    # ...
  end
end

# Containing constant visibility declaration
module Namespace
  class Private
  end

  private_constant :Private
end

# Containing constant definition
module Namespace
  Public = Class.new
end

# Macro calls
module Namespace
  extend Foo
end

AllowedConstants: [‘ClassMethods’]


# good
module A
  module ClassMethods
    # ...
  end
end

Constant Summary collapse

MSG =
'Missing top-level documentation comment for `%<type>s %<identifier>s`.'

Instance Method Summary collapse

Instance Method Details

#constant_definition?(node) ⇒ Object



79
# File 'lib/rubocop/cop/style/documentation.rb', line 79

def_node_matcher :constant_definition?, '{class module casgn}'

#constant_visibility_declaration?(node) ⇒ Object



85
86
87
# File 'lib/rubocop/cop/style/documentation.rb', line 85

def_node_matcher :constant_visibility_declaration?, <<~PATTERN
  (send nil? {:public_constant :private_constant} ({sym str} _))
PATTERN

#include_statement?(node) ⇒ Object



90
91
92
# File 'lib/rubocop/cop/style/documentation.rb', line 90

def_node_matcher :include_statement?, <<~PATTERN
  (send nil? {:include :extend :prepend} const)
PATTERN

#on_class(node) ⇒ Object



94
95
96
97
98
# File 'lib/rubocop/cop/style/documentation.rb', line 94

def on_class(node)
  return unless node.body

  check(node, node.body)
end

#on_module(node) ⇒ Object



100
101
102
# File 'lib/rubocop/cop/style/documentation.rb', line 100

def on_module(node)
  check(node, node.body)
end

#outer_module(node) ⇒ Object



82
# File 'lib/rubocop/cop/style/documentation.rb', line 82

def_node_search :outer_module, '(const (const nil? _) _)'