Class: Yard::Lint::Validators::Documentation::MarkdownSyntax::Validator

Inherits:
Base
  • Object
show all
Defined in:
lib/yard/lint/validators/documentation/markdown_syntax/validator.rb

Overview

Validates markdown syntax in documentation

Instance Attribute Summary

Attributes inherited from Base

#config, #selection

Instance Method Summary collapse

Methods inherited from Base

in_process, in_process?, in_process_visibility, #initialize, validator_name

Constructor Details

This class inherits a constructor from Yard::Lint::Validators::Base

Instance Method Details

#in_process_query(object, collector) ⇒ void

This method returns an undefined value.

Execute query for a single object during in-process execution. Checks for markdown syntax errors in docstrings.

Parameters:

  • object (YARD::CodeObjects::Base)

    the code object to query

  • collector (Executor::ResultCollector)

    collector for output



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yard/lint/validators/documentation/markdown_syntax/validator.rb', line 18

def in_process_query(object, collector)
  docstring_text = object.docstring.to_s
  return if docstring_text.empty?
  return if duplicate_docstring?(object)

  errors = []

  # Check for unclosed inline backticks, ignoring fenced code blocks
  # (``` ... ```): their fence characters and contents are not
  # inline-code markers and otherwise inflate the count.
  errors << 'unclosed_backtick' if inline_backtick_count(docstring_text).odd?

  # Check for unclosed code blocks
  code_block_count = docstring_text.scan(/^```/).count
  errors << 'unclosed_code_block' if code_block_count.odd?

  # Check for unclosed bold markers, ignoring fenced code blocks and
  # inline code spans (their contents are code, not markdown) as well
  # as `**` runs surrounded by whitespace, which cannot delimit
  # CommonMark emphasis (e.g. the exponent operator in `x ** y`).
  errors << 'unclosed_bold' if bold_marker_count(docstring_text).odd?

  # Check for invalid list markers, reported with their absolute
  # source line rather than a docstring-relative index
  docstring_text.lines.each_with_index do |line, line_idx|
    stripped = line.strip
    errors << "invalid_list_marker:#{docstring_line(object, line_idx)}" if stripped.match?(/^[•·]/)
  end

  return if errors.empty?

  collector.puts "#{object.file}:#{object.line}: #{object.title}"
  collector.puts errors.join('|')
end