@@ -53,7 +53,11 @@ public Node visit(BlockStatement s, T t) {
53
53
if (node != statement ) {
54
54
changed = true ;
55
55
}
56
- result .add ((Statement ) node );
56
+ if (node instanceof Statement ) {
57
+ result .add ((Statement ) node );
58
+ } else if (node instanceof Expression ) {
59
+ result .add (new ExprStatement ((Expression ) node ));
60
+ }
57
61
}
58
62
if (changed ) {
59
63
return result ;
@@ -103,7 +107,7 @@ public Node visit(DoWhileStatement s, T t) {
103
107
final Node condition = s .condition .accept (this , t );
104
108
final Node statement = s .statement .accept (this , t );
105
109
if (condition != s .condition || statement != s .statement ) {
106
- return new DoWhileStatement ((Expression ) condition , ( Statement ) statement );
110
+ return new DoWhileStatement ((Expression ) condition , consumeStatement ( statement ) );
107
111
}
108
112
return s ;
109
113
}
@@ -125,8 +129,8 @@ public Node visit(ForStatement s, T t) {
125
129
final Node statement = s .statement .accept (this , t );
126
130
if (initialization != s .initialization || termination != s .termination
127
131
|| increment != s .increment || statement != s .statement ) {
128
- return new ForStatement (( Statement ) initialization ,
129
- (Expression ) termination , ( Statement ) increment , ( Statement ) statement );
132
+ return new ForStatement (consumeStatement ( initialization ) ,
133
+ (Expression ) termination , consumeStatement ( increment ), consumeStatement ( statement ) );
130
134
}
131
135
return s ;
132
136
}
@@ -136,7 +140,7 @@ public Node visit(ForeachArrayStatement s, T t) {
136
140
final Node container = s .container .accept (this , t );
137
141
final Node body = s .body .accept (this , t );
138
142
if (container != s .container || body != s .body ) {
139
- return new ForeachArrayStatement (s .variable , (Expression ) container , ( Statement ) body );
143
+ return new ForeachArrayStatement (s .variable , (Expression ) container , consumeStatement ( body ) );
140
144
}
141
145
return s ;
142
146
}
@@ -146,7 +150,7 @@ public Node visit(ForeachMapStatement s, T t) {
146
150
final Node container = s .container .accept (this , t );
147
151
final Node body = s .body .accept (this , t );
148
152
if (container != s .container || body != s .body ) {
149
- return new ForeachMapStatement (s .key , s .value , (Expression ) container , ( Statement ) body );
153
+ return new ForeachMapStatement (s .key , s .value , (Expression ) container , consumeStatement ( body ) );
150
154
}
151
155
return s ;
152
156
}
@@ -155,7 +159,7 @@ public Node visit(ForeachMapStatement s, T t) {
155
159
public Node visit (FunctionDefineStatement s , T t ) {
156
160
final Node body = s .body .accept (this , t );
157
161
if (body != s .body ) {
158
- return new FunctionDefineStatement (s .name , s .arguments , ( Statement ) body );
162
+ return new FunctionDefineStatement (s .name , s .arguments , consumeStatement ( body ) );
159
163
}
160
164
return s ;
161
165
}
@@ -203,7 +207,7 @@ public Node visit(IfStatement s, T t) {
203
207
elseStatement = null ;
204
208
}
205
209
if (expression != s .expression || ifStatement != s .ifStatement || elseStatement != s .elseStatement ) {
206
- return new IfStatement ((Expression ) expression , ( Statement ) ifStatement , ( Statement ) elseStatement );
210
+ return new IfStatement ((Expression ) expression , consumeStatement ( ifStatement ), consumeStatement ( elseStatement ) );
207
211
}
208
212
return s ;
209
213
}
@@ -244,7 +248,7 @@ public Node visit(MatchExpression s, T t) {
244
248
final Node patternResult = pattern .result .accept (this , t );
245
249
if (patternResult != pattern .result ) {
246
250
changed = true ;
247
- pattern .result = ( Statement ) patternResult ;
251
+ pattern .result = consumeStatement ( patternResult ) ;
248
252
}
249
253
patterns .add (pattern );
250
254
}
@@ -316,7 +320,7 @@ public Node visit(WhileStatement s, T t) {
316
320
final Node condition = s .condition .accept (this , t );
317
321
final Node statement = s .statement .accept (this , t );
318
322
if (condition != s .condition || statement != s .statement ) {
319
- return new WhileStatement ((Expression ) condition , ( Statement ) statement );
323
+ return new WhileStatement ((Expression ) condition , consumeStatement ( statement ) );
320
324
}
321
325
return s ;
322
326
}
@@ -329,4 +333,11 @@ public Node visit(UseStatement s, T t) {
329
333
}
330
334
return s ;
331
335
}
336
+
337
+ protected Statement consumeStatement (Node node ) {
338
+ if (node instanceof Statement ) {
339
+ return (Statement ) node ;
340
+ }
341
+ return new ExprStatement ((Expression ) node );
342
+ }
332
343
}
0 commit comments