/
visitor.go
151 lines (135 loc) · 4.5 KB
/
visitor.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
package parser
import (
"go/ast"
"go/token"
"strings"
)
type Visitor struct {
// map[locale][]MessageStruct
// example:
//{
// "English": [
// {
// "PkgName": "x/i18n",
// "StructName": "Messages_en_US"
// }
// ],
//}
RigisterMap map[string][]MessageStruct
// map[locale][]MessageStruct
// example:
//{
// "Messages_en_US": "English",
// "Messages_zh_CN": "SimplifiedChinese"
//}
LocalesMap map[string]string
// map[pkgName][]structs
// example:
//{
// "x/i18n": []*ast.GenDecl,
//}
Variables map[string][]*ast.GenDecl
// the current package path
// example:
// when visit file "x/i18n/i18n_test.go", currentPkgPath = "x/i18n"
currentPkgPath string
// the current import map
// example:
// when visit file "x/i18n/i18n_test.go", currentImportMap =
//{
// "fmt": "fmt",
// "http": "net/http",
// "httptest": "net/http/httptest",
// "strings": "strings",
// "testing": "testing"
// "i18n": "github.com/qor5/x/i18n",
// "testingutils": "github.com/theplant/testingutils",
// "language": "golang.org/x/text/language",
//}
currentImportMap map[string]string
projectParentPath string
fset *token.FileSet
}
type MessageStruct struct {
PkgName string
StructName string
}
func (v *Visitor) Visit(node ast.Node) ast.Visitor {
// find all global variables and insert it into v.Variables
if f, ok := node.(*ast.File); ok {
var temp = strings.Split(v.fset.File(f.Package).Name(), "/")
pkgName := strings.TrimPrefix(strings.Join(temp[:len(temp)-1], "/"), strings.TrimSuffix(v.projectParentPath, "/")+"/")
for _, decl := range f.Decls {
if genDecl, ok := decl.(*ast.GenDecl); ok && genDecl.Tok == token.VAR {
v.Variables[pkgName] = append(v.Variables[pkgName], genDecl)
}
}
}
// find all places calling func RegisterForModule
// analyze the calls and fill it into v.LocalesMap and v.RigisterMap
if callExpr, ok := node.(*ast.CallExpr); ok {
if selectorExpr, ok := callExpr.Fun.(*ast.SelectorExpr); ok && selectorExpr.Sel.Name == "RegisterForModule" {
if len(callExpr.Args) == 3 {
if selectorExpr2, ok := callExpr.Args[0].(*ast.SelectorExpr); ok {
if ident, ok := selectorExpr2.X.(*ast.Ident); ok && ident.Name == "language" {
if ident2, ok := callExpr.Args[2].(*ast.Ident); ok {
var messageStruct MessageStruct
messageStruct.PkgName = strings.TrimPrefix(v.currentPkgPath, strings.TrimSuffix(v.projectParentPath, "/")+"/")
messageStruct.StructName = ident2.Name
v.LocalesMap[messageStruct.StructName] = selectorExpr2.Sel.Name
v.RigisterMap[selectorExpr2.Sel.Name] = append(v.RigisterMap[selectorExpr2.Sel.Name], messageStruct)
}
if selectorExpr3, ok := callExpr.Args[2].(*ast.SelectorExpr); ok {
var messageStruct MessageStruct
messageStruct.PkgName = v.currentImportMap[selectorExpr3.X.(*ast.Ident).Name]
messageStruct.StructName = selectorExpr3.Sel.Name
v.LocalesMap[messageStruct.StructName] = selectorExpr2.Sel.Name
v.RigisterMap[selectorExpr2.Sel.Name] = append(v.RigisterMap[selectorExpr2.Sel.Name], messageStruct)
}
}
}
}
}
}
return v
}
// declare a Visitor
// walk all files and fill the RigisterMap, LocalesMap and Variables
func newVisitorAndWalk(fset *token.FileSet, pkgs map[string]*ast.Package, projectPath string) (v *Visitor, err error) {
v = &Visitor{
RigisterMap: make(map[string][]MessageStruct),
LocalesMap: make(map[string]string),
Variables: make(map[string][]*ast.GenDecl),
currentImportMap: make(map[string]string),
fset: fset,
}
for pkgPath, pkg := range pkgs {
for _, f := range pkg.Files {
for _, decl := range f.Decls {
if decl, ok := decl.(*ast.GenDecl); ok {
if decl.Tok == token.IMPORT {
for _, spec := range decl.Specs {
if spec, ok := spec.(*ast.ImportSpec); ok {
var importName string
var importValue string
if spec.Name == nil {
var temp = strings.Split(strings.Trim(spec.Path.Value, "\""), "/")
importName = temp[len(temp)-1]
} else {
importName = spec.Name.Name
}
importValue = strings.Trim(spec.Path.Value, "\"")
v.currentImportMap[importName] = importValue
}
}
}
}
}
v.currentPkgPath = pkgPath
var temp = strings.Split(projectPath, "/")
v.projectParentPath = strings.Join(temp[:len(temp)-1], "/")
ast.Walk(v, f)
}
}
return
}