-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathconfig.go
More file actions
317 lines (290 loc) · 6.51 KB
/
config.go
File metadata and controls
317 lines (290 loc) · 6.51 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package wsl
import (
"fmt"
"strings"
)
// CheckSet is a set of checks to run.
type CheckSet map[CheckType]struct{}
// CheckType is a type that represents a checker to run.
type CheckType int
// Each checker is represented by a CheckType that is used to enable or disable
// the check.
// A check can either be of a specific built-in keyword or custom checks.
const (
CheckInvalid CheckType = iota
CheckAssign
CheckBranch
CheckDecl
CheckDefer
CheckExpr
CheckFor
CheckGo
CheckIf
CheckIncDec
CheckLabel
CheckRange
CheckReturn
CheckSelect
CheckSend
CheckSwitch
CheckTypeSwitch
// CheckAfterBlock ensures there's a newline after each block.
CheckAfterBlock
// CheckAfterDecl ensures there's a newline after a declaration statement
// (`var`, `const`, `type`) unless the following statement is another
// declaration.
CheckAfterDecl
// CheckAfterDefer ensures there's a newline after a `defer` statement
// unless the following statement is another `defer`.
CheckAfterDefer
// CheckAfterExpr ensures there's a newline after an expression statement
// unless the following statement is another expression statement.
CheckAfterExpr
// CheckAfterGo ensures there's a newline after a `go` statement unless the
// following statement is another `go`.
CheckAfterGo
// CheckAppend only allows assignments of `append` to be cuddled with other
// assignments if it's a variable used in the append statement, e.g.
//
// a := 1
// x = append(x, a)
// .
CheckAppend
// CheckAssignExclusive only allows assignments of either new variables or
// re-assignment of existing ones, e.g.
//
// a := 1
// b := 2
//
// a = 1
// b = 2
// .
CheckAssignExclusive
// CheckAssignExpr will check so assignments are not cuddled with expression
// nodes, e.g.
//
// t1.Fn1()
//
// x := t1.Fn2()
// t1.Fn3()
// .
CheckAssignExpr
// CheckErr force error checking to follow immediately after an error
// variable is assigned, e.g.
//
// _, err := someFn()
// if err != nil {
// panic(err)
// }
// .
CheckErr
CheckLeadingWhitespace
CheckTrailingWhitespace
//nolint:godoclint // No need to document
// CheckTypes only used for reporting.
CheckCaseTrailingNewline
)
func (c CheckType) String() string {
return [...]string{
"invalid",
"assign",
"branch",
"decl",
"defer",
"expr",
"for",
"go",
"if",
"inc-dec",
"label",
"range",
"return",
"select",
"send",
"switch",
"type-switch",
//
"after-block",
"after-decl",
"after-defer",
"after-expr",
"after-go",
"append",
"assign-exclusive",
"assign-expr",
"err",
"leading-whitespace",
"trailing-whitespace",
//
"case-trailing-newline",
}[c]
}
type Configuration struct {
IncludeGenerated bool
AllowFirstInBlock bool
AllowWholeBlock bool
BranchMaxLines int
CaseMaxLines int
CuddleMaxStatements int
Checks CheckSet
}
func NewConfig() *Configuration {
return &Configuration{
IncludeGenerated: false,
AllowFirstInBlock: true,
AllowWholeBlock: false,
CaseMaxLines: 0,
BranchMaxLines: 2,
CuddleMaxStatements: 1,
Checks: DefaultChecks(),
}
}
func NewWithChecks(
defaultChecks string,
enable []string,
disable []string,
) (*Configuration, error) {
checks, err := NewCheckSet(defaultChecks, enable, disable)
if err != nil {
return nil, fmt.Errorf("failed to create config: %w", err)
}
cfg := NewConfig()
cfg.Checks = checks
return cfg, nil
}
func NewCheckSet(
defaultChecks string,
enable []string,
disable []string,
) (CheckSet, error) {
var cs CheckSet
switch strings.ToLower(defaultChecks) {
case "", "default":
cs = DefaultChecks()
case "all":
cs = AllChecks()
case "none":
cs = NoChecks()
default:
return nil, fmt.Errorf("invalid preset '%s', must be `all`, `none` or `` (empty)", defaultChecks)
}
for _, s := range enable {
check, err := CheckFromString(s)
if err != nil {
return nil, fmt.Errorf("invalid check '%s'", s)
}
cs.Add(check)
}
for _, s := range disable {
check, err := CheckFromString(s)
if err != nil {
return nil, fmt.Errorf("invalid check '%s'", s)
}
cs.Remove(check)
}
return cs, nil
}
func DefaultChecks() CheckSet {
return CheckSet{
CheckAppend: {},
CheckAssign: {},
CheckBranch: {},
CheckDecl: {},
CheckDefer: {},
CheckErr: {},
CheckExpr: {},
CheckFor: {},
CheckGo: {},
CheckIf: {},
CheckIncDec: {},
CheckLabel: {},
CheckLeadingWhitespace: {},
CheckTrailingWhitespace: {},
CheckRange: {},
CheckReturn: {},
CheckSelect: {},
CheckSend: {},
CheckSwitch: {},
CheckTypeSwitch: {},
}
}
func AllChecks() CheckSet {
c := DefaultChecks()
c.Add(CheckAssignExclusive)
c.Add(CheckAssignExpr)
c.Add(CheckAfterBlock)
c.Add(CheckAfterDecl)
c.Add(CheckAfterDefer)
c.Add(CheckAfterExpr)
c.Add(CheckAfterGo)
return c
}
func NoChecks() CheckSet {
return CheckSet{}
}
func (c CheckSet) Add(check CheckType) {
c[check] = struct{}{}
}
func (c CheckSet) Remove(check CheckType) {
delete(c, check)
}
func CheckFromString(s string) (CheckType, error) {
switch strings.ToLower(s) {
case "assign":
return CheckAssign, nil
case "branch":
return CheckBranch, nil
case "decl":
return CheckDecl, nil
case "defer":
return CheckDefer, nil
case "expr":
return CheckExpr, nil
case "for":
return CheckFor, nil
case "go":
return CheckGo, nil
case "if":
return CheckIf, nil
case "inc-dec":
return CheckIncDec, nil
case "label":
return CheckLabel, nil
case "range":
return CheckRange, nil
case "return":
return CheckReturn, nil
case "select":
return CheckSelect, nil
case "send":
return CheckSend, nil
case "switch":
return CheckSwitch, nil
case "type-switch":
return CheckTypeSwitch, nil
case "after-block":
return CheckAfterBlock, nil
case "after-decl":
return CheckAfterDecl, nil
case "after-defer":
return CheckAfterDefer, nil
case "after-expr":
return CheckAfterExpr, nil
case "after-go":
return CheckAfterGo, nil
case "append":
return CheckAppend, nil
case "assign-exclusive":
return CheckAssignExclusive, nil
case "assign-expr":
return CheckAssignExpr, nil
case "err":
return CheckErr, nil
case "leading-whitespace":
return CheckLeadingWhitespace, nil
case "trailing-whitespace":
return CheckTrailingWhitespace, nil
default:
return CheckInvalid, fmt.Errorf("invalid check '%s'", s)
}
}