@@ -2,6 +2,7 @@ package formatter
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"strings"
6
7
7
8
"github.com/conventionalcommit/commitlint/lint"
@@ -14,40 +15,35 @@ type JSONFormatter struct{}
14
15
func (f * JSONFormatter ) Name () string { return "json" }
15
16
16
17
// Format formats the lint.Result
17
- func (f * JSONFormatter ) Format (res * lint.Result ) (string , error ) {
18
+ func (f * JSONFormatter ) Format (result * lint.Result ) (string , error ) {
18
19
output := make (map [string ]interface {}, 4 )
19
20
20
- errs , warns , others := bySeverity (res )
21
+ output ["input" ] = result .Input ()
22
+ output ["valid" ] = result .IsOK ()
23
+ output ["issues" ] = f .formatIssue (result .Issues ())
21
24
22
- output ["input" ] = res .Input ()
23
- output ["status" ] = res .IsOK ()
24
-
25
- output ["errors" ] = f .formRuleFailure (errs , false )
26
- output ["warnings" ] = f .formRuleFailure (warns , false )
27
- output ["others" ] = f .formRuleFailure (others , true )
28
-
29
- msg , err := json .Marshal (output )
25
+ formatted , err := json .Marshal (output )
30
26
if err != nil {
31
- return "" , err
27
+ return "" , fmt . Errorf ( "json formatting failed: %w" , err )
32
28
}
33
- return strings .Trim (string (msg ), "\n " ), nil
29
+ return strings .Trim (string (formatted ), "\n " ), nil
34
30
}
35
31
36
- func (f * JSONFormatter ) formRuleFailure ( res []* lint.Issue , includeSev bool ) [] map [ string ]interface {} {
37
- outs := make ([]map [ string ] interface {}, 0 , len (res ))
32
+ func (f * JSONFormatter ) formatIssue ( issues []* lint.Issue ) []interface {} {
33
+ formattedIssues := make ([]interface {}, 0 , len (issues ))
38
34
39
- for _ , r := range res {
35
+ for _ , issue := range issues {
40
36
output := make (map [string ]interface {})
41
37
42
- output ["name" ] = r .Name ()
43
- output ["messages " ] = r . Message ()
38
+ output ["name" ] = issue .Name ()
39
+ output ["severity " ] = issue . Severity ()
44
40
45
- if includeSev {
46
- output ["severity " ] = r . Severity ()
41
+ if len ( issue . Message ()) > 0 {
42
+ output ["messages " ] = issue . Message ()
47
43
}
48
44
49
- outs = append (outs , output )
45
+ formattedIssues = append (formattedIssues , output )
50
46
}
51
47
52
- return outs
48
+ return formattedIssues
53
49
}
0 commit comments