@@ -145,35 +145,6 @@ function restoreInnerScopes(node, map) {
145
145
} ) ;
146
146
}
147
147
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
-
177
148
function hasSideEffects ( node ) {
178
149
// Conservative analysis.
179
150
const map = ignoreInnerScopes ( node ) ;
@@ -272,8 +243,24 @@ function JSDCE(ast, aggressive) {
272
243
}
273
244
function cleanUp ( ast , names ) {
274
245
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
+ } ,
275
263
VariableDeclaration ( node , _c ) {
276
- const old = node . declarations ;
277
264
let removedHere = 0 ;
278
265
node . declarations = node . declarations . filter ( ( node ) => {
279
266
assert ( node . type === 'VariableDeclarator' ) ;
@@ -294,8 +281,6 @@ function JSDCE(ast, aggressive) {
294
281
removed += removedHere ;
295
282
if ( node . declarations . length === 0 ) {
296
283
emptyOut ( node ) ;
297
- // If this is in a for, we may need to restore it.
298
- node . oldDeclarations = old ;
299
284
}
300
285
} ,
301
286
ExpressionStatement ( node , _c ) {
@@ -316,7 +301,6 @@ function JSDCE(ast, aggressive) {
316
301
FunctionExpression ( ) { } ,
317
302
ArrowFunctionExpression ( ) { } ,
318
303
} ) ;
319
- removed -= restoreForVars ( ast ) ;
320
304
}
321
305
322
306
function handleFunction ( node , c , defun ) {
0 commit comments