forked from golang/lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
148 lines (128 loc) · 3.58 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package hint
import (
"encoding/json"
"fmt"
"io/ioutil"
)
var defaultCommonInitialisms = map[string]bool{
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SSH": true,
"TLS": true,
"TTL": true,
"UI": true,
"UID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
}
var defaultBadReceiverNames = map[string]bool{
"me": true,
"this": true,
"self": true,
}
// Config defines configuration options for linter
type Config struct {
Package bool `json:"package"`
Imports bool `json:"imports"`
Names bool `json:"names"`
Exported bool `json:"exported"`
VarDecls bool `json:"var-decls"`
Elses bool `json:"elses"`
MakeSlice bool `json:"make-slice"`
ErrorReturn bool `json:"error-return"`
IgnoredReturn bool `json:"ignored-return"`
PackageUnderscore bool `json:"package-underscore"`
NamedReturn bool `json:"named-return"`
PackagePrefixNames bool `json:"package-prefix-names"`
MinConfidence float64 `json:"min-confidence"`
IgnoreFiles []string `json:"ignore-files"`
ignoreFilesMap map[string]bool
IgnorePackages []string `json:"ignore-packages"`
ignorePackagesMap map[string]bool
IgnoreTypes []string `json:"ignore-types"`
ignoreTypesMap map[string]bool
Initialisms map[string]bool `json:"initialisms"`
BadReceiverNames map[string]bool `json:"bad-receivers"`
}
// NewDefaultConfig creates linter config with predefined options
func NewDefaultConfig() *Config {
return &Config{
Package: true,
Imports: true,
Names: true,
Exported: true,
VarDecls: true,
Elses: true,
MakeSlice: true,
ErrorReturn: true,
IgnoredReturn: true,
PackageUnderscore: true,
NamedReturn: false,
PackagePrefixNames: false,
MinConfidence: 0.8,
Initialisms: defaultCommonInitialisms,
BadReceiverNames: defaultBadReceiverNames,
// IgnoreFiles: []string{}, // TODO: for future use
// IgnorePackages: []string{}, // TODO: for future use
// IgnoreTypes: []string{}, // TODO: for future use
}
}
// NewConfig reads config from given file. If filename is empty, default config will be returned
func NewConfig(file string) (*Config, error) {
c := NewDefaultConfig()
if file != "" {
contents, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("could not read config file %s: %s", file, err.Error())
}
if err := json.Unmarshal(contents, c); err != nil {
return nil, fmt.Errorf("could not parse configuration from %s: %s", file, err.Error())
}
sliceToMapBool := func(slice []string) map[string]bool {
res := map[string]bool{}
for _, v := range slice {
res[v] = true
}
return res
}
c.ignoreFilesMap = sliceToMapBool(c.IgnoreFiles)
c.ignorePackagesMap = sliceToMapBool(c.IgnorePackages)
c.ignoreTypesMap = sliceToMapBool(c.IgnoreTypes)
}
return c, nil
}
// TODO: for future use
//func (c *Config) IsPackageIgnored(packageName string) (ok bool) {
// _, ok = c.ignorePackagesMap[packageName]
// return
//}
//
//func (c *Config) IsFileIgnored(fileName string) (ok bool) {
// _, ok = c.ignoreFilesMap[fileName]
// return
//}
//
//func (c *Config) IsTypeIgnored(typeName string) (ok bool) {
// _, ok = c.ignoreTypesMap[typeName]
// return
//}