Skip to content

Commit 9173967

Browse files
refactor: linter errors, common handling for rules
1 parent 37fe7a7 commit 9173967

24 files changed

Lines changed: 276 additions & 470 deletions

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func Parse(confPath string) (*lint.Config, error) {
5252
// Validate validates given config instance, it checks the following
5353
// If formatters, rules are registered/known
5454
// If arguments to rules are valid
55-
// If version is valid and atleast minimum than commitlint version used
55+
// If version is valid and at least minimum than commitlint version used
5656
func Validate(conf *lint.Config) []error {
5757
var errs []error
5858

@@ -78,7 +78,7 @@ func Validate(conf *lint.Config) []error {
7878
for ruleName, sev := range conf.Severity.Rules {
7979
// Check Severity Level of rule config
8080
if !isSeverityValid(sev) {
81-
errs = append(errs, fmt.Errorf("unknown default severity level '%s' for rule '%s'", ruleName, sev))
81+
errs = append(errs, fmt.Errorf("unknown severity level '%s' for rule '%s'", sev, ruleName))
8282
}
8383
}
8484

internal/cmd/hook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ func getRepoRootDir() (string, error) {
109109
}
110110

111111
func isHookExists(err error) bool {
112-
return err == errHooksExist
112+
return errors.Is(err, errHooksExist)
113113
}
114114

115115
func isConfExists(err error) bool {
116-
return err == errConfigExist
116+
return errors.Is(err, errConfigExist)
117117
}

internal/cmd/lint.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ func runLint(confFilePath, fileInput string) (lintResult string, hasError bool,
4747
return "", false, err
4848
}
4949

50-
cleanMsg, err := cleanupMsg(commitMsg)
51-
if err != nil {
52-
return "", false, err
53-
}
50+
cleanMsg := cleanupMsg(commitMsg)
5451

5552
result, err := linter.ParseAndLint(cleanMsg)
5653
if err != nil {
@@ -125,7 +122,7 @@ func trimRightSpace(s string) string {
125122
return strings.TrimRightFunc(s, unicode.IsSpace)
126123
}
127124

128-
func cleanupMsg(dirtyMsg string) (string, error) {
125+
func cleanupMsg(dirtyMsg string) string {
129126
// commit msg cleanup in git is configurable: https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---cleanupltmodegt
130127
// For now we do a combination of the "scissors" behavior and the "strip" behavior
131128
// * remove the scissors line and everything below
@@ -168,7 +165,7 @@ func cleanupMsg(dirtyMsg string) (string, error) {
168165
// strip trailing empty line
169166
cleanMsg = strings.TrimSuffix(cleanMsg, "\n")
170167
}
171-
return cleanMsg, nil
168+
return cleanMsg
172169
}
173170

174171
func readStdInPipe() (string, error) {

internal/cmd/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func (m *multiError) Error() string {
1414

1515
func (m *multiError) Errors() []error {
1616
errs := make([]error, len(*m))
17-
for _, err := range *m {
18-
errs = append(errs, err)
17+
for i, err := range *m {
18+
errs[i] = err
1919
}
2020
return errs
2121
}

rule/body_max_length.go

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

rule/body_max_line_length.go

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

rule/body_min_length.go

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

rule/charset_rules.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package rule
2+
3+
import "github.com/conventionalcommit/commitlint/lint"
4+
5+
// Compile-time interface checks
6+
var (
7+
_ lint.Rule = (*TypeCharsetRule)(nil)
8+
_ lint.Rule = (*ScopeCharsetRule)(nil)
9+
)
10+
11+
// applyStringArg is a shared helper that extracts a string argument from a RuleSetting.
12+
func applyStringArg(dst *string, ruleName string, setting lint.RuleSetting) error {
13+
if err := setStringArg(dst, setting.Argument); err != nil {
14+
return errInvalidArg(ruleName, err)
15+
}
16+
return nil
17+
}
18+
19+
// TypeCharsetRule to validate charset of type
20+
type TypeCharsetRule struct{ Charset string }
21+
22+
func (r *TypeCharsetRule) Name() string { return "type-charset" }
23+
func (r *TypeCharsetRule) Apply(s lint.RuleSetting) error {
24+
return applyStringArg(&r.Charset, r.Name(), s)
25+
}
26+
27+
func (r *TypeCharsetRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
28+
invalidChars, isValid := validateCharset(r.Charset, msg.Type())
29+
if isValid {
30+
return nil, true
31+
}
32+
return lint.NewIssue(
33+
"type can only have chars ["+r.Charset+"]",
34+
"invalid characters ["+invalidChars+"]",
35+
), false
36+
}
37+
38+
// ScopeCharsetRule to validate charset of scope
39+
type ScopeCharsetRule struct{ Charset string }
40+
41+
func (r *ScopeCharsetRule) Name() string { return "scope-charset" }
42+
func (r *ScopeCharsetRule) Apply(s lint.RuleSetting) error {
43+
return applyStringArg(&r.Charset, r.Name(), s)
44+
}
45+
46+
func (r *ScopeCharsetRule) Validate(msg lint.Commit) (*lint.Issue, bool) {
47+
invalidChars, isValid := validateCharset(r.Charset, msg.Scope())
48+
if isValid {
49+
return nil, true
50+
}
51+
return lint.NewIssue(
52+
"scope can only have these chars ["+r.Charset+"]",
53+
"invalid characters ["+invalidChars+"]",
54+
), false
55+
}

rule/desc_max_length.go

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

rule/desc_min_length.go

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

0 commit comments

Comments
 (0)