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

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

Overview

This cop 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`.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

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

#on_class(node) ⇒ Object


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

def on_class(node)
  return unless node.body

  check(node, node.body)
end

#on_module(node) ⇒ Object


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

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? _) _)'