-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharvest_pkg_lvl_entities.go
265 lines (243 loc) · 6.82 KB
/
harvest_pkg_lvl_entities.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
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
package main
import (
"go/ast"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"path"
"strings"
"unicode"
)
var HarvestPkgLvlEntities = &analysis.Analyzer{
Name: "cross_package_linter",
Doc: `-`,
Run: runHarvestPkgLvlEntities,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
type objectType int
const (
objFunc objectType = iota + 1
objConst
objVar
objType
)
func extractPkgLevelObjects(pass *analysis.Pass) map[string]objectType {
objToType := make(map[string]objectType)
names := pass.Pkg.Scope().Names()
for _, name := range names {
currNameObj := pass.Pkg.Scope().Lookup(name)
if !currNameObj.Exported() {
continue
}
switch t := currNameObj.(type) {
case *types.Var:
objToType[t.Name()] = objVar
case *types.Const:
objToType[t.Name()] = objConst
case *types.TypeName:
//q := t.Type().Underlying()
//switch q.(type) {
//case *types.Interface:
// fmt.Println("!")
//case *types.Struct:
// fmt.Println("!")
//case *types.Basic: // it's when alias to basic type
// fmt.Println("!")
//case *types.Slice: // it's when alias to slice of basic types
// fmt.Println("!")
//case *types.Signature: // it's when alias to func
// fmt.Println("!")
//case *types.Map: // it's when alias to map
// fmt.Println("!")
//case *types.Pointer: // it's when alias to pointer (on another typename, for example)
// fmt.Println("!")
//default:
// panic("!")
//}
//fmt.Println(q)
objToType[t.Name()] = objType
case *types.Func:
objToType[t.Name()] = objFunc
default:
panic("!")
}
}
return objToType
}
func isExportedName(objName string) bool {
return unicode.IsUpper(rune(objName[0]))
}
func runHarvestPkgLvlEntities(pass *analysis.Pass) (interface{}, error) {
mu.Lock()
defer mu.Unlock()
var currPackageFullPath string
for _, file := range pass.Files {
pathToFile := getPathToFile(pass, file)
currPackageFullPath = path.Dir(pathToFile)
break
}
objToType := extractPkgLevelObjects(pass)
typeToMethods := make(map[string]map[string]bool)
ctorToOutTypes := make(map[string][]string)
for _, file := range pass.Files {
pathToFile := getPathToFile(pass, file)
if strings.HasSuffix(pathToFile, "_test.go") || strings.HasSuffix(pathToFile, "_mock.go") {
continue
}
if strings.Contains(pathToFile, "/go/tests/") {
continue
}
if isProtoCodeGeneratedFile(pathToFile) {
continue
}
//------------------------------------------------------------------------
// handle methods
ast.Inspect(file, func(node ast.Node) bool {
if funcDecl, ok := node.(*ast.FuncDecl); ok {
if funcDecl.Recv != nil && len(funcDecl.Recv.List) > 1 {
panic("!")
}
if funcDecl.Recv != nil && len(funcDecl.Recv.List) != 0 {
var objName string
switch e := funcDecl.Recv.List[0].Type.(type) {
case *ast.StarExpr:
switch ee := e.X.(type) {
case *ast.Ident: // func (r *ChannelReader) Close()
objName = ee.Name
case *ast.IndexExpr: // func (s *Set[T]) Add(value T)
switch eee := ee.X.(type) {
case *ast.Ident:
objName = eee.Name
default:
panic("!")
}
case *ast.IndexListExpr: // func (d *MapIter[T, R]) Value() (R, error)
switch eee := ee.X.(type) {
case *ast.Ident:
objName = eee.Name
default:
panic("!")
}
default:
panic("!")
}
case *ast.IndexExpr: // func (c Comparator[T]) Compare() {...}
switch ee := e.X.(type) {
case *ast.Ident:
objName = ee.Name
default:
panic("!")
}
case *ast.Ident: // func (v Values) GetTS() time.Time {...}
objName = e.Name
default:
panic("!")
}
if objName == "" {
panic("!")
}
methodName := funcDecl.Name.Name
//-------------------------------------------------------------
if isExportedName(methodName) {
if _, ok := typeToMethods[currPackageFullPath]; !ok {
typeToMethods = make(map[string]map[string]bool)
}
if _, ok := typeToMethods[objName]; !ok {
typeToMethods[objName] = make(map[string]bool)
}
typeToMethods[objName][methodName] = true
}
}
}
return true
})
//------------------------------------------------------------------------
// handle constructors
ast.Inspect(file, func(node ast.Node) bool {
if funcDecl, ok := node.(*ast.FuncDecl); ok {
currCtorName := funcDecl.Name.String()
if funcDecl.Type.Results == nil {
return true
}
if isExportedName(currCtorName) {
for _, el := range funcDecl.Type.Results.List {
if ident, ok := el.Type.(*ast.StarExpr); ok {
retTypeName := "_"
switch retType := ident.X.(type) {
case *ast.Ident:
retTypeName = retType.Name
case *ast.IndexExpr:
if retTypeNameQ, ok := retType.X.(*ast.Ident); ok {
retTypeName = retTypeNameQ.Name
}
}
if _, ok := ctorToOutTypes[currCtorName]; !ok {
ctorToOutTypes[currCtorName] = make([]string, 0)
}
if isExportedName(retTypeName) {
ctorToOutTypes[currCtorName] = append(ctorToOutTypes[currCtorName], retTypeName)
}
}
}
}
}
return true
})
}
typesAlreadyExported := make(map[string]bool)
for _, file := range pass.Files {
ast.Inspect(file, func(node ast.Node) bool {
if funcDecl, ok := node.(*ast.FuncDecl); ok {
funcName := funcDecl.Name.Name
isExportedFunc := unicode.IsUpper(rune(funcName[0]))
if !isExportedFunc {
return true
}
if funcDecl == nil || funcDecl.Type == nil || funcDecl.Type.Results == nil || funcDecl.Type.Results.List == nil {
return true
}
results := funcDecl.Type.Results.List
for _, outputEl := range results {
if ident, ok := outputEl.Type.(*ast.Ident); ok {
if ident != nil {
methodName := ident.Name
if unicode.IsUpper(rune(methodName[0])) {
typesAlreadyExported[ident.Name] = true
}
}
}
}
}
return true
})
}
// fill results
for k, v := range objToType {
if _, ok := pkgToObjToType[currPackageFullPath]; !ok {
pkgToObjToType[currPackageFullPath] = make(map[string]objectType)
}
if typesAlreadyExported[k] {
continue
}
pkgToObjToType[currPackageFullPath][k] = v
}
for k, v := range typeToMethods {
if _, ok := pkgToTypeToMethods[currPackageFullPath]; !ok {
pkgToTypeToMethods[currPackageFullPath] = make(map[string]map[string]bool)
}
if typesAlreadyExported[k] {
continue
}
pkgToTypeToMethods[currPackageFullPath][k] = v
}
for k, v := range ctorToOutTypes {
if _, ok := pkgToCtorToRetTypes[currPackageFullPath]; !ok {
pkgToCtorToRetTypes[currPackageFullPath] = make(map[string][]string)
}
if typesAlreadyExported[k] {
continue
}
pkgToCtorToRetTypes[currPackageFullPath][k] = v
}
return nil, nil
}