-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlet.go
49 lines (42 loc) · 887 Bytes
/
let.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
package main
import (
"github.com/tdewolff/parse/v2/js"
"strconv"
)
var lastUnique int
func unique() string {
lastUnique++
return strconv.Itoa(lastUnique)
}
type convertLetConst struct {
parent js.INode
scope *js.Scope
block *js.BlockStmt
}
func ConvertLetConst(ast *js.AST) {
js.Walk(convertLetConst{}, ast)
}
func (c convertLetConst) Enter(n js.INode) js.IVisitor {
switch v := n.(type) {
case *js.VarDecl:
for _, be := range v.List {
vr := be.Binding.(*js.Var)
if v.TokenType == js.VarToken && vr.Decl == js.VariableDecl {
continue
}
v.TokenType = js.VarToken
vr.Decl = js.VariableDecl
if c.scope.Parent == nil {
break
}
vr.Data = []byte(string(vr.Data) + "$" + unique())
}
}
if blk, ok := n.(*js.BlockStmt); ok {
c.block = blk
c.scope = &blk.Scope
}
c.parent = n
return c
}
func (c convertLetConst) Exit(n js.INode) {}