Skip to content

Commit

Permalink
feat(lint): add parser interface
Browse files Browse the repository at this point in the history
  • Loading branch information
muthukrishnan24 committed Dec 12, 2021
1 parent fb85e69 commit 6bd1565
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
19 changes: 10 additions & 9 deletions lint/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ package lint
type Linter struct {
conf *Config
rules []Rule

parser Parser
}

// New returns a new Linter instance with given config and rules
func New(conf *Config, rules []Rule) (*Linter, error) {
return &Linter{conf: conf, rules: rules}, nil
l := &Linter{
conf: conf,
rules: rules,
parser: &defaultParser{},
}
return l, nil
}

// Lint checks the given commitMsg string against rules
func (l *Linter) Lint(commitMsg string) (*Failure, error) {
msg, err := Parse(commitMsg)
msg, err := l.parser.Parse(commitMsg)
if err != nil {
return l.parserErrorRule(commitMsg, err)
}
Expand Down Expand Up @@ -49,13 +56,7 @@ func (l *Linter) runRule(rule Rule, severity Severity, msg *Commit) (*RuleFailur
func (l *Linter) parserErrorRule(commitMsg string, err error) (*Failure, error) {
res := newFailure(commitMsg)

var errMsg string
if isHeaderErr(err) {
// TODO: show more information
errMsg = "commit header is not in valid format"
} else {
errMsg = err.Error()
}
errMsg := err.Error()

ruleFail := newRuleFailure("parser", []string{errMsg}, SeverityError)
res.add(ruleFail)
Expand Down
7 changes: 0 additions & 7 deletions lint/message.go

This file was deleted.

14 changes: 0 additions & 14 deletions lint/parse.go

This file was deleted.

18 changes: 18 additions & 0 deletions lint/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package lint

import "github.com/conventionalcommit/parser"

// Commit represent a commit message
// for now it is an alias of parser.Commit
type Commit = parser.Commit

// Parser parses given commit message
type Parser interface {
Parse(msg string) (*Commit, error)
}

type defaultParser struct{}

func (d *defaultParser) Parse(msg string) (*Commit, error) {
return parser.Parse(msg)
}

0 comments on commit 6bd1565

Please sign in to comment.