@@ -126,14 +126,22 @@ namespace ts {
126126 context : TransformationContext ,
127127 node : VariableDeclaration ,
128128 value ?: Expression ,
129- visitor ?: ( node : Node ) => VisitResult < Node > ) {
129+ visitor ?: ( node : Node ) => VisitResult < Node > ,
130+ recordTempVariable ?: ( node : Identifier ) => void ) {
130131 const declarations : VariableDeclaration [ ] = [ ] ;
131132
133+ let pendingAssignments : Expression [ ] ;
132134 flattenDestructuring ( context , node , value , node , emitAssignment , emitTempVariableAssignment , visitor ) ;
133135
134136 return declarations ;
135137
136138 function emitAssignment ( name : Identifier , value : Expression , location : TextRange , original : Node ) {
139+ if ( pendingAssignments ) {
140+ pendingAssignments . push ( value ) ;
141+ value = inlineExpressions ( pendingAssignments ) ;
142+ pendingAssignments = undefined ;
143+ }
144+
137145 const declaration = createVariableDeclaration ( name , /*type*/ undefined , value , location ) ;
138146 declaration . original = original ;
139147
@@ -146,8 +154,19 @@ namespace ts {
146154 }
147155
148156 function emitTempVariableAssignment ( value : Expression , location : TextRange ) {
149- const name = createTempVariable ( /*recordTempVariable*/ undefined ) ;
150- emitAssignment ( name , value , location , /*original*/ undefined ) ;
157+ const name = createTempVariable ( recordTempVariable ) ;
158+ if ( recordTempVariable ) {
159+ const assignment = createAssignment ( name , value , location ) ;
160+ if ( pendingAssignments ) {
161+ pendingAssignments . push ( assignment ) ;
162+ }
163+ else {
164+ pendingAssignments = [ assignment ] ;
165+ }
166+ }
167+ else {
168+ emitAssignment ( name , value , location , /*original*/ undefined ) ;
169+ }
151170 return name ;
152171 }
153172 }
0 commit comments