@@ -10,11 +10,12 @@ import (
10
10
)
11
11
12
12
type walker struct {
13
- stack []ast.Node
14
- source string
15
- shift file.Idx
16
- seen map [ast.Node ]struct {}
17
- duplicate int
13
+ stack []ast.Node
14
+ source string
15
+ shift file.Idx
16
+ seen map [ast.Node ]struct {}
17
+ duplicate int
18
+ newExpressionIdx1 file.Idx
18
19
}
19
20
20
21
// push and pop below are to prove the symmetry of Enter/Exit calls
@@ -48,17 +49,23 @@ func (w *walker) Enter(n ast.Node) ast.Visitor {
48
49
49
50
w .seen [n ] = struct {}{}
50
51
51
- if id , ok := n .(* ast.Identifier ); ok && id != nil {
52
- idx := n .Idx0 () + w .shift - 1
53
- s := w .source [:idx ] + "IDENT_" + w .source [idx :]
54
- w .source = s
55
- w .shift += 6
56
- }
57
- if v , ok := n .(* ast.VariableExpression ); ok && v != nil {
58
- idx := n .Idx0 () + w .shift - 1
59
- s := w .source [:idx ] + "VAR_" + w .source [idx :]
60
- w .source = s
61
- w .shift += 4
52
+ switch t := n .(type ) {
53
+ case * ast.Identifier :
54
+ if t != nil {
55
+ idx := n .Idx0 () + w .shift - 1
56
+ s := w .source [:idx ] + "IDENT_" + w .source [idx :]
57
+ w .source = s
58
+ w .shift += 6
59
+ }
60
+ case * ast.VariableExpression :
61
+ if t != nil {
62
+ idx := n .Idx0 () + w .shift - 1
63
+ s := w .source [:idx ] + "VAR_" + w .source [idx :]
64
+ w .source = s
65
+ w .shift += 4
66
+ }
67
+ case * ast.NewExpression :
68
+ w .newExpressionIdx1 = n .Idx1 ()
62
69
}
63
70
64
71
return w
@@ -93,3 +100,40 @@ func TestVisitorRewrite(t *testing.T) {
93
100
require .Len (t , w .stack , 0 )
94
101
require .Equal (t , w .duplicate , 0 )
95
102
}
103
+
104
+ func Test_issue261 (t * testing.T ) {
105
+ tests := map [string ]struct {
106
+ code string
107
+ want file.Idx
108
+ }{
109
+ "no-parenthesis" : {
110
+ code : `var i = new Image;` ,
111
+ want : 18 ,
112
+ },
113
+ "no-args" : {
114
+ code : `var i = new Image();` ,
115
+ want : 20 ,
116
+ },
117
+ "two-args" : {
118
+ code : `var i = new Image(1, 2);` ,
119
+ want : 24 ,
120
+ },
121
+ }
122
+
123
+ for name , tt := range tests {
124
+ t .Run (name , func (t * testing.T ) {
125
+ prog , err := parser .ParseFile (nil , "" , tt .code , 0 )
126
+ require .NoError (t , err )
127
+
128
+ w := & walker {
129
+ source : tt .code ,
130
+ seen : make (map [ast.Node ]struct {}),
131
+ }
132
+ ast .Walk (w , prog )
133
+
134
+ require .Equal (t , tt .want , w .newExpressionIdx1 )
135
+ require .Len (t , w .stack , 0 )
136
+ require .Equal (t , w .duplicate , 0 )
137
+ })
138
+ }
139
+ }
0 commit comments