Debian Codegraph @

ast/walk_test.go

raw symbols SCIP
1 package ast_test
2
3 import (
4 "testing"
5
6 "github.com/robertkrimen/otto/ast"
7 "github.com/robertkrimen/otto/file"
8 "github.com/robertkrimen/otto/parser"
9 "github.com/stretchr/testify/require"
10 )
11
12 type walker struct {
13 seen map[ast.Node]struct{}
14 source string
15 stack []ast.Node
16 shift file.Idx
17 duplicate int
18 newExpressionIdx1 file.Idx
19 }
20
21 // push and pop below are to prove the symmetry of Enter/Exit calls
22
23 func (w *walker) push(n ast.Node) {
24 w.stack = append(w.stack, n)
25 }
26
27 func (w *walker) pop(n ast.Node) {
28 size := len(w.stack)
29 if size <= 0 {
30 panic("pop of empty stack")
31 }
32
33 if toPop := w.stack[size-1]; toPop != n {
34 panic("pop: nodes do not equal")
35 }
36
37 w.stack[size-1] = nil
38 w.stack = w.stack[:size-1]
39 }
40
41 func (w *walker) Enter(n ast.Node) ast.Visitor {
42 w.push(n)
43 if _, ok := w.seen[n]; ok {
44 // Skip items we've already seen which occurs due to declarations.
45 w.duplicate++
46 return w
47 }
48
49 w.seen[n] = struct{}{}
50
51 switch t := n.(type) {
52 case *ast.Identifier:
53 if t != nil {
54 idx := n.Idx0() + w.shift - 1
55 s := w.source[:idx] + "IDENT_" + w.source[idx:]
56 w.source = s
57 w.shift += 6
58 }
59 case *ast.VariableExpression:
60 if t != nil {
61 idx := n.Idx0() + w.shift - 1
62 s := w.source[:idx] + "VAR_" + w.source[idx:]
63 w.source = s
64 w.shift += 4
65 }
66 case *ast.NewExpression:
67 w.newExpressionIdx1 = n.Idx1()
68 }
69
70 return w
71 }
72
73 func (w *walker) Exit(n ast.Node) {
74 w.pop(n)
75 }
76
77 func TestVisitorRewrite(t *testing.T) {
78 source := `var b = function() {
79 test();
80 try {} catch(e) {}
81 var test = "test(); var test = 1"
82 } // test`
83 program, err := parser.ParseFile(nil, "", source, 0)
84 require.NoError(t, err)
85
86 w := &walker{
87 source: source,
88 seen: make(map[ast.Node]struct{}),
89 }
90 ast.Walk(w, program)
91
92 xformed := `var VAR_b = function() {
93 IDENT_test();
94 try {} catch(IDENT_e) {}
95 var VAR_test = "test(); var test = 1"
96 } // test`
97
98 require.Equal(t, xformed, w.source)
99 require.Empty(t, w.stack)
100 require.Zero(t, w.duplicate)
101 }
102
103 func Test_issue261(t *testing.T) {
104 tests := map[string]struct {
105 code string
106 want file.Idx
107 }{
108 "no-parenthesis": {
109 code: `var i = new Image;`,
110 want: 18,
111 },
112 "no-args": {
113 code: `var i = new Image();`,
114 want: 20,
115 },
116 "two-args": {
117 code: `var i = new Image(1, 2);`,
118 want: 24,
119 },
120 }
121
122 for name, tt := range tests {
123 t.Run(name, func(t *testing.T) {
124 prog, err := parser.ParseFile(nil, "", tt.code, 0)
125 require.NoError(t, err)
126
127 w := &walker{
128 source: tt.code,
129 seen: make(map[ast.Node]struct{}),
130 }
131 ast.Walk(w, prog)
132
133 require.Equal(t, tt.want, w.newExpressionIdx1)
134 require.Empty(t, w.stack)
135 require.Zero(t, w.duplicate)
136 })
137 }
138 }
139
140 func TestBadStatement(t *testing.T) {
141 source := `
142 var abc;
143 break; do {
144 } while(true);
145 `
146 program, err := parser.ParseFile(nil, "", source, 0)
147 require.ErrorContains(t, err, "Illegal break statement")
148
149 w := &walker{
150 source: source,
151 seen: make(map[ast.Node]struct{}),
152 }
153
154 require.NotPanics(t, func() {
155 ast.Walk(w, program)
156 })
157 }