This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.go
200 lines (161 loc) · 4.42 KB
/
analyzer.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
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
package decorder
import (
"go/ast"
"go/token"
"strings"
"golang.org/x/tools/go/analysis"
)
type (
decNumChecker struct {
tokenMap map[string]token.Token
tokenCounts map[token.Token]int
decOrder []string
funcPoss []funcPos
}
funcPos struct {
start token.Pos
end token.Pos
}
)
const (
Name = "decorder"
FlagDo = "dec-order"
FlagDdnc = "disable-dec-num-check"
FlagDdoc = "disable-dec-order-check"
FlagDiffc = "disable-init-func-first-check"
)
var (
Analyzer = &analysis.Analyzer{
Name: Name,
Doc: "check declaration order and count of types, constants, variables and functions",
Run: run,
}
decOrder string
disableDecNumCheck bool
disableDecOrderCheck bool
disableInitFuncFirstCheck bool
tokens = []token.Token{token.TYPE, token.CONST, token.VAR, token.FUNC}
)
//nolint:lll
func init() {
Analyzer.Flags.StringVar(&decOrder, FlagDo, "type,const,var,func", "define the required order of types, constants, variables and functions declarations inside a file")
Analyzer.Flags.BoolVar(&disableDecNumCheck, FlagDdnc, false, "option to disable check for number of e.g. var declarations inside file")
Analyzer.Flags.BoolVar(&disableDecOrderCheck, FlagDdoc, false, "option to disable check for order of declarations inside file")
Analyzer.Flags.BoolVar(&disableInitFuncFirstCheck, FlagDiffc, false, "option to disable check that init function is always first function in file")
}
func run(pass *analysis.Pass) (interface{}, error) {
for _, f := range pass.Files {
ast.Inspect(f, runDeclNumAndDecOrderCheck(pass))
if !disableInitFuncFirstCheck {
ast.Inspect(f, runInitFuncFirstCheck(pass))
}
}
return nil, nil
}
func runInitFuncFirstCheck(pass *analysis.Pass) func(ast.Node) bool {
nonInitFound := false
return func(n ast.Node) bool {
dec, ok := n.(*ast.FuncDecl)
if !ok {
return true
}
if dec.Name.Name == "init" && dec.Recv == nil {
if nonInitFound {
pass.Reportf(dec.Pos(), "init func must be the first function in file")
}
} else {
nonInitFound = true
}
return true
}
}
func runDeclNumAndDecOrderCheck(pass *analysis.Pass) func(ast.Node) bool {
dnc := newDecNumChecker()
if disableDecNumCheck && disableDecOrderCheck {
return func(n ast.Node) bool {
return true
}
}
return func(n ast.Node) bool {
fd, ok := n.(*ast.FuncDecl)
if ok {
return dnc.handleFuncDec(fd, pass)
}
gd, ok := n.(*ast.GenDecl)
if !ok {
return true
}
if dnc.isInsideFunction(gd) {
return true
}
dnc.handleGenDecl(gd, pass)
if !disableDecOrderCheck {
dnc.handleDecOrderCheck(gd, pass)
}
return true
}
}
func newDecNumChecker() decNumChecker {
dnc := decNumChecker{
tokenMap: map[string]token.Token{},
tokenCounts: map[token.Token]int{},
decOrder: []string{},
funcPoss: []funcPos{},
}
for _, t := range tokens {
dnc.tokenCounts[t] = 0
dnc.tokenMap[t.String()] = t
}
for _, do := range strings.Split(decOrder, ",") {
dnc.decOrder = append(dnc.decOrder, strings.TrimSpace(do))
}
return dnc
}
func (dnc decNumChecker) isToLate(t token.Token) (string, bool) {
for i, do := range dnc.decOrder {
if do == t.String() {
for j := i + 1; j < len(dnc.decOrder); j++ {
if dnc.tokenCounts[dnc.tokenMap[dnc.decOrder[j]]] > 0 {
return dnc.decOrder[j], false
}
}
return "", true
}
}
return "", true
}
func (dnc *decNumChecker) handleGenDecl(gd *ast.GenDecl, pass *analysis.Pass) {
for _, t := range tokens {
if gd.Tok == t {
dnc.tokenCounts[t]++
if !disableDecNumCheck && dnc.tokenCounts[t] > 1 {
pass.Reportf(gd.Pos(), "multiple \"%s\" declarations are not allowed; use parentheses instead", t.String())
}
}
}
}
func (dnc decNumChecker) handleDecOrderCheck(gd *ast.GenDecl, pass *analysis.Pass) {
l, c := dnc.isToLate(gd.Tok)
if !c {
pass.Reportf(gd.Pos(), "%s must not be placed after %s", gd.Tok.String(), l)
}
}
func (dnc decNumChecker) isInsideFunction(dn *ast.GenDecl) bool {
for _, poss := range dnc.funcPoss {
if poss.start < dn.Pos() && poss.end > dn.Pos() {
return true
}
}
return false
}
func (dnc *decNumChecker) handleFuncDec(fd *ast.FuncDecl, pass *analysis.Pass) bool {
dnc.funcPoss = append(dnc.funcPoss, funcPos{start: fd.Pos(), end: fd.End()})
dnc.tokenCounts[token.FUNC]++
if !disableDecOrderCheck {
l, c := dnc.isToLate(token.FUNC)
if !c {
pass.Reportf(fd.Pos(), "%s must not be placed after %s", token.FUNC.String(), l)
}
}
return true
}