/
common.go
133 lines (121 loc) · 3.16 KB
/
common.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
package main
import (
"go/ast"
"go/parser"
"go/token"
"strings"
"golang.org/x/tools/go/ast/astutil"
)
func createStmt(s string) ast.Stmt {
return &ast.ExprStmt{X: &ast.BasicLit{Value: s}}
}
func parseFile(solver *Solver, fpath string) *File {
fileSet := token.NewFileSet()
fileAst, err := parser.ParseFile(fileSet, fpath, nil, parser.ParseComments)
if nil != err {
panic(err)
}
fileCmtMap := ast.NewCommentMap(fileSet, fileAst, fileAst.Comments)
f := &File{
ast: fileAst,
fileSet: fileSet,
commentMap: fileCmtMap,
path: fpath,
imports: map[string]string{},
}
for _, im := range f.ast.Imports {
path := im.Path.Value[1 : len(im.Path.Value)-1]
if !strings.HasPrefix(path, solver.basePackagePath) {
continue
}
path = path[len(solver.basePackagePath):]
if nil != im.Name {
f.imports[im.Name.Name] = path
} else {
es := strings.Split(path, "/")
f.imports[es[len(es)-1]] = path
}
}
return f
}
func importPackages(f *File, names [][2]string) {
for _, pair := range names {
if len(pair[1]) > 0 {
astutil.AddNamedImport(f.fileSet, f.ast, pair[1], pair[0])
} else {
astutil.AddImport(f.fileSet, f.ast, pair[0])
}
}
ast.SortImports(f.fileSet, f.ast)
f.modified = true
}
func checkNotation(f *File, node ast.Node, notation string) bool {
cmts := f.commentMap.Filter(node).Comments()
if 0 == len(cmts) {
return false
}
for _, line := range cmts[0].List {
if "//@"+notation == line.Text {
return true
}
}
return false
}
func prependStatements(funcDecl *ast.FuncDecl, prefStmts []ast.Stmt) {
stmts := make([]ast.Stmt, len(prefStmts)+len(funcDecl.Body.List))
copy(stmts, prefStmts)
if len(funcDecl.Body.List) > 0 {
copy(stmts[len(prefStmts):], funcDecl.Body.List)
}
funcDecl.Body.List = stmts
}
func checkComponentCall(solver *Solver, expr ast.Expr) *ast.CallExpr {
callExpr, ok := expr.(*ast.CallExpr)
if !ok {
return nil
}
switch funExpr := callExpr.Fun.(type) {
case *ast.SelectorExpr:
x, ok := funExpr.X.(*ast.Ident)
if !ok {
return nil
}
packagePath, ok := solver.file.imports[x.Name]
if !ok || !solver.componentFuncs.check(packagePath, funExpr.Sel.Name) {
return nil
}
case *ast.Ident:
if !solver.componentFuncs.check(solver.packagePath, funExpr.Name) {
return nil
}
default:
return nil
}
return callExpr
}
func processComponentCall(solver *Solver, stmts []ast.Stmt, componentVarName string) {
for _, stmt := range stmts {
var callExpr *ast.CallExpr
switch s := stmt.(type) {
case *ast.IfStmt:
processComponentCall(solver, s.Body.List, componentVarName)
case *ast.ForStmt:
processComponentCall(solver, s.Body.List, componentVarName)
case *ast.BlockStmt:
processComponentCall(solver, s.List, componentVarName)
case *ast.SwitchStmt:
processComponentCall(solver, s.Body.List, componentVarName)
case *ast.CaseClause:
processComponentCall(solver, s.Body, componentVarName)
case *ast.AssignStmt:
if 1 == len(s.Rhs) {
callExpr = checkComponentCall(solver, s.Rhs[0])
}
case *ast.ExprStmt:
callExpr = checkComponentCall(solver, s.X)
}
if nil != callExpr {
callExpr.Args = append(callExpr.Args, &ast.BasicLit{Value: componentVarName})
}
}
}