-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
227 lines (177 loc) · 4.46 KB
/
factory.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
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
package gofactory
import (
"go/ast"
"go/types"
"log"
"github.com/gobwas/glob"
"golang.org/x/tools/go/analysis"
)
type config struct {
pkgGlobs globsFlag
onlyPkgGlobs bool
}
const (
name = "gofactory"
doc = "Blocks the creation of structures directly, without a factory."
packageGlobsDesc = "list of glob packages, which can create structures without factories inside the glob package"
onlyPkgGlobsDesc = "use a factory to initiate a structure for glob packages only"
)
func NewAnalyzer() *analysis.Analyzer {
analyzer := &analysis.Analyzer{
Name: name,
Doc: doc,
}
cfg := config{}
analyzer.Flags.Var(&cfg.pkgGlobs, "packageGlobs", packageGlobsDesc)
analyzer.Flags.BoolVar(&cfg.onlyPkgGlobs, "packageGlobsOnly", false, onlyPkgGlobsDesc)
analyzer.Run = run(&cfg)
return analyzer
}
func run(cfg *config) func(pass *analysis.Pass) (interface{}, error) {
return func(pass *analysis.Pass) (interface{}, error) {
var blockedStrategy blockedStrategy = newAnotherPkg()
pkgGlobs := cfg.pkgGlobs.Value()
if len(pkgGlobs) > 0 {
defaultStrategy := blockedStrategy
if cfg.onlyPkgGlobs {
defaultStrategy = newNilPkg()
}
blockedStrategy = newBlockedPkgs(
pkgGlobs,
defaultStrategy,
)
}
for _, file := range pass.Files {
v := &visitor{
pass: pass,
blockedStrategy: blockedStrategy,
}
v.walk(file)
}
return nil, nil
}
}
type visitor struct {
pass *analysis.Pass
blockedStrategy blockedStrategy
}
func (v *visitor) walk(n ast.Node) {
if n != nil {
ast.Walk(v, n)
}
}
func (v *visitor) Visit(node ast.Node) ast.Visitor {
// casting pkg.Struct(struct{}{})
callExpr, ok := node.(*ast.CallExpr)
if ok {
ident := v.getIdent(callExpr.Fun)
identObj := v.pass.TypesInfo.ObjectOf(ident)
_, isTypeName := identObj.(*types.TypeName)
if isTypeName {
if v.blockedStrategy.IsBlocked(v.pass.Pkg, identObj) {
v.report(ident, identObj)
}
}
return v
}
compLit, ok := node.(*ast.CompositeLit)
if !ok {
return v
}
compLitType := compLit.Type
// check []*Struct{{},&Struct}
slice, isMap := compLitType.(*ast.ArrayType)
if isMap && len(compLit.Elts) > 0 {
v.checkSlice(slice, compLit)
return v
}
// check map[Struct]Struct{{}:{}}
mp, isMap := compLitType.(*ast.MapType)
if isMap {
v.checkMap(mp, compLit)
return v
}
// check Struct{}
ident := v.getIdent(compLitType)
identObj := v.pass.TypesInfo.ObjectOf(ident)
if identObj == nil {
return v
}
if v.blockedStrategy.IsBlocked(v.pass.Pkg, identObj) {
v.report(ident, identObj)
}
return v
}
func (v *visitor) getIdent(expr ast.Expr) *ast.Ident {
// pointer *Struct{}
if starExpr, ok := expr.(*ast.StarExpr); ok {
expr = starExpr.X
}
// generic Struct[any]{}
indexExpr, ok := expr.(*ast.IndexExpr)
if ok {
expr = indexExpr.X
}
selExpr, ok := expr.(*ast.SelectorExpr)
if !ok {
return nil
}
return selExpr.Sel
}
func (v *visitor) checkSlice(arr *ast.ArrayType, compLit *ast.CompositeLit) {
ident := v.getIdent(arr.Elt)
identObj := v.pass.TypesInfo.ObjectOf(ident)
if identObj == nil {
return
}
for _, elt := range compLit.Elts {
v.checkBrackets(elt, identObj)
}
}
func (v *visitor) checkMap(mp *ast.MapType, compLit *ast.CompositeLit) {
keyIdent := v.getIdent(mp.Key)
keyIdentObj := v.pass.TypesInfo.ObjectOf(keyIdent)
valueIdent := v.getIdent(mp.Value)
valueIdentObj := v.pass.TypesInfo.ObjectOf(valueIdent)
if keyIdentObj == nil && valueIdentObj == nil {
return
}
for _, elt := range compLit.Elts {
keyValueExpr, ok := elt.(*ast.KeyValueExpr)
if !ok {
v.unexpectedCode(elt)
continue
}
v.checkBrackets(keyValueExpr.Key, keyIdentObj)
v.checkBrackets(keyValueExpr.Value, valueIdentObj)
}
}
// checkBrackets check {} in array, slice, map.
func (v *visitor) checkBrackets(expr ast.Expr, identObj types.Object) {
compLit, ok := expr.(*ast.CompositeLit)
if ok && compLit.Type == nil && identObj != nil {
if v.blockedStrategy.IsBlocked(v.pass.Pkg, identObj) {
v.report(compLit, identObj)
}
}
}
func (v *visitor) report(node ast.Node, obj types.Object) {
v.pass.Reportf(
node.Pos(),
"Use factory for %s.%s", obj.Pkg().Name(), obj.Name(),
)
}
func (v *visitor) unexpectedCode(node ast.Node) {
log.Printf("%s: unexpected code in %s, please report to the developer with example.\n",
name,
v.pass.Fset.Position(node.Pos()),
)
}
func containsMatchGlob(globs []glob.Glob, el string) bool {
for _, g := range globs {
if g.Match(el) {
return true
}
}
return false
}