Skip to content

Commit 6bd1565

Browse files
feat(lint): add parser interface
1 parent fb85e69 commit 6bd1565

File tree

4 files changed

+28
-30
lines changed

4 files changed

+28
-30
lines changed

lint/linter.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ package lint
55
type Linter struct {
66
conf *Config
77
rules []Rule
8+
9+
parser Parser
810
}
911

1012
// New returns a new Linter instance with given config and rules
1113
func New(conf *Config, rules []Rule) (*Linter, error) {
12-
return &Linter{conf: conf, rules: rules}, nil
14+
l := &Linter{
15+
conf: conf,
16+
rules: rules,
17+
parser: &defaultParser{},
18+
}
19+
return l, nil
1320
}
1421

1522
// Lint checks the given commitMsg string against rules
1623
func (l *Linter) Lint(commitMsg string) (*Failure, error) {
17-
msg, err := Parse(commitMsg)
24+
msg, err := l.parser.Parse(commitMsg)
1825
if err != nil {
1926
return l.parserErrorRule(commitMsg, err)
2027
}
@@ -49,13 +56,7 @@ func (l *Linter) runRule(rule Rule, severity Severity, msg *Commit) (*RuleFailur
4956
func (l *Linter) parserErrorRule(commitMsg string, err error) (*Failure, error) {
5057
res := newFailure(commitMsg)
5158

52-
var errMsg string
53-
if isHeaderErr(err) {
54-
// TODO: show more information
55-
errMsg = "commit header is not in valid format"
56-
} else {
57-
errMsg = err.Error()
58-
}
59+
errMsg := err.Error()
5960

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

lint/message.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

lint/parse.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

lint/parser.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package lint
2+
3+
import "github.com/conventionalcommit/parser"
4+
5+
// Commit represent a commit message
6+
// for now it is an alias of parser.Commit
7+
type Commit = parser.Commit
8+
9+
// Parser parses given commit message
10+
type Parser interface {
11+
Parse(msg string) (*Commit, error)
12+
}
13+
14+
type defaultParser struct{}
15+
16+
func (d *defaultParser) Parse(msg string) (*Commit, error) {
17+
return parser.Parse(msg)
18+
}

0 commit comments

Comments
 (0)