module RuboCop::Cop::DocumentationComment

Common functionality for checking documentation.

Private Instance Methods

documentation_comment?(node) click to toggle source
# File lib/rubocop/cop/mixin/documentation_comment.rb, line 14
def documentation_comment?(node)
  preceding_lines = preceding_lines(node)

  return false unless preceding_comment?(node, preceding_lines.last)

  preceding_lines.any? do |comment|
    !annotation?(comment) &&
      !interpreter_directive_comment?(comment) &&
      !rubocop_directive_comment?(comment)
  end
end
interpreter_directive_comment?(comment) click to toggle source
# File lib/rubocop/cop/mixin/documentation_comment.rb, line 45
def interpreter_directive_comment?(comment)
  comment.text =~ /^#\s*(frozen_string_literal|encoding):/
end
precede?(node1, node2) click to toggle source

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

# File lib/rubocop/cop/mixin/documentation_comment.rb, line 35
def precede?(node1, node2)
  node2.loc.line - node1.loc.line == 1
end
preceding_comment?(node1, node2) click to toggle source

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

# File lib/rubocop/cop/mixin/documentation_comment.rb, line 28
def preceding_comment?(node1, node2)
  node1 && node2 && precede?(node2, node1) &&
    comment_line?(node2.loc.expression.source)
end
preceding_lines(node) click to toggle source
# File lib/rubocop/cop/mixin/documentation_comment.rb, line 39
def preceding_lines(node)
  processed_source.ast_with_comments[node].select do |line|
    line.loc.line < node.loc.line
  end
end
rubocop_directive_comment?(comment) click to toggle source
# File lib/rubocop/cop/mixin/documentation_comment.rb, line 49
def rubocop_directive_comment?(comment)
  comment.text =~ CommentConfig::COMMENT_DIRECTIVE_REGEXP
end