class RuboCop::Cop::Style::Documentation
end
# …
class Person
# Description/Explanation of Person class
# good
end
# …
class Person
# bad
@example
same for all its children.
a “#:nodoc:” comment next to it. Likewise, “#:nodoc: all” does the
The documentation requirement is annulled if the class or module has
their bodies except classes, other modules, or constant definitions.
check and so are namespace modules - modules that have nothing in
classes and modules. Classes with no body are exempt from the
This cop checks for missing top-level documentation of
def check(node, body, type)
def check(node, body, type) return if namespace?(body) return if documentation_comment?(node) || nodoc_comment?(node) return if compact_namespace?(node) && nodoc_comment?(outer_module(node).first) add_offense(node, location: :keyword, message: format(MSG, type: type)) end
def compact_namespace?(node)
def compact_namespace?(node) node.loc.name.source =~ /::/ end
def namespace?(node)
def namespace?(node) return false unless node if node.begin_type? node.children.all? { |child| constant_definition?(child) } else constant_definition?(node) end end
def nodoc(node)
def nodoc(node) processed_source.ast_with_comments[node.children.first].first end
def nodoc?(comment, require_all = false)
def nodoc?(comment, require_all = false) comment.text =~ /^#\s*:nodoc:#{"\s+all\s*$" if require_all}/ end
def nodoc_comment?(node, require_all = false)
Note: How end-of-line comments are associated with code changed in
proceeds to check its ancestors for :nodoc: all.
class/module. Unless the element is tagged with :nodoc:, the search
First checks if the :nodoc: comment is associated with the
def nodoc_comment?(node, require_all = false) return false unless node && node.children.first nodoc = nodoc(node) return true if same_line?(nodoc, node) && nodoc?(nodoc, require_all) nodoc_comment?(node.parent, true) end
def on_class(node)
def on_class(node) _, _, body = *node return unless body check(node, body, :class) end
def on_module(node)
def on_module(node) _, body = *node check(node, body, :module) end