@@ -9,78 +9,85 @@ import (
9
9
"github.com/conventionalcommit/commitlint/lint"
10
10
)
11
11
12
+ const (
13
+ truncateSize = 25
14
+ )
15
+
12
16
// DefaultFormatter represent default formatter
13
17
type DefaultFormatter struct {}
14
18
15
19
// Name returns name of formatter
16
20
func (f * DefaultFormatter ) Name () string { return "default" }
17
21
18
22
// Format formats the lint.Failure
19
- func (f * DefaultFormatter ) Format (res * lint.Result ) (string , error ) {
20
- return formatFailure (res ), nil
23
+ func (f * DefaultFormatter ) Format (result * lint.Result ) (string , error ) {
24
+ return f . formatFailure (result . Input (), result . Issues () ), nil
21
25
}
22
26
23
- func formatFailure (res * lint.Result ) string {
24
- if res . IsOK () {
27
+ func ( f * DefaultFormatter ) formatFailure (msg string , issues [] * lint.Issue ) string {
28
+ if len ( issues ) == 0 {
25
29
return " ✔ commit message"
26
30
}
27
- return writeFailure (res )
31
+ return f . writeFailure (msg , issues )
28
32
}
29
33
30
- func writeFailure (res * lint.Result ) string {
34
+ func ( f * DefaultFormatter ) writeFailure (msg string , issues [] * lint.Issue ) string {
31
35
str := & strings.Builder {}
32
36
33
- quotedStr := strconv .Quote (truncate (25 , res . Input () ))
37
+ quotedStr := strconv .Quote (truncate (truncateSize , msg ))
34
38
35
39
str .WriteString ("commitlint\n " )
36
40
str .WriteString ("\n → input: " + quotedStr )
37
41
38
- errs , warns , others := bySeverity (res )
42
+ errs , warns , others := f . bySeverity (issues )
39
43
40
- writeRuleFailure (str , "❌" , "Errors" , errs )
41
- writeRuleFailure (str , "!" , "Warnings" , warns )
42
- writeRuleFailure (str , "?" , "Other Severities" , others )
44
+ f . writeIssues (str , "❌" , "Errors" , errs )
45
+ f . writeIssues (str , "!" , "Warnings" , warns )
46
+ f . writeIssues (str , "?" , "Other Severities" , others )
43
47
44
48
fmt .Fprintf (str , "\n \n Total %d errors, %d warnings, %d other severities" , len (errs ), len (warns ), len (others ))
45
49
return strings .Trim (str .String (), "\n " )
46
50
}
47
51
48
- func writeRuleFailure ( w * strings.Builder , sign , title string , resArr []* lint.Issue ) {
49
- if len (resArr ) == 0 {
52
+ func ( f * DefaultFormatter ) writeIssues ( w * strings.Builder , sign , title string , issues []* lint.Issue ) {
53
+ if len (issues ) == 0 {
50
54
return
51
55
}
52
56
53
57
w .WriteString ("\n \n " + title + ":" )
54
- for _ , ruleRes := range resArr {
55
- writeMessages (w , ruleRes , sign )
58
+ for _ , issue := range issues {
59
+ f . writeIssue (w , sign , issue )
56
60
}
57
61
}
58
62
59
- func writeMessages (w * strings.Builder , ruleRes * lint.Issue , sign string ) {
60
- msgs := ruleRes .Message ()
61
-
62
- if len (msgs ) == 0 {
63
- return
64
- }
65
-
63
+ func (f * DefaultFormatter ) writeIssue (w * strings.Builder , sign string , issue * lint.Issue ) {
66
64
space := " "
67
65
68
- if len (msgs ) == 1 {
69
- msg := msgs [0 ]
70
- // ❌ rule-name: message
71
- fmt .Fprintf (w , "\n %s %s: %s" , space + sign , ruleRes .Name (), msg )
72
- return
73
- }
74
-
75
66
// ❌ rule-name:
76
67
// - message1
77
68
// - message2
78
- fmt .Fprintf (w , "\n %s %s:" , space + sign , ruleRes .Name ())
79
- for _ , msg := range ruleRes .Message () {
69
+
70
+ fmt .Fprintf (w , "\n %s %s:" , space + sign , issue .Name ())
71
+ for _ , msg := range issue .Message () {
80
72
fmt .Fprintf (w , "\n %s - %s" , space + space , msg )
81
73
}
82
74
}
83
75
76
+ // bySeverity returns all messages with given severity
77
+ func (f * DefaultFormatter ) bySeverity (issues []* lint.Issue ) (errs , warns , others []* lint.Issue ) {
78
+ for _ , r := range issues {
79
+ switch r .Severity () {
80
+ case lint .SeverityError :
81
+ errs = append (errs , r )
82
+ case lint .SeverityWarn :
83
+ warns = append (warns , r )
84
+ default :
85
+ others = append (others , r )
86
+ }
87
+ }
88
+ return errs , warns , others
89
+ }
90
+
84
91
func truncate (maxSize int , input string ) string {
85
92
if len (input ) < maxSize {
86
93
return input
0 commit comments