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.
When AllCops/UseProjectIndex is enabled and the rubydex gem is
installed, a reopened class or module is not reported when any of its
other definition sites (in the same or another file) carries a
documentation comment.
Constant Summary
-
DIRECTIVE_COMMENT_REGEXP =
# File 'lib/rubocop/cop/style/documentation.rb', line 85
Comments that configure tooling rather than document the code.
/\A\#\s*(rubocop:|frozen_string_literal:|encoding:| shareable_constant_value:|typed:|-\*-)/x.freeze
-
MSG =
# File 'lib/rubocop/cop/style/documentation.rb', line 82'Missing top-level documentation comment for `%<type>s %<identifier>s`.'
::RuboCop::Cop::Base - Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::ProjectIndexHelp - Included
BUILTIN_DOCUMENT_URI, FILE_URI_PREFIX, WINDOWS_DRIVE_PREFIX
::RuboCop::Cop::RangeHelp - Included
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 |
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
::RuboCop::Cop::Base - Inherited
::RuboCop::Cop::AutocorrectLogic - Included
Instance Method Summary
- #constant_definition?(node)
- #constant_visibility_declaration?(node)
- #include_statement?(node)
- #on_class(node)
- #on_module(node)
- #outer_module(node)
- #add_documentation_offense(node) private
- #allowed_constants private
- #check(node, body) private
- #compact_namespace?(node) ⇒ Boolean private
- #constant_allowed?(node) ⇒ Boolean private
- #constant_declaration?(node) ⇒ Boolean private
-
#documented_elsewhere?(node) ⇒ Boolean
private
A reopened class or module is typically documented at a single definition site.
- #documenting_comments?(definition) ⇒ Boolean private
- #identifier(node) private
- #include_statement_only?(body) ⇒ Boolean private
- #namespace?(node) ⇒ Boolean private
- #nodoc(node) private
- #nodoc?(comment, require_all: false) ⇒ Boolean private
-
#nodoc_comment?(node, require_all: false) ⇒ Boolean
private
First checks if the :nodoc: comment is associated with the class/module.
- #nodoc_self_or_outer_module?(node) ⇒ Boolean private
- #other_definition?(definition, node) ⇒ Boolean private
- #qualify_const(node) private
::RuboCop::Cop::RangeHelp - Included
| #add_range, | |
| #arguments_range | A range containing the first to the last argument of a method call or method definition. |
| #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::ProjectIndexHelp - Included
| #external_dependency_checksum, #compute_project_index_signature, | |
| #definitions_in_other_files | Returns the definitions among |
| #indexed_singleton_member | A namespace without any singleton method has no singleton-class declaration of its own, so the lookup starts from the first ancestor that has one; its |
| #indexed_singleton_of | The declaration of `declaration’s singleton class, or nil when no singleton method is defined on it anywhere in the project. |
| #inherited_index_member? | Whether an ancestor of |
| #lexical_nesting_of | The lexical nesting the node’s constants resolve through, outermost first. |
| #prior_definition_in_other_file, #project_index_signature, | |
| #resolve_constant_in_index | Resolves a constant node the way Ruby does: the first segment through the lexical nesting and every following segment inside the previous one. |
| #same_file? | |
::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 |
| #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_gem_version | Returns a gems locked versions (i.e. |
| #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, #matches_absolute_include_pattern?, #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
#add_documentation_offense(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/documentation.rb', line 127
def add_documentation_offense(node) range = range_between(node.source_range.begin_pos, node.loc.name.end_pos) = format(MSG, type: node.type, identifier: identifier(node)) add_offense(range, message: ) end
#allowed_constants (private)
[ GitHub ]# File 'lib/rubocop/cop/style/documentation.rb', line 221
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 116
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) return if documented_elsewhere?(node) add_documentation_offense(node) end
#compact_namespace?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/documentation.rb', line 194
def compact_namespace?(node) node.loc.name.source.include?('::') end
#constant_allowed?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/documentation.rb', line 190
def constant_allowed?(node) allowed_constants.include?(node.identifier.short_name) end
#constant_declaration?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/documentation.rb', line 186
def constant_declaration?(node) constant_definition?(node) || constant_visibility_declaration?(node) end
#constant_definition?(node)
[ GitHub ]# File 'lib/rubocop/cop/style/documentation.rb', line 89
def_node_matcher :constant_definition?, '{class module casgn}'
#constant_visibility_declaration?(node)
[ GitHub ]# File 'lib/rubocop/cop/style/documentation.rb', line 95
def_node_matcher :constant_visibility_declaration?, <<~PATTERN (send nil? {:public_constant :private_constant} ({sym str} _)) PATTERN
#documented_elsewhere?(node) ⇒ Boolean (private)
A reopened class or module is typically documented at a single
definition site. When AllCops/UseProjectIndex is enabled, the
documentation requirement is satisfied by a comment on any other
definition of the same class or module.
# File 'lib/rubocop/cop/style/documentation.rb', line 137
def documented_elsewhere?(node) return false unless project_index declaration = resolve_constant_in_index(node.identifier) return false unless declaration.is_a?(Rubydex::Namespace) declaration.definitions.any? do |definition| other_definition?(definition, node) && documenting_comments?(definition) end rescue StandardError false end
#documenting_comments?(definition) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/documentation.rb', line 158
def documenting_comments?(definition) definition.comments.to_a.any? do |comment| text = comment.string.strip !text.empty? && !DIRECTIVE_COMMENT_REGEXP.match?(text) end end
#identifier(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/documentation.rb', line 225
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 100
def_node_matcher :include_statement?, <<~PATTERN (send nil? {:include :extend :prepend} const) PATTERN
#include_statement_only?(body) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/documentation.rb', line 170
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)
# File 'lib/rubocop/cop/style/documentation.rb', line 176
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 217
def nodoc(node) processed_source.ast_with_comments[node.children.first].first end
#nodoc?(comment, require_all: false) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/documentation.rb', line 213
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.
#nodoc_self_or_outer_module?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/documentation.rb', line 165
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 104
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 110
def on_module(node) check(node, node.body) end
#other_definition?(definition, node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/documentation.rb', line 150
def other_definition?(definition, node) location = definition.location return false unless location.uri.start_with?(FILE_URI_PREFIX) !same_file?(location.to_file_path, processed_source.file_path) || location.to_display.start_line != node.first_line end
#outer_module(node)
[ GitHub ]# File 'lib/rubocop/cop/style/documentation.rb', line 92
def_node_search :outer_module, '(const (const nil? _) _)'
#qualify_const(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/documentation.rb', line 233
def qualify_const(node) return if node.nil? if node.type?(:cbase, :self, :call) || node.variable? node.source else [qualify_const(node.namespace), node.short_name].compact end end