Class: Browsable::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/browsable/report.rb

Overview

The aggregated result of an audit: every Finding produced by every analyzer, plus a record of any analyzer or source that was skipped.

A Report makes no decisions. It tells the user what their code requires and how that compares against their target; the formatters present it and the exit-code policy lives entirely in the caller’s chosen –fail-on value.

Defined Under Namespace

Classes: Skip, Suggestion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(findings: [], skips: [], notes: [], policies: [], target: nil, root: nil, config_file: nil) ⇒ Report

Returns a new instance of Report.

Parameters:

  • notes (Array<String>) (defaults to: [])

    caveats about the run itself (e.g. a target that could not be inferred) — distinct from per-file findings.

  • policies (Array<PolicyScanner::Policy>) (defaults to: [])

    every allow_browser callsite discovered across the app’s controllers — the policy landscape.



24
25
26
27
28
29
30
31
32
33
# File 'lib/browsable/report.rb', line 24

def initialize(findings: [], skips: [], notes: [], policies: [],
               target: nil, root: nil, config_file: nil)
  @findings = findings
  @skips = skips
  @notes = notes
  @policies = policies
  @target = target
  @root = root
  @config_file = config_file
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



18
19
20
# File 'lib/browsable/report.rb', line 18

def config_file
  @config_file
end

#findingsObject (readonly)

Returns the value of attribute findings.



18
19
20
# File 'lib/browsable/report.rb', line 18

def findings
  @findings
end

#notesObject (readonly)

Returns the value of attribute notes.



18
19
20
# File 'lib/browsable/report.rb', line 18

def notes
  @notes
end

#policiesObject (readonly)

Returns the value of attribute policies.



18
19
20
# File 'lib/browsable/report.rb', line 18

def policies
  @policies
end

#rootObject (readonly)

Returns the value of attribute root.



18
19
20
# File 'lib/browsable/report.rb', line 18

def root
  @root
end

#skipsObject (readonly)

Returns the value of attribute skips.



18
19
20
# File 'lib/browsable/report.rb', line 18

def skips
  @skips
end

#targetObject (readonly)

Returns the value of attribute target.



18
19
20
# File 'lib/browsable/report.rb', line 18

def target
  @target
end

Instance Method Details

#as_jsonObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/browsable/report.rb', line 74

def as_json
  {
    target: target&.as_json,
    notes: notes,
    summary: {
      errors: errors.size,
      warnings: warnings.size,
      infos: infos.size,
      files: findings_by_file.size
    },
    findings: findings.map(&:as_json),
    skips: skips.map { |skip| { kind: skip.kind.to_s, reason: skip.reason } },
    policies: policies.map { |policy| policy_as_json(policy) },
    suggested_policy: suggestion && { line: suggestion.line, bumps: suggestion.bumps }
  }
end

#empty?Boolean

Returns:

  • (Boolean)


39
# File 'lib/browsable/report.rb', line 39

def empty? = findings.empty?

#errorsObject



35
# File 'lib/browsable/report.rb', line 35

def errors   = findings.select(&:error?)

#exit_code(fail_on:) ⇒ Object

Exit code implementing the –fail-on policy.



51
52
53
54
55
56
57
58
59
60
# File 'lib/browsable/report.rb', line 51

def exit_code(fail_on:)
  case fail_on.to_s
  when "warning"
    errors.any? || warnings.any? ? 1 : 0
  when "error"
    errors.any? ? 1 : 0
  else
    0
  end
end

#findings_by_fileObject

Findings grouped by file path, files sorted, findings sorted by position.



42
43
44
45
46
47
48
# File 'lib/browsable/report.rb', line 42

def findings_by_file
  findings
    .group_by(&:file)
    .sort_by(&:first)
    .to_h
    .transform_values { |group| group.sort_by { |f| [f.line, f.column] } }
end

#infosObject



37
# File 'lib/browsable/report.rb', line 37

def infos    = findings.select(&:info?)

#suggestionObject

An allow_browser line that raises the offending browsers just enough to cover every below-target error, leaving the other browsers untouched.

Returns nil when no error carries comparable version data — CSS/JS findings come from stylelint/eslint, which do not expose exact versions, so a suggestion can only be derived from HTML/ERB findings.



68
69
70
71
72
# File 'lib/browsable/report.rb', line 68

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

  @suggestion = build_suggestion
end

#warningsObject



36
# File 'lib/browsable/report.rb', line 36

def warnings = findings.select(&:warning?)