@@ -145,35 +145,6 @@ function restoreInnerScopes(node, map) {
145145 } ) ;
146146}
147147
148- // If we empty out a var from
149- // for (var i in x) {}
150- // for (var j = 0;;) {}
151- // then it will be invalid. We saved it on the side;
152- // restore it here.
153- function restoreForVars ( node ) {
154- let restored = 0 ;
155- function fix ( init ) {
156- if ( init && init . type === 'EmptyStatement' ) {
157- assertAt ( init . oldDeclarations , init ) ;
158- init . type = 'VariableDeclaration' ;
159- init . declarations = init . oldDeclarations ;
160- restored ++ ;
161- }
162- }
163- simpleWalk ( node , {
164- ForStatement ( node ) {
165- fix ( node . init ) ;
166- } ,
167- ForInStatement ( node ) {
168- fix ( node . left ) ;
169- } ,
170- ForOfStatement ( node ) {
171- fix ( node . left ) ;
172- } ,
173- } ) ;
174- return restored ;
175- }
176-
177148function hasSideEffects ( node ) {
178149 // Conservative analysis.
179150 const map = ignoreInnerScopes ( node ) ;
@@ -272,8 +243,24 @@ function JSDCE(ast, aggressive) {
272243 }
273244 function cleanUp ( ast , names ) {
274245 recursiveWalk ( ast , {
246+ ForStatement ( node , c ) {
247+ visitChildren ( node , c ) ;
248+ // If we had `for (var x = ...; ...)` and we removed `x`, we need to change to `for (; ...)`.
249+ if ( node . init ?. type === 'EmptyStatement' ) {
250+ node . init = null ;
251+ }
252+ } ,
253+ ForInStatement ( node , c ) {
254+ // We can't remove the var in a for-in, as that would result in an invalid syntax. Skip the LHS.
255+ c ( node . right ) ;
256+ c ( node . body ) ;
257+ } ,
258+ ForOfStatement ( node , c ) {
259+ // We can't remove the var in a for-of, as that would result in an invalid syntax. Skip the LHS.
260+ c ( node . right ) ;
261+ c ( node . body ) ;
262+ } ,
275263 VariableDeclaration ( node , _c ) {
276- const old = node . declarations ;
277264 let removedHere = 0 ;
278265 node . declarations = node . declarations . filter ( ( node ) => {
279266 assert ( node . type === 'VariableDeclarator' ) ;
@@ -294,8 +281,6 @@ function JSDCE(ast, aggressive) {
294281 removed += removedHere ;
295282 if ( node . declarations . length === 0 ) {
296283 emptyOut ( node ) ;
297- // If this is in a for, we may need to restore it.
298- node . oldDeclarations = old ;
299284 }
300285 } ,
301286 ExpressionStatement ( node , _c ) {
@@ -316,7 +301,6 @@ function JSDCE(ast, aggressive) {
316301 FunctionExpression ( ) { } ,
317302 ArrowFunctionExpression ( ) { } ,
318303 } ) ;
319- removed -= restoreForVars ( ast ) ;
320304 }
321305
322306 function handleFunction ( node , c , defun ) {
0 commit comments