@@ -6,6 +6,18 @@ import (
6
6
"github.com/conventionalcommit/commitlint/lint"
7
7
)
8
8
9
+ // RegisterRule registers a custom rule
10
+ // if rule already exists, returns error
11
+ func RegisterRule (rule lint.Rule ) error {
12
+ return globalRegistry .RegisterRule (rule )
13
+ }
14
+
15
+ // RegisterFormatter registers a custom formatter
16
+ // if formatter already exists, returns error
17
+ func RegisterFormatter (format lint.Formatter ) error {
18
+ return globalRegistry .RegisterFormatter (format )
19
+ }
20
+
9
21
// GetLinter returns Linter for given confFilePath
10
22
func GetLinter (conf * lint.Config ) (* lint.Linter , error ) {
11
23
rules , err := GetEnabledRules (conf )
@@ -16,37 +28,35 @@ func GetLinter(conf *lint.Config) (*lint.Linter, error) {
16
28
}
17
29
18
30
// GetFormatter returns the formatter as defined in conf
19
- func GetFormatter (c * lint.Config ) (lint.Formatter , error ) {
20
- for _ , f := range defaultFormatters {
21
- if f .Name () == c .Formatter {
22
- return f , nil
23
- }
31
+ func GetFormatter (conf * lint.Config ) (lint.Formatter , error ) {
32
+ format , ok := globalRegistry .GetFormatter (conf .Formatter )
33
+ if ! ok {
34
+ return nil , fmt .Errorf ("'%s' formatter not found" , conf .Formatter )
24
35
}
25
- return nil , fmt . Errorf ( "%s formatter not found" , c . Formatter )
36
+ return format , nil
26
37
}
27
38
28
39
// GetEnabledRules forms Rule object for rules which are enabled in config
29
40
func GetEnabledRules (conf * lint.Config ) ([]lint.Rule , error ) {
30
- // rules lookup map
31
- rulesMap := map [string ]lint.Rule {}
32
- for _ , r := range defaultRules {
33
- rulesMap [r .Name ()] = r
34
- }
35
-
36
41
enabledRules := make ([]lint.Rule , 0 , len (conf .Rules ))
37
42
38
43
for ruleName , ruleConfig := range conf .Rules {
39
- r , ok := rulesMap [ruleName ]
44
+ // Checking if rule is registered
45
+ // before checking if rule is enabled
46
+ r , ok := globalRegistry .GetRule (ruleName )
40
47
if ! ok {
41
- return nil , fmt .Errorf ("unknown rule: %s " , ruleName )
48
+ return nil , fmt .Errorf ("'%s' rule not found " , ruleName )
42
49
}
43
- if ruleConfig .Enabled {
44
- err := r .Apply (ruleConfig .Argument , ruleConfig .Flags )
45
- if err != nil {
46
- return nil , err
47
- }
48
- enabledRules = append (enabledRules , r )
50
+
51
+ if ! ruleConfig .Enabled {
52
+ continue
53
+ }
54
+
55
+ err := r .Apply (ruleConfig .Argument , ruleConfig .Flags )
56
+ if err != nil {
57
+ return nil , err
49
58
}
59
+ enabledRules = append (enabledRules , r )
50
60
}
51
61
52
62
return enabledRules , nil
0 commit comments