Skip to content

Commit 78d2118

Browse files
authored
dev: clean up (#4495)
1 parent 236e0e5 commit 78d2118

File tree

4 files changed

+187
-137
lines changed

4 files changed

+187
-137
lines changed

pkg/config/output.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"slices"
7+
"strings"
78
)
89

910
const (
@@ -49,8 +50,16 @@ func (o *Output) Validate() error {
4950
return errors.New("sort-results should be 'true' to use sort-order")
5051
}
5152

53+
validOrders := []string{"linter", "file", "severity"}
54+
55+
all := strings.Join(o.SortOrder, " ")
56+
5257
for _, order := range o.SortOrder {
53-
if !slices.Contains([]string{"linter", "file", "severity"}, order) {
58+
if strings.Count(all, order) > 1 {
59+
return fmt.Errorf("the sort-order name %q is repeated several times", order)
60+
}
61+
62+
if !slices.Contains(validOrders, order) {
5463
return fmt.Errorf("unsupported sort-order name %q", order)
5564
}
5665
}

pkg/config/output_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ func TestOutput_Validate(t *testing.T) {
3232
SortOrder: []string{"severity"},
3333
},
3434
},
35+
{
36+
desc: "multiple",
37+
settings: &Output{
38+
SortResults: true,
39+
SortOrder: []string{"file", "linter", "severity"},
40+
},
41+
},
3542
}
3643

3744
for _, test := range testCases {
@@ -66,6 +73,14 @@ func TestOutput_Validate_error(t *testing.T) {
6673
},
6774
expected: `unsupported sort-order name "a"`,
6875
},
76+
{
77+
desc: "duplicate",
78+
settings: &Output{
79+
SortResults: true,
80+
SortOrder: []string{"file", "linter", "severity", "linter"},
81+
},
82+
expected: `the sort-order name "linter" is repeated several times`,
83+
},
6984
}
7085

7186
for _, test := range testCases {

pkg/result/processors/sort_results.go

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717
// rules that can compare different properties of the Issues struct.
1818

1919
const (
20-
fileOrderName = "file"
21-
linterOrderName = "linter"
22-
linterSeverityName = "severity"
20+
orderNameFile = "file"
21+
orderNameLinter = "linter"
22+
orderNameSeverity = "severity"
2323
)
2424

2525
var _ Processor = (*SortResults)(nil)
@@ -35,11 +35,11 @@ func NewSortResults(cfg *config.Config) *SortResults {
3535
cmps: map[string][]comparator{
3636
// For sorting we are comparing (in next order):
3737
// file names, line numbers, position, and finally - giving up.
38-
fileOrderName: {&byName{}, &byLine{}, &byColumn{}},
38+
orderNameFile: {&byFileName{}, &byLine{}, &byColumn{}},
3939
// For sorting we are comparing: linter name
40-
linterOrderName: {&byLinter{}},
40+
orderNameLinter: {&byLinter{}},
4141
// For sorting we are comparing: severity
42-
linterSeverityName: {&bySeverity{}},
42+
orderNameSeverity: {&bySeverity{}},
4343
},
4444
cfg: &cfg.Output,
4545
}
@@ -52,7 +52,7 @@ func (sr SortResults) Process(issues []result.Issue) ([]result.Issue, error) {
5252
}
5353

5454
if len(sr.cfg.SortOrder) == 0 {
55-
sr.cfg.SortOrder = []string{fileOrderName}
55+
sr.cfg.SortOrder = []string{orderNameFile}
5656
}
5757

5858
var cmps []comparator
@@ -64,13 +64,13 @@ func (sr SortResults) Process(issues []result.Issue) ([]result.Issue, error) {
6464
}
6565
}
6666

67-
cmp, err := mergeComparator(cmps)
67+
cmp, err := mergeComparators(cmps)
6868
if err != nil {
6969
return nil, err
7070
}
7171

7272
sort.Slice(issues, func(i, j int) bool {
73-
return cmp.Compare(&issues[i], &issues[j]) == Less
73+
return cmp.Compare(&issues[i], &issues[j]) == less
7474
})
7575

7676
return issues, nil
@@ -83,31 +83,31 @@ func (sr SortResults) Finish() {}
8383
type compareResult int
8484

8585
const (
86-
Less compareResult = iota - 1
87-
Equal
88-
Greater
89-
None
86+
less compareResult = iota - 1
87+
equal
88+
greater
89+
none
9090
)
9191

9292
func (c compareResult) isNeutral() bool {
9393
// return true if compare result is incomparable or equal.
94-
return c == None || c == Equal
94+
return c == none || c == equal
9595
}
9696

9797
func (c compareResult) String() string {
9898
switch c {
99-
case Less:
100-
return "Less"
101-
case Equal:
102-
return "Equal"
103-
case Greater:
104-
return "Greater"
99+
case less:
100+
return "less"
101+
case equal:
102+
return "equal"
103+
case greater:
104+
return "greater"
105105
default:
106-
return "None"
106+
return "none"
107107
}
108108
}
109109

