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

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

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, 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, #inspect, 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?, #autocorrect_with_disable_uncorrectable?, #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

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