forked from myitcv/react
/
gen.go
232 lines (181 loc) · 4.51 KB
/
gen.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
// Copyright (c) 2016 Paul Jolly <paul@myitcv.org.uk>, all rights reserved.
// Use of this document is governed by a license found in the LICENSE document.
package main
import (
"bytes"
"go/ast"
"go/build"
"go/parser"
"go/printer"
"go/token"
"os/exec"
"path"
"strings"
"myitcv.io/gogenerate"
)
const (
reactPkg = "github.com/lijianying10/react"
compDefName = "ComponentDef"
compDefSuffix = "Def"
stateTypeSuffix = "State"
propsTypeSuffix = "Props"
propsTypeTmplPrefix = "_"
getInitialState = "GetInitialState"
componentWillReceiveProps = "ComponentWillReceiveProps"
componentDidUpdate = "ComponentDidUpdate"
equals = "Equals"
)
type typeFile struct {
ts *ast.TypeSpec
file *ast.File
}
type funcFile struct {
fn *ast.FuncDecl
file *ast.File
}
var fset = token.NewFileSet()
func astNodeString(i interface{}) string {
b := bytes.NewBuffer(nil)
err := printer.Fprint(b, fset, i)
if err != nil {
fatalf("failed to astNodeString %v: %v", i, err)
}
return b.String()
}
func goFmtBuf(b *bytes.Buffer) (*bytes.Buffer, error) {
out := bytes.NewBuffer(nil)
cmd := exec.Command("gofmt", "-s")
cmd.Stdin = b
cmd.Stdout = out
err := cmd.Run()
return out, err
}
type gen struct {
pkg string
pkgImpPath string
isReactCore bool
propsTmpls map[string]typeFile
components map[string]typeFile
types map[string]typeFile
pointMeths map[string][]funcFile
nonPointMeths map[string][]funcFile
}
func dogen(dir, license string) {
bpkg, err := build.ImportDir(dir, 0)
if err != nil {
fatalf("unable to import pkg in dir %v: %v", dir, err)
}
isReactCore := bpkg.ImportPath == reactPkg
pkgs, err := parser.ParseDir(fset, dir, nil, parser.ParseComments)
if err != nil {
fatalf("unable to parse %v: %v", dir, err)
}
// we intentionally walk all packages, i.e. the package in the current directory
// and any x-test package that may also be present
for pn, pkg := range pkgs {
g := &gen{
pkg: pn,
pkgImpPath: bpkg.ImportPath,
isReactCore: isReactCore,
propsTmpls: make(map[string]typeFile),
components: make(map[string]typeFile),
types: make(map[string]typeFile),
pointMeths: make(map[string][]funcFile),
nonPointMeths: make(map[string][]funcFile),
}
for fn, file := range pkg.Files {
if gogenerate.FileGeneratedBy(fn, "reactGen") {
continue
}
foundImp := false
impName := ""
for _, i := range file.Imports {
p := strings.Trim(i.Path.Value, "\"")
if p == reactPkg {
foundImp = true
if i.Name != nil {
impName = i.Name.Name
} else {
impName = path.Base(reactPkg)
}
break
}
}
if !foundImp && !isReactCore {
continue
}
for _, d := range file.Decls {
switch d := d.(type) {
case *ast.FuncDecl:
if d.Recv == nil {
continue
}
f := d.Recv.List[0]
switch v := f.Type.(type) {
case *ast.StarExpr:
id, ok := v.X.(*ast.Ident)
if !ok {
continue
}
g.pointMeths[id.Name] = append(g.pointMeths[id.Name], funcFile{d, file})
case *ast.Ident:
g.nonPointMeths[v.Name] = append(g.nonPointMeths[v.Name], funcFile{d, file})
}
case *ast.GenDecl:
if d.Tok != token.TYPE {
continue
}
for _, ts := range d.Specs {
ts := ts.(*ast.TypeSpec)
st, ok := ts.Type.(*ast.StructType)
if !ok || st.Fields == nil {
continue
}
if n := ts.Name.Name; strings.HasPrefix(n, propsTypeTmplPrefix) &&
strings.HasSuffix(n, propsTypeSuffix) {
if ts.Doc == nil {
ts.Doc = d.Doc
}
g.propsTmpls[n] = typeFile{
ts: ts,
file: file,
}
continue
}
foundAnon := false
for _, f := range st.Fields.List {
if f.Names != nil {
// it must be anonymous
continue
}
se, ok := f.Type.(*ast.SelectorExpr)
if !ok {
continue
}
if se.Sel.Name != compDefName {
continue
}
id, ok := se.X.(*ast.Ident)
if !ok {
continue
}
if id.Name != impName {
continue
}
foundAnon = true
}
if foundAnon && strings.HasSuffix(ts.Name.Name, compDefSuffix) {
g.components[ts.Name.Name] = typeFile{ts, file}
} else {
g.types[ts.Name.Name] = typeFile{ts, file}
}
}
}
}
}
// at this point we have the components and their methods
for cd := range g.components {
g.genComp(cd)
}
}
}