110-
// comparator describe how to implement compare for two "issues" lexicographically
110+
// comparator describes how to implement compare for two "issues".
111111
type comparator interface {
112112
Compare(a, b *result.Issue) compareResult
113113
Next() comparator
@@ -116,26 +116,25 @@ type comparator interface {
116116
}
117117

118118
var (
119-
_ comparator = (*byName)(nil)
119+
_ comparator = (*byFileName)(nil)
120120
_ comparator = (*byLine)(nil)
121121
_ comparator = (*byColumn)(nil)
122122
_ comparator = (*byLinter)(nil)
123123
_ comparator = (*bySeverity)(nil)
124124
)
125125

126-
type byName struct{ next comparator }
126+
type byFileName struct{ next comparator }
127127

128-
func (cmp *byName) Next() comparator { return cmp.next }
128+
func (cmp *byFileName) Next() comparator { return cmp.next }
129129

130-
func (cmp *byName) AddNext(c comparator) comparator {
130+
func (cmp *byFileName) AddNext(c comparator) comparator {
131131
cmp.next = c
132132
return cmp
133133
}
134134

135-
func (cmp *byName) Compare(a, b *result.Issue) compareResult {
136-
var res compareResult
137-
138-
if res = compareResult(strings.Compare(a.FilePath(), b.FilePath())); !res.isNeutral() {
135+
func (cmp *byFileName) Compare(a, b *result.Issue) compareResult {
136+
res := compareResult(strings.Compare(a.FilePath(), b.FilePath()))
137+
if !res.isNeutral() {
139138
return res
140139
}
141140

@@ -146,8 +145,8 @@ func (cmp *byName) Compare(a, b *result.Issue) compareResult {
146145
return res
147146
}
148147

149-
func (cmp *byName) String() string {
150-
return comparatorToString("byName", cmp)
148+
func (cmp *byFileName) String() string {
149+
return comparatorToString("byFileName", cmp)
151150
}
152151

153152
type byLine struct{ next comparator }
@@ -160,9 +159,8 @@ func (cmp *byLine) AddNext(c comparator) comparator {
160159
}
161160

162161
func (cmp *byLine) Compare(a, b *result.Issue) compareResult {
163-
var res compareResult
164-
165-
if res = numericCompare(a.Line(), b.Line()); !res.isNeutral() {
162+
res := numericCompare(a.Line(), b.Line())
163+
if !res.isNeutral() {
166164
return res
167165
}
168166

@@ -187,9 +185,8 @@ func (cmp *byColumn) AddNext(c comparator) comparator {
187185
}
188186

189187
func (cmp *byColumn) Compare(a, b *result.Issue) compareResult {
190-
var res compareResult
191-
192-
if res = numericCompare(a.Column(), b.Column()); !res.isNeutral() {
188+
res := numericCompare(a.Column(), b.Column())
189+
if !res.isNeutral() {
193190
return res
194191
}
195192

@@ -214,9 +211,8 @@ func (cmp *byLinter) AddNext(c comparator) comparator {
214211
}
215212

216213
func (cmp *byLinter) Compare(a, b *result.Issue) compareResult {
217-
var res compareResult
218-
219-
if res = compareResult(strings.Compare(a.FromLinter, b.FromLinter)); !res.isNeutral() {
214+
res := compareResult(strings.Compare(a.FromLinter, b.FromLinter))
215+
if !res.isNeutral() {
220216
return res
221217
}
222218

@@ -241,9 +237,8 @@ func (cmp *bySeverity) AddNext(c comparator) comparator {
241237
}
242238

243239
func (cmp *bySeverity) Compare(a, b *result.Issue) compareResult {
244-
var res compareResult
245-
246-
if res = severityCompare(a.Severity, b.Severity); !res.isNeutral() {
240+
res := severityCompare(a.Severity, b.Severity)
241+
if !res.isNeutral() {
247242
return res
248243
}
249244

@@ -258,7 +253,7 @@ func (cmp *bySeverity) String() string {
258253
return comparatorToString("bySeverity", cmp)
259254
}
260255

261-
func mergeComparator(cmps []comparator) (comparator, error) {
256+
func mergeComparators(cmps []comparator) (comparator, error) {
262257
if len(cmps) == 0 {
263258
return nil, errors.New("no comparator")
264259
}
@@ -277,14 +272,22 @@ func severityCompare(a, b string) compareResult {
277272
if slices.Contains(classic, a) && slices.Contains(classic, b) {
278273
switch {
279274
case slices.Index(classic, a) > slices.Index(classic, b):
280-
return Greater
275+
return greater
281276
case slices.Index(classic, a) < slices.Index(classic, b):
282-
return Less
277+
return less
283278
default:
284-
return Equal
279+
return equal
285280
}
286281
}
287282

283+
if slices.Contains(classic, a) {
284+
return greater
285+
}
286+
287+
if slices.Contains(classic, b) {
288+
return less
289+
}
290+
288291
return compareResult(strings.Compare(a, b))
289292
}
290293

@@ -299,16 +302,16 @@ func numericCompare(a, b int) compareResult {
299302

300303
switch {
301304
case isZeroValuesBoth || isEqual:
302-
return Equal
305+
return equal
303306
case isValuesInvalid || isZeroValueInA || isZeroValueInB:
304-
return None
307+
return none
305308
case a > b:
306-
return Greater
309+
return greater
307310
case a < b:
308-
return Less
311+
return less
309312
}
310313

311-
return Equal
314+
return equal
312315
}
313316

314317
func comparatorToString(name string, c comparator) string {

0 commit comments

Comments
 (0)