-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlint.go
More file actions
72 lines (58 loc) · 1.7 KB
/
lint.go
File metadata and controls
72 lines (58 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package config
import (
"fmt"
"github.com/conventionalcommit/commitlint/internal/registry"
"github.com/conventionalcommit/commitlint/lint"
)
// NewLinter returns Linter for given confFilePath
func NewLinter(conf *lint.Config) (*lint.Linter, error) {
err := checkIfMinVersion(conf.MinVersion)
if err != nil {
return nil, err
}
rules, err := GetEnabledRules(conf)
if err != nil {
return nil, err
}
return lint.New(conf, rules)
}
// GetFormatter returns the formatter as defined in conf
func GetFormatter(conf *lint.Config) (lint.Formatter, error) {
err := checkIfMinVersion(conf.MinVersion)
if err != nil {
return nil, err
}
format, ok := registry.GetFormatter(conf.Formatter)
if !ok {
return nil, fmt.Errorf("config error: '%s' formatter not found", conf.Formatter)
}
return format, nil
}
// GetEnabledRules forms Rule object for rules which are enabled in config
func GetEnabledRules(conf *lint.Config) ([]lint.Rule, error) {
enabledRules := make([]lint.Rule, 0, len(conf.Rules))
// To check if duplicate rule is added
addedRules := make(map[string]struct{})
for _, ruleName := range conf.Rules {
if _, ok := addedRules[ruleName]; ok {
continue
}
// Checking if rule is registered
// before checking if rule is enabled
r, ok := registry.GetRule(ruleName)
if !ok {
return nil, fmt.Errorf("config error: '%s' rule not found", ruleName)
}
rConf, ok := conf.Settings[ruleName]
if !ok {
return nil, fmt.Errorf("config error: '%s' rule settings not found", ruleName)
}
err := r.Apply(rConf)
if err != nil {
return nil, fmt.Errorf("config error: %v", err)
}
enabledRules = append(enabledRules, r)
addedRules[r.Name()] = struct{}{}
}
return enabledRules, nil
}