Class: Yard::Lint::Validators::Documentation::EmptyCommentLine::Validator

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

Overview

Validates empty comment lines at the start/end of documentation blocks

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 empty leading/trailing comment lines in documentation blocks.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/yard/lint/validators/documentation/empty_comment_line/validator.rb', line 18

def in_process_query(object, collector)
  return unless object.file && File.exist?(object.file) && object.line.to_i > 1

  check_leading = check_leading?
  check_trailing = check_trailing?

  source_lines = File.readlines(object.file)
  definition_line = object.line - 1

  # Find comment block boundaries
  comment_end = nil
  comment_start = nil

  (definition_line - 1).downto(0) do |i|
    line = source_lines[i].to_s.rstrip
    stripped = line.strip

    if stripped.empty? && comment_end.nil?
      # Skip empty lines before finding comment block
      next
    elsif stripped.start_with?('#')
      comment_end ||= i
      comment_start = i
    else
      break
    end
  end

  return unless comment_start && comment_end

  comment_block = source_lines[comment_start..comment_end]

  # Find first and last content lines
  first_content_idx = nil
  last_content_idx = nil

  comment_block.each_with_index do |line, idx|
    stripped = line.strip
    has_content = stripped.match?(/^#.+\S/)
    if has_content
      first_content_idx ||= idx
      last_content_idx = idx
    end
  end

  return unless first_content_idx && last_content_idx

  violations = []

  # Check for leading empty comment lines
  if check_leading
    (0...first_content_idx).each do |i|
      if comment_block[i].strip.match?(/^#\s*$/)
        violations << "leading:#{comment_start + i + 1}"
      end
    end
  end

  # Check for trailing empty comment lines
  if check_trailing
    ((last_content_idx + 1)...comment_block.length).each do |i|
      if comment_block[i].strip.match?(/^#\s*$/)
        violations << "trailing:#{comment_start + i + 1}"
      end
    end
  end

  return if violations.empty?

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