Skip to content

Commit

Permalink
feat(lint)!: Validate returns array of message
Browse files Browse the repository at this point in the history
  • Loading branch information
muthukrishnan24 committed Nov 2, 2021
1 parent d080584 commit cbf329d
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 57 deletions.
39 changes: 33 additions & 6 deletions formatter/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ func formatFailure(res *lint.Failure) string {
func writeFailure(res *lint.Failure) string {
str := &strings.Builder{}

str.WriteString("commitlint")
fmt.Fprintf(str, "\n\n→ input: %s", strconv.Quote(truncate(25, res.Input())))
quotedStr := strconv.Quote(truncate(25, res.Input()))

str.WriteString("commitlint\n")
str.WriteString("\n→ input: " + quotedStr)

errs, warns, others := bySeverity(res)

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

fmt.Fprintf(str, "\n\nTotal %d errors, %d warnings, %d other severities", len(errs), len(warns), len(others))
return str.String()
return strings.Trim(str.String(), "\n")
}

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

fmt.Fprint(w, "\n\n"+title+":")
for _, msg := range resArr {
fmt.Fprintf(w, "\n %s %s: %s", sign, msg.RuleName(), msg.Message())
w.WriteString("\n\n" + title + ":")
for _, ruleRes := range resArr {
writeMessages(w, ruleRes, sign)
}
}

func writeMessages(w *strings.Builder, ruleRes *lint.RuleFailure, sign string) {
msgs := ruleRes.Messages()

if len(msgs) == 0 {
return
}

space := " "

if len(msgs) == 1 {
msg := msgs[0]
// ❌ rule-name: message
fmt.Fprintf(w, "\n%s %s: %s", space+sign, ruleRes.RuleName(), msg)
return
}

// ❌ rule-name:
// - message1
// - message2
fmt.Fprintf(w, "\n%s %s:", space+sign, ruleRes.RuleName())
for _, msg := range ruleRes.Messages() {
fmt.Fprintf(w, "\n%s - %s", space+space, msg)
}
}

Expand Down
2 changes: 1 addition & 1 deletion formatter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (f *JSONFormatter) formRuleFailure(res []*lint.RuleFailure, includeSev bool
output := make(map[string]interface{})

output["name"] = r.RuleName()
output["message"] = r.Message()
output["messages"] = r.Messages()

if includeSev {
output["severity"] = r.Severity()
Expand Down
6 changes: 3 additions & 3 deletions lint/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func (l *Linter) LintCommit(msg *Commit) (*Failure, error) {
}

func (l *Linter) runRule(rule Rule, severity Severity, msg *Commit) (*RuleFailure, bool) {
failMsg, isOK := rule.Validate(msg)
failMsgs, isOK := rule.Validate(msg)
if isOK {
return nil, true
}
res := newRuleFailure(rule.Name(), failMsg, severity)
res := newRuleFailure(rule.Name(), failMsgs, severity)
return res, false
}

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

ruleFail := newRuleFailure("parser", errMsg, SeverityError)
ruleFail := newRuleFailure("parser", []string{errMsg}, SeverityError)
res.add(ruleFail)

return res, nil
Expand Down
10 changes: 5 additions & 5 deletions lint/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func (res *Failure) RuleFailures() []*RuleFailure { return res.failures }
type RuleFailure struct {
name string
severity Severity
message string
messages []string
}

func newRuleFailure(name, msg string, severity Severity) *RuleFailure {
func newRuleFailure(name string, msgs []string, severity Severity) *RuleFailure {
return &RuleFailure{
name: name,
message: msg,
messages: msgs,
severity: severity,
}
}
Expand All @@ -48,5 +48,5 @@ func (r *RuleFailure) RuleName() string { return r.name }
// Severity returns severity of the Rule Failure
func (r *RuleFailure) Severity() Severity { return r.severity }

// Message returns the error message of failed rule
func (r *RuleFailure) Message() string { return r.message }
// Messages returns the error message of failed rule
func (r *RuleFailure) Messages() []string { return r.messages }
8 changes: 4 additions & 4 deletions lint/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type Rule interface {
// Apply is called before Validate
Apply(arg interface{}, flags map[string]interface{}) error

// Validate validates the rule for given message
// if given message is valid, return true and result string is ignored
// if invalid, return a error result with false
Validate(msg *Commit) (result string, isValid bool)
// Validate validates the rule for given commit message
// if given commit is valid, return true and messages slice are ignored
// if invalid, return a error messages with false
Validate(msg *Commit) (messages []string, isValid bool)
}
12 changes: 6 additions & 6 deletions rule/charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ type ScopeCharsetRule struct {
func (r *ScopeCharsetRule) Name() string { return "scope-charset" }

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

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

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

// Apply sets the needed argument for the rule
Expand Down
16 changes: 8 additions & 8 deletions rule/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ type ScopeEnumRule struct {
func (r *ScopeEnumRule) Name() string { return "scope-enum" }

// Validate validates ScopeEnumRule
func (r *ScopeEnumRule) Validate(msg *lint.Commit) (string, bool) {
func (r *ScopeEnumRule) Validate(msg *lint.Commit) ([]string, bool) {
if msg.Header.Scope == "" {
if r.AllowEmpty {
return "", true
return nil, true
}
errMsg := fmt.Sprintf("empty scope is not allowed, you can use one of %v", r.Scopes)
return errMsg, false
return []string{errMsg}, false
}

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

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

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

// Apply sets the needed argument for the rule
Expand Down
20 changes: 10 additions & 10 deletions rule/max_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type HeadMaxLenRule struct {
func (r *HeadMaxLenRule) Name() string { return "header-max-length" }

// Validate validates HeadMaxLenRule
func (r *HeadMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *HeadMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Header.FullHeader)
}

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

// Validate validates BodyMaxLenRule
func (r *BodyMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *BodyMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Body)
}

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

// Validate validates FooterMaxLenRule
func (r *FooterMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *FooterMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Footer.FullFooter)
}

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

// Validate validates TypeMaxLenRule
func (r *TypeMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *TypeMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Header.Type)
}

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

// Validate validates ScopeMaxLenRule
func (r *ScopeMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *ScopeMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Header.Scope)
}

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

// Validate validates DescriptionMaxLenRule
func (r *DescriptionMaxLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *DescriptionMaxLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMaxLen(r.CheckLen, msg.Header.Description)
}

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

func checkMaxLen(checkLen int, toCheck string) (string, bool) {
func checkMaxLen(checkLen int, toCheck string) ([]string, bool) {
if checkLen < 0 {
return "", true
return nil, true
}
actualLen := len(toCheck)
if actualLen > checkLen {
errMsg := fmt.Sprintf("length is %d, should have less than %d chars", actualLen, checkLen)
return errMsg, false
return []string{errMsg}, false
}
return "", true
return nil, true
}
16 changes: 11 additions & 5 deletions rule/max_line_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type BodyMaxLineLenRule struct {
func (r *BodyMaxLineLenRule) Name() string { return "body-max-line-length" }

// Validate validates BodyMaxLineLenRule rule
func (r *BodyMaxLineLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *BodyMaxLineLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMaxLineLength(r.CheckLen, msg.Body)
}

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

// Validate validates FooterMaxLineLenRule rule
func (r *FooterMaxLineLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *FooterMaxLineLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMaxLineLength(r.CheckLen, msg.Footer.FullFooter)
}

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

func checkMaxLineLength(checkLen int, toCheck string) (string, bool) {
func checkMaxLineLength(checkLen int, toCheck string) ([]string, bool) {
lines := strings.Split(toCheck, "\n")

msgs := []string{}
for index, line := range lines {
actualLen := len(line)
if actualLen > checkLen {
errMsg := fmt.Sprintf("in line %d, length is %d, should have less than %d chars", index+1, actualLen, checkLen)
return errMsg, false
msgs = append(msgs, errMsg)
}
}
return "", true

if len(msgs) == 0 {
return nil, true
}
return msgs, false
}
18 changes: 9 additions & 9 deletions rule/min_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type HeadMinLenRule struct {
func (r *HeadMinLenRule) Name() string { return "header-min-length" }

// Validate validates HeadMinLenRule
func (r *HeadMinLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *HeadMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Header.FullHeader)
}

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

// Validate validates BodyMinLenRule
func (r *BodyMinLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *BodyMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Body)
}

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

// Validate validates FooterMinLenRule
func (r *FooterMinLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *FooterMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Footer.FullFooter)
}

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

// Validate validates TypeMinLenRule
func (r *TypeMinLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *TypeMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Header.Type)
}

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

// Validate validates ScopeMinLenRule
func (r *ScopeMinLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *ScopeMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Header.Scope)
}

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

// Validate validates DescriptionMinLenRule
func (r *DescriptionMinLenRule) Validate(msg *lint.Commit) (string, bool) {
func (r *DescriptionMinLenRule) Validate(msg *lint.Commit) ([]string, bool) {
return checkMinLen(r.CheckLen, msg.Header.Description)
}

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

func checkMinLen(checkLen int, toCheck string) (string, bool) {
func checkMinLen(checkLen int, toCheck string) ([]string, bool) {
actualLen := len(toCheck)
if actualLen < checkLen {
errMsg := fmt.Sprintf("length is %d, should have atleast %d chars", actualLen, checkLen)
return errMsg, false
return []string{errMsg}, false
}
return "", true
return nil, true
}

0 comments on commit cbf329d

Please sign in to comment.