Skip to content

Commit cbf329d

Browse files
feat(lint)!: Validate returns array of message
1 parent d080584 commit cbf329d

File tree

10 files changed

+90
-57
lines changed

10 files changed

+90
-57
lines changed

formatter/default.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ func formatFailure(res *lint.Failure) string {
3030
func writeFailure(res *lint.Failure) string {
3131
str := &strings.Builder{}
3232

33-
str.WriteString("commitlint")
34-
fmt.Fprintf(str, "\n\n→ input: %s", strconv.Quote(truncate(25, res.Input())))
33+
quotedStr := strconv.Quote(truncate(25, res.Input()))
34+
35+
str.WriteString("commitlint\n")
36+
str.WriteString("\n→ input: " + quotedStr)
3537

3638
errs, warns, others := bySeverity(res)
3739

@@ -40,17 +42,42 @@ func writeFailure(res *lint.Failure) string {
4042
writeRuleFailure(str, "Other Severities", others, "?")
4143

4244
fmt.Fprintf(str, "\n\nTotal %d errors, %d warnings, %d other severities", len(errs), len(warns), len(others))
43-
return str.String()
45+
return strings.Trim(str.String(), "\n")
4446
}
4547

4648
func writeRuleFailure(w *strings.Builder, title string, resArr []*lint.RuleFailure, sign string) {
4749
if len(resArr) == 0 {
4850
return
4951
}
5052

51-
fmt.Fprint(w, "\n\n"+title+":")
52-
for _, msg := range resArr {
53-
fmt.Fprintf(w, "\n %s %s: %s", sign, msg.RuleName(), msg.Message())
53+
w.WriteString("\n\n" + title + ":")
54+
for _, ruleRes := range resArr {
55+
writeMessages(w, ruleRes, sign)
56+
}
57+
}
58+
59+
func writeMessages(w *strings.Builder, ruleRes *lint.RuleFailure, sign string) {
60+
msgs := ruleRes.Messages()
61+
62+
if len(msgs) == 0 {
63+
return
64+
}
65+
66+
space := " "
67+
68+
if len(msgs) == 1 {
69+
msg := msgs[0]
70+
// ❌ rule-name: message
71+
fmt.Fprintf(w, "\n%s %s: %s", space+sign, ruleRes.RuleName(), msg)
72+
return
73+
}
74+
75+
// ❌ rule-name:
76+
// - message1
77+
// - message2
78+
fmt.Fprintf(w, "\n%s %s:", space+sign, ruleRes.RuleName())
79+
for _, msg := range ruleRes.Messages() {
80+
fmt.Fprintf(w, "\n%s - %s", space+space, msg)
5481
}
5582
}
5683

formatter/json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (f *JSONFormatter) formRuleFailure(res []*lint.RuleFailure, includeSev bool
4040
output := make(map[string]interface{})
4141

4242
output["name"] = r.RuleName()
43-
output["message"] = r.Message()
43+
output["messages"] = r.Messages()
4444

4545
if includeSev {
4646
output["severity"] = r.Severity()

lint/linter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func (l *Linter) LintCommit(msg *Commit) (*Failure, error) {
3838
}
3939

4040
func (l *Linter) runRule(rule Rule, severity Severity, msg *Commit) (*RuleFailure, bool) {
41-
failMsg, isOK := rule.Validate(msg)
41+
failMsgs, isOK := rule.Validate(msg)
4242
if isOK {
4343
return nil, true
4444
}
45-
res := newRuleFailure(rule.Name(), failMsg, severity)
45+
res := newRuleFailure(rule.Name(), failMsgs, severity)
4646
return res, false
4747
}
4848

@@ -57,7 +57,7 @@ func (l *Linter) parserErrorRule(commitMsg string, err error) (*Failure, error)
5757
errMsg = err.Error()
5858
}
5959

60-
ruleFail := newRuleFailure("parser", errMsg, SeverityError)
60+
ruleFail := newRuleFailure("parser", []string{errMsg}, SeverityError)
6161
res.add(ruleFail)
6262

6363
return res, nil

lint/result.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ func (res *Failure) RuleFailures() []*RuleFailure { return res.failures }
3131
type RuleFailure struct {
3232
name string
3333
severity Severity
34-
message string
34+
messages []string
3535
}
3636

37-
func newRuleFailure(name, msg string, severity Severity) *RuleFailure {
37+
func newRuleFailure(name string, msgs []string, severity Severity) *RuleFailure {
3838
return &RuleFailure{
3939
name: name,
40-
message: msg,
40+
messages: msgs,
4141
severity: severity,
4242
}
4343
}
@@ -48,5 +48,5 @@ func (r *RuleFailure) RuleName() string { return r.name }
4848
// Severity returns severity of the Rule Failure
4949
func (r *RuleFailure) Severity() Severity { return r.severity }
5050

51-
// Message returns the error message of failed rule
52-
func (r *RuleFailure) Message() string { return r.message }
51+
// Messages returns the error message of failed rule
52+
func (r *RuleFailure) Messages() []string { return r.messages }

lint/rule.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type Rule interface {
1010
// Apply is called before Validate
1111
Apply(arg interface{}, flags map[string]interface{}) error
1212

13-
// Validate validates the rule for given message
14-
// if given message is valid, return true and result string is ignored
15-
// if invalid, return a error result with false
16-
Validate(msg *Commit) (result string, isValid bool)
13+
// Validate validates the rule for given commit message
14+
// if given commit is valid, return true and messages slice are ignored
15+
// if invalid, return a error messages with false
16+
Validate(msg *Commit) (messages []string, isValid bool)
1717
}

rule/charset.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ type ScopeCharsetRule struct {
1616
func (r *ScopeCharsetRule) Name() string { return "scope-charset" }
1717

1818
// Validate validates ScopeCharsetRule
19-
func (r *ScopeCharsetRule) Validate(msg *lint.Commit) (string, bool) {
19+
func (r *ScopeCharsetRule) Validate(msg *lint.Commit) ([]string, bool) {
2020
invalidChar, isValid := checkCharset(r.Charset, msg.Header.Scope)
2121
if !isValid {
2222
errMsg := fmt.Sprintf("scope contains invalid char '%s', allowed chars are [%s]", invalidChar, r.Charset)
23-
return errMsg, false
23+
return []string{errMsg}, false
2424
}
25-
return "", true
25+
return nil, true
2626
}
2727

2828
// Apply sets the needed argument for the rule
@@ -43,13 +43,13 @@ type TypeCharsetRule struct {
4343
func (r *TypeCharsetRule) Name() string { return "type-charset" }
4444

4545
// Validate validates TypeCharsetRule
46-
func (r *TypeCharsetRule) Validate(msg *lint.Commit) (string, bool) {
46+
func (r *TypeCharsetRule) Validate(msg *lint.Commit) ([]string, bool) {
4747
invalidChar, isValid := checkCharset(r.Charset, msg.Header.Type)
4848
if !isValid {
4949
errMsg := fmt.Sprintf("type contains invalid char '%s', allowed chars are [%s]", invalidChar, r.Charset)
50-
return errMsg, false
50+
return []string{errMsg}, false
5151
}
52-
return "", true
52+
return nil, true
5353
}
5454

5555
// Apply sets the needed argument for the rule

rule/enum.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ type ScopeEnumRule struct {
1818
func (r *ScopeEnumRule) Name() string { return "scope-enum" }
1919

2020
// Validate validates ScopeEnumRule
21-
func (r *ScopeEnumRule) Validate(msg *lint.Commit) (string, bool) {
21+
func (r *ScopeEnumRule) Validate(msg *lint.Commit) ([]string, bool) {
2222
if msg.Header.Scope == "" {
2323
if r.AllowEmpty {
24-
return "", true
24+
return nil, true
2525
}
2626
errMsg := fmt.Sprintf("empty scope is not allowed, you can use one of %v", r.Scopes)
27-
return errMsg, false
27+
return []string{errMsg}, false
2828
}
2929

3030
isFound := search(r.Scopes, msg.Header.Scope)
3131
if !isFound {
3232
errMsg := fmt.Sprintf("scope '%s' is not allowed, you can use one of %v", msg.Header.Scope, r.Scopes)
33-
return errMsg, false
33+
return []string{errMsg}, false
3434
}
35-
return "", true
35+
return nil, true
3636
}
3737

3838
// Apply sets the needed argument for the rule
@@ -64,13 +64,13 @@ type TypeEnumRule struct {
6464
func (r *TypeEnumRule) Name() string { return "type-enum" }
6565

6666
// Validate validates TypeEnumRule
67-
func (r *TypeEnumRule) Validate(msg *lint.Commit) (string, bool) {
67+
func (r *TypeEnumRule) Validate(msg *lint.Commit) ([]string, bool) {
6868
isFound := search(r.Types, msg.Header.Type)
6969
if !isFound {
7070
errMsg := fmt.Sprintf("type '%s' is not allowed, you can use one of %v", msg.Header.Type, r.Types)
71-
return errMsg, false
71+
return []string{errMsg}, false
7272
}
73-
return "", true
73+
return nil, true
7474
}
7575

7676
// Apply sets the needed argument for the rule

rule/max_length.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type HeadMaxLenRule struct {
1515
func (r *HeadMaxLenRule) Name() string { return "header-max-length" }
1616

1717
// Validate validates HeadMaxLenRule
18-
func (r *HeadMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
18+
func (r *HeadMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
1919
return checkMaxLen(r.CheckLen, msg.Header.FullHeader)
2020
}
2121

@@ -37,7 +37,7 @@ type BodyMaxLenRule struct {
3737
func (r *BodyMaxLenRule) Name() string { return "body-max-length" }
3838

3939
// Validate validates BodyMaxLenRule
40-
func (r *BodyMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
40+
func (r *BodyMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
4141
return checkMaxLen(r.CheckLen, msg.Body)
4242
}
4343

@@ -59,7 +59,7 @@ type FooterMaxLenRule struct {
5959
func (r *FooterMaxLenRule) Name() string { return "footer-max-length" }
6060

6161
// Validate validates FooterMaxLenRule
62-
func (r *FooterMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
62+
func (r *FooterMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
6363
return checkMaxLen(r.CheckLen, msg.Footer.FullFooter)
6464
}
6565

@@ -81,7 +81,7 @@ type TypeMaxLenRule struct {
8181
func (r *TypeMaxLenRule) Name() string { return "type-max-length" }
8282

8383
// Validate validates TypeMaxLenRule
84-
func (r *TypeMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
84+
func (r *TypeMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
8585
return checkMaxLen(r.CheckLen, msg.Header.Type)
8686
}
8787

@@ -103,7 +103,7 @@ type ScopeMaxLenRule struct {
103103
func (r *ScopeMaxLenRule) Name() string { return "scope-max-length" }
104104

105105
// Validate validates ScopeMaxLenRule
106-
func (r *ScopeMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
106+
func (r *ScopeMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
107107
return checkMaxLen(r.CheckLen, msg.Header.Scope)
108108
}
109109

@@ -125,7 +125,7 @@ type DescriptionMaxLenRule struct {
125125
func (r *DescriptionMaxLenRule) Name() string { return "description-max-length" }
126126

127127
// Validate validates DescriptionMaxLenRule
128-
func (r *DescriptionMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
128+
func (r *DescriptionMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
129129
return checkMaxLen(r.CheckLen, msg.Header.Description)
130130
}
131131

@@ -138,14 +138,14 @@ func (r *DescriptionMaxLenRule) Apply(arg interface{}, flags map[string]interfac
138138
return nil
139139
}
140140

141-
func checkMaxLen(checkLen int, toCheck string) (string, bool) {
141+
func checkMaxLen(checkLen int, toCheck string) ([]string, bool) {
142142
if checkLen < 0 {
143-
return "", true
143+
return nil, true
144144
}
145145
actualLen := len(toCheck)
146146
if actualLen > checkLen {
147147
errMsg := fmt.Sprintf("length is %d, should have less than %d chars", actualLen, checkLen)
148-
return errMsg, false
148+
return []string{errMsg}, false
149149
}
150-
return "", true
150+
return nil, true
151151
}

rule/max_line_length.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type BodyMaxLineLenRule struct {
1616
func (r *BodyMaxLineLenRule) Name() string { return "body-max-line-length" }
1717

1818
// Validate validates BodyMaxLineLenRule rule
19-
func (r *BodyMaxLineLenRule) Validate(msg *lint.Commit) (string, bool) {
19+
func (r *BodyMaxLineLenRule) Validate(msg *lint.Commit) ([]string, bool) {
2020
return checkMaxLineLength(r.CheckLen, msg.Body)
2121
}
2222

@@ -38,7 +38,7 @@ type FooterMaxLineLenRule struct {
3838
func (r *FooterMaxLineLenRule) Name() string { return "footer-max-line-length" }
3939

4040
// Validate validates FooterMaxLineLenRule rule
41-
func (r *FooterMaxLineLenRule) Validate(msg *lint.Commit) (string, bool) {
41+
func (r *FooterMaxLineLenRule) Validate(msg *lint.Commit) ([]string, bool) {
4242
return checkMaxLineLength(r.CheckLen, msg.Footer.FullFooter)
4343
}
4444

@@ -51,14 +51,20 @@ func (r *FooterMaxLineLenRule) Apply(arg interface{}, flags map[string]interface
5151
return nil
5252
}
5353

54-
func checkMaxLineLength(checkLen int, toCheck string) (string, bool) {
54+
func checkMaxLineLength(checkLen int, toCheck string) ([]string, bool) {
5555
lines := strings.Split(toCheck, "\n")
56+
57+
msgs := []string{}
5658
for index, line := range lines {
5759
actualLen := len(line)
5860
if actualLen > checkLen {
5961
errMsg := fmt.Sprintf("in line %d, length is %d, should have less than %d chars", index+1, actualLen, checkLen)
60-
return errMsg, false
62+
msgs = append(msgs, errMsg)
6163
}
6264
}
63-
return "", true
65+
66+
if len(msgs) == 0 {
67+
return nil, true
68+
}
69+
return msgs, false
6470
}

rule/min_length.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type HeadMinLenRule struct {
1515
func (r *HeadMinLenRule) Name() string { return "header-min-length" }
1616

1717
// Validate validates HeadMinLenRule
18-
func (r *HeadMinLenRule) Validate(msg *lint.Commit) (string, bool) {
18+
func (r *HeadMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
1919
return checkMinLen(r.CheckLen, msg.Header.FullHeader)
2020
}
2121

@@ -37,7 +37,7 @@ type BodyMinLenRule struct {
3737
func (r *BodyMinLenRule) Name() string { return "body-min-length" }
3838

3939
// Validate validates BodyMinLenRule
40-
func (r *BodyMinLenRule) Validate(msg *lint.Commit) (string, bool) {
40+
func (r *BodyMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
4141
return checkMinLen(r.CheckLen, msg.Body)
4242
}
4343

@@ -59,7 +59,7 @@ type FooterMinLenRule struct {
5959
func (r *FooterMinLenRule) Name() string { return "footer-min-length" }
6060

6161
// Validate validates FooterMinLenRule
62-
func (r *FooterMinLenRule) Validate(msg *lint.Commit) (string, bool) {
62+
func (r *FooterMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
6363
return checkMinLen(r.CheckLen, msg.Footer.FullFooter)
6464
}
6565

@@ -81,7 +81,7 @@ type TypeMinLenRule struct {
8181
func (r *TypeMinLenRule) Name() string { return "type-min-length" }
8282

8383
// Validate validates TypeMinLenRule
84-
func (r *TypeMinLenRule) Validate(msg *lint.Commit) (string, bool) {
84+
func (r *TypeMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
8585
return checkMinLen(r.CheckLen, msg.Header.Type)
8686
}
8787

@@ -103,7 +103,7 @@ type ScopeMinLenRule struct {
103103
func (r *ScopeMinLenRule) Name() string { return "scope-min-length" }
104104

105105
// Validate validates ScopeMinLenRule
106-
func (r *ScopeMinLenRule) Validate(msg *lint.Commit) (string, bool) {
106+
func (r *ScopeMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
107107
return checkMinLen(r.CheckLen, msg.Header.Scope)
108108
}
109109

@@ -125,7 +125,7 @@ type DescriptionMinLenRule struct {
125125
func (r *DescriptionMinLenRule) Name() string { return "description-min-length" }
126126

127127
// Validate validates DescriptionMinLenRule
128-
func (r *DescriptionMinLenRule) Validate(msg *lint.Commit) (string, bool) {
128+
func (r *DescriptionMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
129129
return checkMinLen(r.CheckLen, msg.Header.Description)
130130
}
131131

@@ -138,11 +138,11 @@ func (r *DescriptionMinLenRule) Apply(arg interface{}, flags map[string]interfac
138138
return nil
139139
}
140140

141-
func checkMinLen(checkLen int, toCheck string) (string, bool) {
141+
func checkMinLen(checkLen int, toCheck string) ([]string, bool) {
142142
actualLen := len(toCheck)
143143
if actualLen < checkLen {
144144
errMsg := fmt.Sprintf("length is %d, should have atleast %d chars", actualLen, checkLen)
145-
return errMsg, false
145+
return []string{errMsg}, false
146146
}
147-
return "", true
147+
return nil, true
148148
}

0 commit comments

Comments
 (0)