-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconfig.go
118 lines (100 loc) · 2.27 KB
/
config.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package config
import (
"regexp"
"strings"
)
type Config struct {
Checks map[string]bool
IgnoredNumbers map[string]struct{}
IgnoredFunctions []*regexp.Regexp
IgnoredFiles []*regexp.Regexp
}
type Option func(config *Config)
func DefaultConfig() *Config {
return &Config{
Checks: map[string]bool{},
IgnoredNumbers: map[string]struct{}{
"0": {},
"0.0": {},
"1": {},
"1.0": {},
},
IgnoredFiles: []*regexp.Regexp{
regexp.MustCompile(`_test.go`),
},
IgnoredFunctions: []*regexp.Regexp{
regexp.MustCompile(`time.Date`),
},
}
}
func WithOptions(options ...Option) *Config {
c := DefaultConfig()
for _, option := range options {
option(c)
}
return c
}
func WithIgnoredFunctions(excludes string) Option {
return func(config *Config) {
for _, exclude := range strings.Split(excludes, ",") {
if exclude == "" {
continue
}
config.IgnoredFunctions = append(config.IgnoredFunctions, regexp.MustCompile(exclude))
}
}
}
func WithIgnoredFiles(excludes string) Option {
return func(config *Config) {
for _, exclude := range strings.Split(excludes, ",") {
if exclude == "" {
continue
}
config.IgnoredFiles = append(config.IgnoredFiles, regexp.MustCompile(exclude))
}
}
}
func WithIgnoredNumbers(numbers string) Option {
return func(config *Config) {
for _, number := range strings.Split(numbers, ",") {
if number == "" {
continue
}
config.IgnoredNumbers[config.removeDigitSeparator(number)] = struct{}{}
}
}
}
func WithCustomChecks(checks string) Option {
return func(config *Config) {
if checks == "" {
return
}
for name, _ := range config.Checks {
config.Checks[name] = false
}
for _, name := range strings.Split(checks, ",") {
if name == "" {
continue
}
config.Checks[name] = true
}
}
}
func (c *Config) IsCheckEnabled(name string) bool {
return c.Checks[name]
}
func (c *Config) IsIgnoredNumber(number string) bool {
_, ok := c.IgnoredNumbers[c.removeDigitSeparator(number)]
return ok
}
func (c *Config) IsIgnoredFunction(f string) bool {
for _, pattern := range c.IgnoredFunctions {
if pattern.MatchString(f) {
return true
}
}
return false
}
func (c *Config) removeDigitSeparator(number string) string {
return strings.Replace(number, "_", "", -1)
}