Class: Yard::Lint::Validators::Documentation::BlankLineBeforeDefinition::Validator

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

Overview

Validates blank lines between documentation and definitions

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 blank lines between documentation blocks and definitions.

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
# File 'lib/yard/lint/validators/documentation/blank_line_before_definition/validator.rb', line 18

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

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

  blank_count, doc_block_line = analyze_spacing(source_lines, definition_line)

  return if blank_count.zero? || doc_block_line.nil?

  # Only a comment block that is genuinely THIS object's documentation can have
  # been detached from it by the blank line. When the object is documented (its
  # docstring is non-empty) but that docstring did not come from the comment block
  # sitting above this definition, the block is a foreign comment - a file-level
  # license/copyright banner, an encoding note, or an unrelated comment above a
  # namespace reopening that is documented in another file - and the blank line is
  # intentional. This is decided by comparing the object's docstring against the
  # block's text, so it covers any such banner without hardcoding its wording.
  return if foreign_comment_block?(object, source_lines, doc_block_line)

  violation_type = blank_count >= 2 ? 'orphaned' : 'single'

  return unless pattern_enabled?(violation_type)

  collector.puts "#{object.file}:#{object.line}: #{object.title}"
  collector.puts "#{violation_type}:#{blank_count}"
end