/
structfield.go
74 lines (59 loc) · 1.71 KB
/
structfield.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
package structfield
import (
"go/ast"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const Doc = `Find struct literals using non-labeled fields.
The structfield analysis reports the usage of struct literal using non-labeled fields more than defined limit (default: 2).
The non-labeled field above the limit considered has higher cognitive load (harder to understand and remember).
`
var Analyzer = &analysis.Analyzer{
Name: "structfield",
Doc: Doc,
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: run,
}
var (
limit int // -limit flag
)
func init() {
Analyzer.Flags.IntVar(&limit, "limit", 2, "Limit of non-labeled fields allowed on struct literal")
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.CompositeLit)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
lit := n.(*ast.CompositeLit)
ok, count := CountNonLabeledFields(lit)
if !ok {
return
}
if count > limit {
pass.Reportf(lit.Pos(), "Found %d non-labeled fields on struct literal (> %d)", count, limit)
}
})
return nil, nil
}
// CountNonLabeledFields count usage of non-labeled fields in struct literal,
// returned as n.
// If the lit is not struct literal, then it will return ok as false.
func CountNonLabeledFields(lit *ast.CompositeLit) (ok bool, n int) {
ident, ok := lit.Type.(*ast.Ident)
if !ok {
return false, 0
}
if ident.Obj.Kind != ast.Typ {
return false, 0
}
var noLabelCount int
for _, e := range lit.Elts {
if _, ok := e.(*ast.KeyValueExpr); !ok {
noLabelCount++
}
}
return true, noLabelCount
}