Skip to content

Commit

Permalink
refactor(lint): rename Failure, RuleFailure to Result, Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
muthukrishnan24 committed Feb 20, 2022
1 parent a3a9938 commit d7c2fb1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 38 deletions.
10 changes: 5 additions & 5 deletions formatter/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ type DefaultFormatter struct{}
func (f *DefaultFormatter) Name() string { return "default" }

// Format formats the lint.Failure
func (f *DefaultFormatter) Format(res *lint.Failure) (string, error) {
func (f *DefaultFormatter) Format(res *lint.Result) (string, error) {
return formatFailure(res), nil
}

func formatFailure(res *lint.Failure) string {
func formatFailure(res *lint.Result) string {
if res.IsOK() {
return " ✔ commit message"
}
return writeFailure(res)
}

func writeFailure(res *lint.Failure) string {
func writeFailure(res *lint.Result) string {
str := &strings.Builder{}

quotedStr := strconv.Quote(truncate(25, res.Input()))
Expand All @@ -45,7 +45,7 @@ func writeFailure(res *lint.Failure) string {
return strings.Trim(str.String(), "\n")
}

func writeRuleFailure(w *strings.Builder, sign, title string, resArr []*lint.RuleFailure) {
func writeRuleFailure(w *strings.Builder, sign, title string, resArr []*lint.Issue) {
if len(resArr) == 0 {
return
}
Expand All @@ -56,7 +56,7 @@ func writeRuleFailure(w *strings.Builder, sign, title string, resArr []*lint.Rul
}
}

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

if len(msgs) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions formatter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type JSONFormatter struct{}
func (f *JSONFormatter) Name() string { return "json" }

// Format formats the lint.Result
func (f *JSONFormatter) Format(res *lint.Failure) (string, error) {
func (f *JSONFormatter) Format(res *lint.Result) (string, error) {
output := make(map[string]interface{}, 4)

errs, warns, others := bySeverity(res)
Expand All @@ -33,7 +33,7 @@ func (f *JSONFormatter) Format(res *lint.Failure) (string, error) {
return strings.Trim(string(msg), "\n"), nil
}

func (f *JSONFormatter) formRuleFailure(res []*lint.RuleFailure, includeSev bool) []map[string]interface{} {
func (f *JSONFormatter) formRuleFailure(res []*lint.Issue, includeSev bool) []map[string]interface{} {
outs := make([]map[string]interface{}, 0, len(res))

for _, r := range res {
Expand Down
4 changes: 2 additions & 2 deletions formatter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package formatter
import "github.com/conventionalcommit/commitlint/lint"

// bySeverity returns all messages with given severity
func bySeverity(res *lint.Failure) (errs, warns, others []*lint.RuleFailure) {
for _, r := range res.Failures() {
func bySeverity(res *lint.Result) (errs, warns, others []*lint.Issue) {
for _, r := range res.Issues() {
switch r.Severity() {
case lint.SeverityError:
errs = append(errs, r)
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func readStdInPipe() (string, error) {
return strings.TrimSpace(s), nil
}

func hasErrorSeverity(res *lint.Failure) bool {
for _, r := range res.Failures() {
func hasErrorSeverity(res *lint.Result) bool {
for _, r := range res.Issues() {
if r.Severity() == lint.SeverityError {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Formatter interface {
Name() string

// Format formats the linter result
Format(result *Failure) (string, error)
Format(result *Result) (string, error)
}

// Rule represent a linter rule
Expand Down
16 changes: 8 additions & 8 deletions lint/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func New(conf *Config, rules []Rule) (*Linter, error) {
}

// Lint checks the given commitMsg string against rules
func (l *Linter) Lint(commitMsg string) (*Failure, error) {
func (l *Linter) Lint(commitMsg string) (*Result, error) {
msg, err := l.parser.Parse(commitMsg)
if err != nil {
return l.parserErrorRule(commitMsg, err)
Expand All @@ -29,8 +29,8 @@ func (l *Linter) Lint(commitMsg string) (*Failure, error) {
}

// LintCommit checks the given Commit against rules
func (l *Linter) LintCommit(msg Commit) (*Failure, error) {
res := newFailure(msg.Message())
func (l *Linter) LintCommit(msg Commit) (*Result, error) {
res := newResult(msg.Message())

for _, rule := range l.rules {
currentRule := rule
Expand All @@ -44,21 +44,21 @@ func (l *Linter) LintCommit(msg Commit) (*Failure, error) {
return res, nil
}

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

func (l *Linter) parserErrorRule(commitMsg string, err error) (*Failure, error) {
res := newFailure(commitMsg)
func (l *Linter) parserErrorRule(commitMsg string, err error) (*Result, error) {
res := newResult(commitMsg)

errMsg := err.Error()

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

return res, nil
Expand Down
36 changes: 18 additions & 18 deletions lint/result.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
package lint

// Failure holds Failure of linter
type Failure struct {
// Result holds Result of linter
type Result struct {
inputMsg string

failures []*RuleFailure
issues []*Issue
}

func newFailure(inputMsg string) *Failure {
return &Failure{
func newResult(inputMsg string) *Result {
return &Result{
inputMsg: inputMsg,
}
}

// AddError adds
func (res *Failure) add(r *RuleFailure) {
res.failures = append(res.failures, r)
func (res *Result) add(r *Issue) {
res.issues = append(res.issues, r)
}

// IsOK returns true if commit message passed all the rules
func (res *Failure) IsOK() bool { return len(res.failures) == 0 }
func (res *Result) IsOK() bool { return len(res.issues) == 0 }

// Input returns input commit message
func (res *Failure) Input() string { return res.inputMsg }
func (res *Result) Input() string { return res.inputMsg }

// Failures returns rule Failures
func (res *Failure) Failures() []*RuleFailure { return res.failures }
// Issues returns rule Issues
func (res *Result) Issues() []*Issue { return res.issues }

// RuleFailure holds Failure of a linter rule
type RuleFailure struct {
// Issue holds Failure of a linter rule
type Issue struct {
name string
severity Severity
messages []string
}

func newRuleFailure(name string, msgs []string, severity Severity) *RuleFailure {
return &RuleFailure{
func newIssue(name string, msgs []string, severity Severity) *Issue {
return &Issue{
name: name,
messages: msgs,
severity: severity,
}
}

// Name returns rule name
func (r *RuleFailure) Name() string { return r.name }
func (r *Issue) Name() string { return r.name }

// Severity returns severity of the Rule Failure
func (r *RuleFailure) Severity() Severity { return r.severity }
func (r *Issue) Severity() Severity { return r.severity }

// Message returns the error messages of failed rule
func (r *RuleFailure) Message() []string { return r.messages }
func (r *Issue) Message() []string { return r.messages }

0 comments on commit d7c2fb1

Please sign in to comment.