123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
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

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

Class Attribute Summary

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns a url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::RangeHelp - Included

#add_range, #column_offset_between,
#contents_range

A range containing only the contents of a literal with delimiters (e.g.

#directions,
#effective_column

Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value.

#final_pos, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range

::RuboCop::Cop::DocumentationComment - Included

#annotation_keywords, #documentation_comment?, #interpreter_directive_comment?,
#precede?

The args node1 & node2 may represent a RuboCop::AST::Node or a Parser::Source::Comment.

#preceding_comment?

The args node1 & node2 may represent a RuboCop::AST::Node or a Parser::Source::Comment.

#preceding_lines, #rubocop_directive_comment?

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect,
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

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

Instance Method Details

#allowed_constants (private)

[ GitHub ]

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

def allowed_constants
  @allowed_constants ||= cop_config.fetch('AllowedConstants', []).map(&:intern)
end

#check(node, body) (private)

[ GitHub ]

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

def check(node, body)
  return if namespace?(body)
  return if documentation_comment?(node)
  return if constant_allowed?(node)
  return if nodoc_self_or_outer_module?(node)
  return if include_statement_only?(body)

  range = range_between(node.source_range.begin_pos, node.loc.name.end_pos)
  message = format(MSG, type: node.type, identifier: identifier(node))
  add_offense(range, message: message)
end

#compact_namespace?(node) ⇒ Boolean (private)

[ GitHub ]

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

def compact_namespace?(node)
  node.loc.name.source.include?('::')
end

#constant_allowed?(node) ⇒ Boolean (private)

[ GitHub ]

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

def constant_allowed?(node)
  allowed_constants.include?(node.identifier.short_name)
end

#constant_declaration?(node) ⇒ Boolean (private)

[ GitHub ]

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

def constant_declaration?(node)
  constant_definition?(node) || constant_visibility_declaration?(node)
end

#constant_definition?(node)

[ GitHub ]

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

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

#constant_visibility_declaration?(node)

[ GitHub ]

  
# 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

#identifier(node) (private)

[ GitHub ]

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

def identifier(node)
  # Get the fully qualified identifier for a class/module
  nodes = [node, *node.each_ancestor(:class, :module)]
  identifier = nodes.reverse_each.flat_map { |n| qualify_const(n.identifier) }.join('::')

  identifier.sub('::::', '::')
end

#include_statement?(node)

[ GitHub ]

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

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

#include_statement_only?(body) ⇒ Boolean (private)

[ GitHub ]

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

def include_statement_only?(body)
  return true if include_statement?(body)

  body.respond_to?(:children) && body.children.all? { |node| include_statement_only?(node) }
end

#namespace?(node) ⇒ Boolean (private)

[ GitHub ]

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

def namespace?(node)
  return false unless node

  if node.begin_type?
    node.children.all? { |child| constant_declaration?(child) }
  else
    constant_definition?(node)
  end
end

#nodoc(node) (private)

[ GitHub ]

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

def nodoc(node)
  processed_source.ast_with_comments[node.children.first].first
end

#nodoc?(comment, require_all: false) ⇒ Boolean (private)

[ GitHub ]

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

def nodoc?(comment, require_all: false)
  /^#\s*:nodoc:#{"\s+all\s*$" if require_all}/.match?(comment.text)
end

#nodoc_comment?(node, require_all: false) ⇒ Boolean (private)

First checks if the :nodoc: comment is associated with the class/module. Unless the element is tagged with :nodoc:, the search proceeds to check its ancestors for :nodoc: all. Note: How end-of-line comments are associated with code changed in parser-2.2.0.4.

[ GitHub ]

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

def nodoc_comment?(node, require_all: false)
  return false unless node&.children&.first

  nodoc = nodoc(node)

  return true if same_line?(nodoc, node) && nodoc?(nodoc, require_all: require_all)

  nodoc_comment?(node.parent, require_all: true)
end

#nodoc_self_or_outer_module?(node) ⇒ Boolean (private)

[ GitHub ]

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

def nodoc_self_or_outer_module?(node)
  nodoc_comment?(node) ||
    (compact_namespace?(node) && nodoc_comment?(outer_module(node).first))
end

#on_class(node)

[ GitHub ]

  
# 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)

[ GitHub ]

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

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

#outer_module(node)

[ GitHub ]

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

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

#qualify_const(node) (private)

[ GitHub ]

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

def qualify_const(node)
  return if node.nil?

  if node.cbase_type? || node.self_type? || node.call_type? || node.variable?
    node.source
  else
    [qualify_const(node.namespace), node.short_name].compact
  end
end