Skip to content

Commit 8992194

Browse files
authored
Merge pull request #83 from golangci/feature/improve-json-output
Write JSON output more compactly and output object, not array
2 parents 2a8eec1 + 541656c commit 8992194

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

Diff for: pkg/golinters/dupl.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (d Dupl) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue,
4141
Filename: i.From.Filename(),
4242
Line: i.From.LineStart(),
4343
},
44-
LineRange: result.Range{
44+
LineRange: &result.Range{
4545
From: i.From.LineStart(),
4646
To: i.From.LineEnd(),
4747
},

Diff for: pkg/golinters/gas.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ func (lint Gas) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issu
4141
res := make([]result.Issue, 0, len(issues))
4242
for _, i := range issues {
4343
text := fmt.Sprintf("%s: %s", i.RuleID, i.What) // TODO: use severity and confidence
44-
var r result.Range
44+
var r *result.Range
4545
line, err := strconv.Atoi(i.Line)
4646
if err != nil {
47+
r = &result.Range{}
4748
if n, rerr := fmt.Sscanf(i.Line, "%d-%d", &r.From, &r.To); rerr != nil || n != 2 {
4849
logutils.HiddenWarnf("Can't convert gas line number %q of %v to int: %s", i.Line, i, err)
4950
continue

Diff for: pkg/printers/json.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,25 @@ func NewJSON() *JSON {
1414
return &JSON{}
1515
}
1616

17+
type JSONResult struct {
18+
Issues []result.Issue
19+
}
20+
1721
func (JSON) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
18-
var allIssues []result.Issue
22+
allIssues := []result.Issue{}
1923
for i := range issues {
2024
allIssues = append(allIssues, i)
2125
}
22-
outputJSON, err := json.Marshal(allIssues)
26+
27+
res := JSONResult{
28+
Issues: allIssues,
29+
}
30+
31+
outputJSON, err := json.Marshal(res)
2332
if err != nil {
2433
return false, err
2534
}
35+
2636
fmt.Fprint(StdOut, string(outputJSON))
2737
return len(allIssues) != 0, nil
2838
}

Diff for: pkg/result/issue.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type Issue struct {
1111
Text string
1212

1313
Pos token.Position
14-
LineRange Range
15-
HunkPos int
14+
LineRange *Range `json:",omitempty"`
15+
HunkPos int `json:",omitempty"`
1616
}
1717

1818
func (i Issue) FilePath() string {
@@ -24,12 +24,19 @@ func (i Issue) Line() int {
2424
}
2525

2626
func (i Issue) GetLineRange() Range {
27+
if i.LineRange == nil {
28+
return Range{
29+
From: i.Line(),
30+
To: i.Line(),
31+
}
32+
}
33+
2734
if i.LineRange.From == 0 {
2835
return Range{
2936
From: i.Line(),
3037
To: i.Line(),
3138
}
3239
}
3340

34-
return i.LineRange
41+
return *i.LineRange
3542
}

0 commit comments

Comments
 (0)