@@ -2954,9 +2954,11 @@ namespace ts {
29542954 // variables declared in the loop initializer that will be changed inside the loop
29552955 const loopOutParameters : LoopOutParameter [ ] = [ ] ;
29562956 if ( loopInitializer && ( getCombinedNodeFlags ( loopInitializer ) & NodeFlags . BlockScoped ) ) {
2957- const hasCapturedBindingsInForInitializer = shouldConvertInitializerOfForStatement ( node ) ;
2957+ const hasCapturedBindingsInForHead = shouldConvertInitializerOfForStatement ( node ) ||
2958+ shouldConvertConditionOfForStatement ( node ) ||
2959+ shouldConvertIncrementorOfForStatement ( node ) ;
29582960 for ( const decl of loopInitializer . declarations ) {
2959- processLoopVariableDeclaration ( node , decl , loopParameters , loopOutParameters , hasCapturedBindingsInForInitializer ) ;
2961+ processLoopVariableDeclaration ( node , decl , loopParameters , loopOutParameters , hasCapturedBindingsInForHead ) ;
29602962 }
29612963 }
29622964
@@ -3434,26 +3436,32 @@ namespace ts {
34343436 } ) ;
34353437 }
34363438
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 ) {
34383440 const name = decl . name ;
34393441 if ( isBindingPattern ( name ) ) {
34403442 for ( const element of name . elements ) {
34413443 if ( ! isOmittedExpression ( element ) ) {
3442- processLoopVariableDeclaration ( container , element , loopParameters , loopOutParameters , hasCapturedBindingsInForInitializer ) ;
3444+ processLoopVariableDeclaration ( container , element , loopParameters , loopOutParameters , hasCapturedBindingsInForHead ) ;
34433445 }
34443446 }
34453447 }
34463448 else {
34473449 loopParameters . push ( factory . createParameterDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , /*dotDotDotToken*/ undefined , name ) ) ;
34483450 const checkFlags = resolver . getNodeCheckFlags ( decl ) ;
3449- if ( checkFlags & NodeCheckFlags . NeedsLoopOutParameter || hasCapturedBindingsInForInitializer ) {
3451+ if ( checkFlags & NodeCheckFlags . NeedsLoopOutParameter || hasCapturedBindingsInForHead ) {
34503452 const outParamName = factory . createUniqueName ( "out_" + idText ( name ) ) ;
34513453 let flags : LoopOutParameterFlags = 0 ;
34523454 if ( checkFlags & NodeCheckFlags . NeedsLoopOutParameter ) {
34533455 flags |= LoopOutParameterFlags . Body ;
34543456 }
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+ }
34573465 }
34583466 loopOutParameters . push ( { flags, originalName : name , outParamName } ) ;
34593467 }
0 commit comments