/
parser-util.go
339 lines (294 loc) · 7.43 KB
/
parser-util.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package gen
import (
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"io"
"sort"
"strings"
// "github.com/vugu/vugu/internal/htmlx"
// "golang.org/x/net/html"
"github.com/vugu/html"
"github.com/vugu/vugu"
)
func attrFromHtml(attr html.Attribute) vugu.VGAttribute {
return vugu.VGAttribute{
Namespace: attr.Namespace,
Key: attr.OrigKey,
Val: attr.Val,
}
}
// func attrFromHtmlx(attr htmlx.Attribute) vugu.VGAttribute {
// return vugu.VGAttribute{
// Namespace: attr.Namespace,
// Key: attr.Key,
// Val: attr.Val,
// }
// }
// stuff that is common to both parsers can get moved into here
func staticVGAttr(inAttr []html.Attribute) (ret []vugu.VGAttribute) {
for _, a := range inAttr {
switch {
// case a.Key == "vg-if":
// case a.Key == "vg-for":
// case a.Key == "vg-key":
// case a.Key == "vg-html":
case strings.HasPrefix(a.Key, "vg-"):
case strings.HasPrefix(a.Key, "."):
case strings.HasPrefix(a.Key, ":"):
case strings.HasPrefix(a.Key, "@"):
default:
ret = append(ret, attrFromHtml(a))
}
}
return ret
}
func vgSlotName(n *html.Node) string {
for _, a := range n.Attr {
if a.Key == "name" {
return a.Val
}
}
return ""
}
func vgVarExpr(n *html.Node) string {
for _, a := range n.Attr {
if a.Key == "vg-var" {
return a.Val
}
}
return ""
}
func vgIfExpr(n *html.Node) string {
for _, a := range n.Attr {
if a.Key == "vg-if" {
return a.Val
}
}
return ""
}
func vgKeyExpr(n *html.Node) string {
for _, a := range n.Attr {
if a.Key == "vg-key" {
return a.Val
}
}
return ""
}
func vgCompExpr(n *html.Node) string {
for _, a := range n.Attr {
if a.Key == "expr" {
return a.Val
}
}
return ""
}
// func vgIfExprx(n *htmlx.Node) string {
// for _, a := range n.Attr {
// if a.Key == "vg-if" {
// return a.Val
// }
// }
// return ""
// }
type vgForAttr struct {
expr string
noshadow bool
}
func vgForExpr(n *html.Node) (vgForAttr, error) {
for _, a := range n.Attr {
if strings.HasPrefix(a.Key, "vg-for") {
v := vgForAttr{expr: strings.TrimSpace(a.Val)}
opts := strings.Split(a.Key, ".")
if len(opts) > 1 {
for _, opt := range opts[1:] {
switch opt {
case "noshadow":
v.noshadow = true
default:
return vgForAttr{}, fmt.Errorf("option %q unknown", opt)
}
}
}
return v, nil
}
}
return vgForAttr{}, nil
}
func vgHTMLExpr(n *html.Node) string {
for _, a := range n.Attr {
// vg-html and vg-content are the same thing,
// the name vg-content was introduced to call out
// the difference between Vue's v-html attribute
// which does not perform escaping.
if a.Key == "vg-html" {
return a.Val
}
if a.Key == "vg-content" {
return a.Val
}
}
return ""
}
// extract ":attr" stuff from a node
func dynamicVGAttrExpr(n *html.Node) (ret map[string]string, retKeys []string) {
var da []html.Attribute
// get dynamic attrs first
for _, a := range n.Attr {
// ":" and "vg-attr" are the AttributeLister case
if strings.HasPrefix(a.OrigKey, ":") || a.OrigKey == "vg-attr" {
da = append(da, a)
}
}
if len(da) == 0 { // don't allocate map if we don't have to
return
}
// make map as small as possible
ret = make(map[string]string, len(da))
retKeys = make([]string, len(da))
for i, a := range da {
k := strings.TrimPrefix(a.OrigKey, ":")
retKeys[i] = k
ret[k] = a.Val
}
sort.Strings(retKeys)
return
}
// extract ".prop" stuff from a node
func propVGAttrExpr(n *html.Node) (ret map[string]string, retKeys []string) {
var da []html.Attribute
// get prop attrs first
for _, a := range n.Attr {
if strings.HasPrefix(a.OrigKey, ".") {
da = append(da, a)
}
}
if len(da) == 0 { // don't allocate map if we don't have to
return
}
// make map as small as possible
ret = make(map[string]string, len(da))
retKeys = make([]string, len(da))
for i, a := range da {
k := strings.TrimPrefix(a.OrigKey, ".")
retKeys[i] = k
ret[k] = a.Val
}
sort.Strings(retKeys)
return
}
// returns vg-js-create and vg-js-populate
func jsCallbackVGAttrExpr(n *html.Node) (ret map[string]string) {
for _, attr := range n.Attr {
if strings.HasPrefix(attr.OrigKey, "vg-js-") {
if ret == nil {
ret = make(map[string]string, 2)
}
ret[attr.OrigKey] = attr.Val
}
}
return ret
}
func vgDOMEventExprs(n *html.Node) (ret map[string]string, retKeys []string) {
return vgEventExprs(n)
}
// extract "@event" stuff from a node
func vgEventExprs(n *html.Node) (ret map[string]string, retKeys []string) {
var da []html.Attribute
// get attrs first
for _, a := range n.Attr {
if strings.HasPrefix(a.OrigKey, "@") {
da = append(da, a)
}
}
if len(da) == 0 { // don't allocate map if we don't have to
return
}
// make map as small as possible
ret = make(map[string]string, len(da))
for _, a := range da {
k := strings.TrimPrefix(a.OrigKey, "@")
retKeys = append(retKeys, k)
ret[k] = a.Val
}
return
}
// var vgDOMParseExprRE = regexp.MustCompile(`^([a-zA-Z0-9_.]+)\((.*)\)$`)
// func vgDOMParseExpr(expr string) (receiver string, methodName string, argList string) {
// parts := vgDOMParseExprRE.FindStringSubmatch(expr)
// if len(parts) != 3 {
// return
// }
// argList = parts[2]
// f := parts[1]
// fparts := strings.Split(f, ".")
// receiver, methodName = strings.Join(fparts[:len(fparts)-1], "."), fparts[len(fparts)-1]
// // if len(fparts) == 1 { // just "methodName"
// // methodName = f
// // } else if len(fparts) > 2 { // "a.b.MethodName"
// // receiver, methodName = strings.Join(fparts[:len(fparts)-1], "."), fparts[len(fparts)-1]
// // } else { // "a.MethodName"
// // receiver, methodName = fparts[0], fparts[1]
// // }
// return
// }
// ^([a-zA-Z0-9_.]+)\((.*)\)$
// dedupImports reads Go source and removes duplicate import statements.
func dedupImports(r io.Reader, w io.Writer, fname string) error {
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, fname, r, parser.AllErrors|parser.ParseComments)
if err != nil {
return err
}
// ast.Print(fset, f)
// ast.Print(fset, f)
// ast.Print(fset, f.Decls)
// f.Decls = f.Decls[1:]
dedupAstFileImports(f)
ast.SortImports(fset, f)
err = printer.Fprint(w, fset, f)
if err != nil {
return err
}
return nil
}
func dedupAstFileImports(f *ast.File) {
imap := make(map[string]bool, len(f.Imports)+10)
outdecls := make([]ast.Decl, 0, len(f.Decls))
for _, decl := range f.Decls {
// check for import declaration
genDecl, _ := decl.(*ast.GenDecl)
// not an import declaration, just copy and continue
if genDecl == nil || genDecl.Tok != token.IMPORT {
outdecls = append(outdecls, decl)
continue
}
// for imports, we loop over each ImportSpec (each package, regardless of which form of import statement)
outspecs := make([]ast.Spec, 0, len(genDecl.Specs))
for _, spec := range genDecl.Specs {
ispec := spec.(*ast.ImportSpec)
// always use path
key := ispec.Path.Value
// if name is present, prepend
if ispec.Name != nil {
key = ispec.Name.Name + " " + key
}
// if we've seen this import before, then just move to the next
if imap[key] {
continue
}
imap[key] = true // mark this import as having been seen
// keep the import
outspecs = append(outspecs, ispec)
}
// use outspecs for this import decl, unless it's empty in which case we remove/skip the whole import decl
if len(outspecs) == 0 {
continue
}
genDecl.Specs = outspecs
outdecls = append(outdecls, genDecl)
}
f.Decls = outdecls
}