@@ -2954,9 +2954,11 @@ namespace ts {
2954
2954
// variables declared in the loop initializer that will be changed inside the loop
2955
2955
const loopOutParameters : LoopOutParameter [ ] = [ ] ;
2956
2956
if ( loopInitializer && ( getCombinedNodeFlags ( loopInitializer ) & NodeFlags . BlockScoped ) ) {
2957
- const hasCapturedBindingsInForInitializer = shouldConvertInitializerOfForStatement ( node ) ;
2957
+ const hasCapturedBindingsInForHead = shouldConvertInitializerOfForStatement ( node ) ||
2958
+ shouldConvertConditionOfForStatement ( node ) ||
2959
+ shouldConvertIncrementorOfForStatement ( node ) ;
2958
2960
for ( const decl of loopInitializer . declarations ) {
2959
- processLoopVariableDeclaration ( node , decl , loopParameters , loopOutParameters , hasCapturedBindingsInForInitializer ) ;
2961
+ processLoopVariableDeclaration ( node , decl , loopParameters , loopOutParameters , hasCapturedBindingsInForHead ) ;
2960
2962
}
2961
2963
}
2962
2964
@@ -3434,26 +3436,32 @@ namespace ts {
3434
3436
} ) ;
3435
3437
}
3436
3438
3437
- function processLoopVariableDeclaration ( container : IterationStatement , decl : VariableDeclaration | BindingElement , loopParameters : ParameterDeclaration [ ] , loopOutParameters : LoopOutParameter [ ] , hasCapturedBindingsInForInitializer : boolean ) {
3439
+ function processLoopVariableDeclaration ( container : IterationStatement , decl : VariableDeclaration | BindingElement , loopParameters : ParameterDeclaration [ ] , loopOutParameters : LoopOutParameter [ ] , hasCapturedBindingsInForHead : boolean ) {
3438
3440
const name = decl . name ;
3439
3441
if ( isBindingPattern ( name ) ) {
3440
3442
for ( const element of name . elements ) {
3441
3443
if ( ! isOmittedExpression ( element ) ) {
3442
- processLoopVariableDeclaration ( container , element , loopParameters , loopOutParameters , hasCapturedBindingsInForInitializer ) ;
3444
+ processLoopVariableDeclaration ( container , element , loopParameters , loopOutParameters , hasCapturedBindingsInForHead ) ;
3443
3445
}
3444
3446
}
3445
3447
}
3446
3448
else {
3447
3449
loopParameters . push ( factory . createParameterDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , /*dotDotDotToken*/ undefined , name ) ) ;
3448
3450
const checkFlags = resolver . getNodeCheckFlags ( decl ) ;
3449
- if ( checkFlags & NodeCheckFlags . NeedsLoopOutParameter || hasCapturedBindingsInForInitializer ) {
3451
+ if ( checkFlags & NodeCheckFlags . NeedsLoopOutParameter || hasCapturedBindingsInForHead ) {
3450
3452
const outParamName = factory . createUniqueName ( "out_" + idText ( name ) ) ;
3451
3453
let flags : LoopOutParameterFlags = 0 ;
3452
3454
if ( checkFlags & NodeCheckFlags . NeedsLoopOutParameter ) {
3453
3455
flags |= LoopOutParameterFlags . Body ;
3454
3456
}
3455
- if ( isForStatement ( container ) && container . initializer && resolver . isBindingCapturedByNode ( container . initializer , decl ) ) {
3456
- flags |= LoopOutParameterFlags . Initializer ;
3457
+ if ( isForStatement ( container ) ) {
3458
+ if ( container . initializer && resolver . isBindingCapturedByNode ( container . initializer , decl ) ) {
3459
+ flags |= LoopOutParameterFlags . Initializer ;
3460
+ }
3461
+ if ( container . condition && resolver . isBindingCapturedByNode ( container . condition , decl ) ||
3462
+ container . incrementor && resolver . isBindingCapturedByNode ( container . incrementor , decl ) ) {
3463
+ flags |= LoopOutParameterFlags . Body ;
3464
+ }
3457
3465
}
3458
3466
loopOutParameters . push ( { flags, originalName : name , outParamName } ) ;
3459
3467
}
0 commit comments