-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.go
More file actions
168 lines (152 loc) · 3.91 KB
/
Copy pathreport.go
File metadata and controls
168 lines (152 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package lint
import (
"fmt"
"strings"
"sync"
"unicode"
)
// Report collects violations and fix results from rule checks.
type Report struct {
mu sync.Mutex
violations []Violation
fixResults []FixResult
}
// NewReport creates an empty report.
func NewReport() *Report {
return &Report{}
}
// Add appends a violation to the report. Safe for concurrent use.
func (r *Report) Add(v Violation) {
v.File = sanitizeDiagnostic(v.File)
v.Message = sanitizeDiagnostic(v.Message)
v.Rule = sanitizeDiagnostic(v.Rule)
v.Found = sanitizeDiagnostic(v.Found)
v.Expected = sanitizeDiagnostic(v.Expected)
r.mu.Lock()
defer r.mu.Unlock()
r.violations = append(r.violations, v)
}
// sanitizeDiagnostic replaces control characters (CR, LF, tab, C0/C1, DEL) with a
// space. Every human-readable string field of a violation flows into line-based
// output — the text and GitHub formatters and the exported Report.String() — so a
// config- or source-derived value (a path, an import string, a parser error)
// could otherwise carry a newline and inject a second line, e.g. a forged GitHub
// Actions workflow command. Neutralized once at the source so every formatter and
// String() path is protected rather than each separately.
func sanitizeDiagnostic(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsControl(r) {
return ' '
}
return r
}, s)
}
// Violations returns all collected violations.
func (r *Report) Violations() []Violation {
r.mu.Lock()
defer r.mu.Unlock()
result := make([]Violation, len(r.violations))
copy(result, r.violations)
return result
}
// Errors returns only error-severity violations.
func (r *Report) Errors() []Violation {
r.mu.Lock()
defer r.mu.Unlock()
var result []Violation
for _, v := range r.violations {
if v.Severity == Error {
result = append(result, v)
}
}
return result
}
// Warnings returns only warning-severity violations.
func (r *Report) Warnings() []Violation {
r.mu.Lock()
defer r.mu.Unlock()
var result []Violation
for _, v := range r.violations {
if v.Severity == Warn {
result = append(result, v)
}
}
return result
}
// HasErrors returns true if any error-severity violations exist.
func (r *Report) HasErrors() bool {
r.mu.Lock()
defer r.mu.Unlock()
for _, v := range r.violations {
if v.Severity == Error {
return true
}
}
return false
}
// ErrorCount returns the number of error-severity violations.
func (r *Report) ErrorCount() int {
r.mu.Lock()
defer r.mu.Unlock()
n := 0
for _, v := range r.violations {
if v.Severity == Error {
n++
}
}
return n
}
// WarningCount returns the number of warning-severity violations.
func (r *Report) WarningCount() int {
r.mu.Lock()
defer r.mu.Unlock()
n := 0
for _, v := range r.violations {
if v.Severity == Warn {
n++
}
}
return n
}
// Total returns the total number of violations.
func (r *Report) Total() int {
r.mu.Lock()
defer r.mu.Unlock()
return len(r.violations)
}
// String returns a human-readable summary of all violations.
func (r *Report) String() string {
violations := r.Violations()
if len(violations) == 0 {
return "no violations found"
}
var sb strings.Builder
for _, v := range violations {
fmt.Fprintln(&sb, v.String())
}
fmt.Fprintf(&sb, "\n%d error(s), %d warning(s)\n", r.ErrorCount(), r.WarningCount())
return sb.String()
}
// AddFixResult records a fix result.
func (r *Report) AddFixResult(fr FixResult) {
r.mu.Lock()
defer r.mu.Unlock()
r.fixResults = append(r.fixResults, fr)
}
// FixResults returns all recorded fix results.
func (r *Report) FixResults() []FixResult {
r.mu.Lock()
defer r.mu.Unlock()
out := make([]FixResult, len(r.fixResults))
copy(out, r.fixResults)
return out
}
// ByRule groups violations by rule name.
func (r *Report) ByRule() map[string][]Violation {
violations := r.Violations()
result := make(map[string][]Violation)
for _, v := range violations {
result[v.Rule] = append(result[v.Rule], v)
}
return result
}