-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_cross_pkg_links.go
94 lines (83 loc) · 2.44 KB
/
detect_cross_pkg_links.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
package main
import (
"go/ast"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
)
var DetectCrossPkgLinks = &analysis.Analyzer{
Name: "cross_package_linter",
Doc: `-`,
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
func getPkgName(in ast.Expr) string {
switch ww := in.(type) {
case *ast.Ident: // aws_credentials.Credentials
return ww.Name
case *ast.SelectorExpr: // buildinfo.Info.ArcadiaSourceRevision
return getPkgName(ww.X)
case *ast.CallExpr:
return getPkgName(ww.Fun)
case *ast.IndexExpr: // s.stats[key].Inc() -> s
return getPkgName(ww.X)
case *ast.TypeAssertExpr: // req.(*console.ExecuteOperationRequest).Operation -> req
return getPkgName(ww.X)
case *ast.ParenExpr: // (Kafka)
return getPkgName(ww.X)
case *ast.StarExpr: // unsafe.Pointer(&pattern)).Data
return ""
case *ast.CompositeLit: // protojson.MarshalOptions{UseProtoNames: true}.Marshal(logSafeMessage) - speaking about 'UseProtoNames'
return ""
case *ast.UnaryExpr: // &net.Dialer{
return getPkgName(ww.X)
default:
panic("!")
}
}
func run(pass *analysis.Pass) (interface{}, error) {
mu.Lock()
defer mu.Unlock()
cacheObj := newObject()
for k := range pkgToObjToType {
cacheObj.add(k)
}
for k := range pkgToTypeToMethods {
cacheObj.add(k)
}
for _, file := range pass.Files {
imports := extractImports(file) // shortName->importPath
ast.Inspect(file, func(node ast.Node) bool {
if selectorExpr, ok := node.(*ast.SelectorExpr); ok {
pkgName := getPkgName(selectorExpr)
if pkgName == "" {
return true
}
objName := selectorExpr.Sel.Name // objName
if importFullPath, ok := imports[pkgName]; ok {
fullPath := cacheObj.fullPathByShortPath(importFullPath)
if fullPath == "" {
return true
}
//------------------------------------------------------
// remove obj
if _, ok := pkgToObjToType[fullPath]; ok {
if _, ok := pkgToObjToType[fullPath][objName]; ok {
delete(pkgToObjToType[fullPath], objName)
}
}
//------------------------------------------------------
// if object is constructor - then remove all return types
if _, ok := pkgToCtorToRetTypes[fullPath]; ok {
if retTypes, ok := pkgToCtorToRetTypes[fullPath][objName]; ok {
for _, retType := range retTypes {
delete(pkgToObjToType[fullPath], retType)
}
}
}
}
}
return true
})
}
return nil, nil
}