@@ -53,7 +53,11 @@ public Node visit(BlockStatement s, T t) {
5353 if (node != statement ) {
5454 changed = true ;
5555 }
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+ }
5761 }
5862 if (changed ) {
5963 return result ;
@@ -103,7 +107,7 @@ public Node visit(DoWhileStatement s, T t) {
103107 final Node condition = s .condition .accept (this , t );
104108 final Node statement = s .statement .accept (this , t );
105109 if (condition != s .condition || statement != s .statement ) {
106- return new DoWhileStatement ((Expression ) condition , ( Statement ) statement );
110+ return new DoWhileStatement ((Expression ) condition , consumeStatement ( statement ) );
107111 }
108112 return s ;
109113 }
@@ -125,8 +129,8 @@ public Node visit(ForStatement s, T t) {
125129 final Node statement = s .statement .accept (this , t );
126130 if (initialization != s .initialization || termination != s .termination
127131 || 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 ) );
130134 }
131135 return s ;
132136 }
@@ -136,7 +140,7 @@ public Node visit(ForeachArrayStatement s, T t) {
136140 final Node container = s .container .accept (this , t );
137141 final Node body = s .body .accept (this , t );
138142 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 ) );
140144 }
141145 return s ;
142146 }
@@ -146,7 +150,7 @@ public Node visit(ForeachMapStatement s, T t) {
146150 final Node container = s .container .accept (this , t );
147151 final Node body = s .body .accept (this , t );
148152 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 ) );
150154 }
151155 return s ;
152156 }
@@ -155,7 +159,7 @@ public Node visit(ForeachMapStatement s, T t) {
155159 public Node visit (FunctionDefineStatement s , T t ) {
156160 final Node body = s .body .accept (this , t );
157161 if (body != s .body ) {
158- return new FunctionDefineStatement (s .name , s .arguments , ( Statement ) body );
162+ return new FunctionDefineStatement (s .name , s .arguments , consumeStatement ( body ) );
159163 }
160164 return s ;
161165 }
@@ -203,7 +207,7 @@ public Node visit(IfStatement s, T t) {
203207 elseStatement = null ;
204208 }
205209 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 ) );
207211 }
208212 return s ;
209213 }
@@ -244,7 +248,7 @@ public Node visit(MatchExpression s, T t) {
244248 final Node patternResult = pattern .result .accept (this , t );
245249 if (patternResult != pattern .result ) {
246250 changed = true ;
247- pattern .result = ( Statement ) patternResult ;
251+ pattern .result = consumeStatement ( patternResult ) ;
248252 }
249253 patterns .add (pattern );
250254 }
@@ -316,7 +320,7 @@ public Node visit(WhileStatement s, T t) {
316320 final Node condition = s .condition .accept (this , t );
317321 final Node statement = s .statement .accept (this , t );
318322 if (condition != s .condition || statement != s .statement ) {
319- return new WhileStatement ((Expression ) condition , ( Statement ) statement );
323+ return new WhileStatement ((Expression ) condition , consumeStatement ( statement ) );
320324 }
321325 return s ;
322326 }
@@ -329,4 +333,11 @@ public Node visit(UseStatement s, T t) {
329333 }
330334 return s ;
331335 }
336+
337+ protected Statement consumeStatement (Node node ) {
338+ if (node instanceof Statement ) {
339+ return (Statement ) node ;
340+ }
341+ return new ExprStatement ((Expression ) node );
342+ }
332343}
0 commit comments