Skip to content

Commit d5963f6

Browse files
feat: add registry for custom rule and formatters
1 parent 63f2581 commit d5963f6

File tree

2 files changed

+120
-20
lines changed

2 files changed

+120
-20
lines changed

config/lint.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import (
66
"github.com/conventionalcommit/commitlint/lint"
77
)
88

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+
921
// GetLinter returns Linter for given confFilePath
1022
func GetLinter(conf *lint.Config) (*lint.Linter, error) {
1123
rules, err := GetEnabledRules(conf)
@@ -16,37 +28,35 @@ func GetLinter(conf *lint.Config) (*lint.Linter, error) {
1628
}
1729

1830
// 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)
2435
}
25-
return nil, fmt.Errorf("%s formatter not found", c.Formatter)
36+
return format, nil
2637
}
2738

2839
// GetEnabledRules forms Rule object for rules which are enabled in config
2940
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-
3641
enabledRules := make([]lint.Rule, 0, len(conf.Rules))
3742

3843
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)
4047
if !ok {
41-
return nil, fmt.Errorf("unknown rule: %s", ruleName)
48+
return nil, fmt.Errorf("'%s' rule not found", ruleName)
4249
}
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
4958
}
59+
enabledRules = append(enabledRules, r)
5060
}
5161

5262
return enabledRules, nil

config/registry.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
7+
"github.com/conventionalcommit/commitlint/lint"
8+
)
9+
10+
var globalRegistry = newRegistry()
11+
12+
type registry struct {
13+
mut *sync.Mutex
14+
15+
allRules map[string]lint.Rule
16+
allFormatters map[string]lint.Formatter
17+
}
18+
19+
func newRegistry() *registry {
20+
reg := &registry{
21+
mut: &sync.Mutex{},
22+
23+
allRules: make(map[string]lint.Rule),
24+
allFormatters: make(map[string]lint.Formatter),
25+
}
26+
27+
// Register Default Rules
28+
for _, rule := range defaultRules {
29+
err := reg.RegisterRule(rule)
30+
if err != nil {
31+
// default rules should not throw error
32+
panic(err)
33+
}
34+
}
35+
36+
// Register Default Formatters
37+
for _, format := range defaultFormatters {
38+
err := reg.RegisterFormatter(format)
39+
if err != nil {
40+
// default formatters should not throw error
41+
panic(err)
42+
}
43+
}
44+
45+
return reg
46+
}
47+
48+
func (r *registry) RegisterRule(rule lint.Rule) error {
49+
r.mut.Lock()
50+
defer r.mut.Unlock()
51+
52+
_, ok := r.allRules[rule.Name()]
53+
if ok {
54+
return fmt.Errorf("'%s' rule already registered", rule.Name())
55+
}
56+
57+
r.allRules[rule.Name()] = rule
58+
59+
return nil
60+
}
61+
62+
func (r *registry) RegisterFormatter(format lint.Formatter) error {
63+
r.mut.Lock()
64+
defer r.mut.Unlock()
65+
66+
_, ok := r.allFormatters[format.Name()]
67+
if ok {
68+
return fmt.Errorf("'%s' formatter already registered", format.Name())
69+
}
70+
71+
r.allFormatters[format.Name()] = format
72+
73+
return nil
74+
}
75+
76+
func (r *registry) GetRule(name string) (lint.Rule, bool) {
77+
r.mut.Lock()
78+
defer r.mut.Unlock()
79+
80+
rule, ok := r.allRules[name]
81+
return rule, ok
82+
}
83+
84+
func (r *registry) GetFormatter(name string) (lint.Formatter, bool) {
85+
r.mut.Lock()
86+
defer r.mut.Unlock()
87+
88+
format, ok := r.allFormatters[name]
89+
return format, ok
90+
}

0 commit comments

Comments
 (0)