Class: Yard::Lint::Results::Aggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/lint/results/aggregate.rb

Overview

Aggregates multiple validator results into a single result object

Constant Summary collapse

SEVERITY_ERROR =

Error severity level constant

'error'
SEVERITY_WARNING =

Warning severity level constant

'warning'
SEVERITY_CONVENTION =

Convention severity level constant

'convention'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results, config = nil, files = nil) ⇒ Aggregate

Initialize aggregate result with array of validator results

Parameters:

  • results (Array<Results::Base>)

    array of validator result objects

  • config (Config, nil) (defaults to: nil)

    configuration object

  • files (Array<String>, nil) (defaults to: nil)

    array of files that were analyzed



22
23
24
25
26
# File 'lib/yard/lint/results/aggregate.rb', line 22

def initialize(results, config = nil, files = nil)
  @results = Array(results)
  @config = config
  @files = Array(files)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/yard/lint/results/aggregate.rb', line 16

def config
  @config
end

#filesObject (readonly)

Returns the value of attribute files.



16
17
18
# File 'lib/yard/lint/results/aggregate.rb', line 16

def files
  @files
end

Instance Method Details

#clean?Boolean

Check if there are no offenses

Returns:

  • (Boolean)

    true if no offenses found



47
48
49
# File 'lib/yard/lint/results/aggregate.rb', line 47

def clean?
  offenses.empty?
end

#countInteger

Total number of offenses

Returns:

  • (Integer)

    offense count



41
42
43
# File 'lib/yard/lint/results/aggregate.rb', line 41

def count
  offenses.count
end

#documentation_coverageHash

Calculate documentation coverage statistics

Returns:

  • (Hash)

    coverage statistics with :total, :documented, :coverage keys



72
73
74
75
76
77
78
79
# File 'lib/yard/lint/results/aggregate.rb', line 72

def documentation_coverage
  return @documentation_coverage if defined?(@documentation_coverage)

  return nil unless @config && !@files.empty?

  calculator = StatsCalculator.new(@config, @files)
  @documentation_coverage = calculator.calculate
end

#exit_codeInteger

Determine exit code based on configured fail_on_severity Uses the config object stored during initialization

Returns:

  • (Integer)

    0 for success, 1 for failure



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/yard/lint/results/aggregate.rb', line 84

def exit_code
  # Check minimum coverage requirement first
  if @config&.min_coverage
    coverage = documentation_coverage
    # Fail safe: if a minimum is required but coverage could not be
    # determined (e.g. the YARD stats subprocess failed), do not pass.
    return 1 if coverage.nil? || coverage[:coverage] < @config.min_coverage
  end

  return 0 if offenses.empty?
  return 0 unless @config # No config means don't fail

  fail_on = @config.fail_on_severity

  case fail_on
  when SEVERITY_ERROR
    statistics[:error].positive? ? 1 : 0
  when SEVERITY_WARNING
    (statistics[:error] + statistics[:warning]).positive? ? 1 : 0
  when SEVERITY_CONVENTION
    # Exclude 'never'-severity offenses, which are meant to run without
    # ever failing the build (they are also omitted from #statistics).
    (statistics[:error] + statistics[:warning] + statistics[:convention]).positive? ? 1 : 0
  else
    0
  end
end

#offensesArray<Hash>

Get all offenses from all validators Identical offenses are reported once: objects sharing one docstring (e.g. the reader and writer generated by attr_accessor) produce the same offense for each generated method

Returns:

  • (Array<Hash>)

    flattened array of unique offenses



33
34
35
36
37
# File 'lib/yard/lint/results/aggregate.rb', line 33

def offenses
  @offenses ||= @results.flat_map(&:offenses).uniq do |offense|
    offense.values_at(:validator, :name, :location, :location_line, :message)
  end
end

#statisticsHash

Get offense statistics by severity

Returns:

  • (Hash)

    hash with severity counts (using symbol keys)



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/yard/lint/results/aggregate.rb', line 53

def statistics
  stats = {
    error: 0,
    warning: 0,
    convention: 0,
    total: 0
  }

  offenses.each do |offense|
    severity = offense[:severity].to_sym
    stats[severity] += 1 if stats.key?(severity)
    stats[:total] += 1
  end

  stats
end