@@ -163,6 +163,7 @@ namespace ts {
163163 let currentText : string ;
164164 let currentParent : Node ;
165165 let currentNode : Node ;
166+ let enclosingVariableStatement : VariableStatement ;
166167 let enclosingBlockScopeContainer : Node ;
167168 let enclosingBlockScopeContainerParent : Node ;
168169 let enclosingFunction : FunctionLikeDeclaration ;
@@ -205,6 +206,7 @@ namespace ts {
205206 const savedEnclosingNonAsyncFunctionBody = enclosingNonAsyncFunctionBody ;
206207 const savedEnclosingBlockScopeContainer = enclosingBlockScopeContainer ;
207208 const savedEnclosingBlockScopeContainerParent = enclosingBlockScopeContainerParent ;
209+ const savedEnclosingVariableStatement = enclosingVariableStatement ;
208210 const savedCurrentParent = currentParent ;
209211 const savedCurrentNode = currentNode ;
210212 const savedConvertedLoopState = convertedLoopState ;
@@ -222,6 +224,7 @@ namespace ts {
222224 enclosingNonAsyncFunctionBody = savedEnclosingNonAsyncFunctionBody ;
223225 enclosingBlockScopeContainer = savedEnclosingBlockScopeContainer ;
224226 enclosingBlockScopeContainerParent = savedEnclosingBlockScopeContainerParent ;
227+ enclosingVariableStatement = savedEnclosingVariableStatement ;
225228 currentParent = savedCurrentParent ;
226229 currentNode = savedCurrentNode ;
227230 return visited ;
@@ -306,7 +309,7 @@ namespace ts {
306309 return visitFunctionExpression ( < FunctionExpression > node ) ;
307310
308311 case SyntaxKind . VariableDeclaration :
309- return visitVariableDeclaration ( < VariableDeclaration > node , /*offset*/ undefined ) ;
312+ return visitVariableDeclaration ( < VariableDeclaration > node ) ;
310313
311314 case SyntaxKind . Identifier :
312315 return visitIdentifier ( < Identifier > node ) ;
@@ -410,6 +413,25 @@ namespace ts {
410413 }
411414 }
412415 }
416+
417+ // keep track of the enclosing variable statement when in the context of
418+ // variable statements, variable declarations, binding elements, and binding
419+ // patterns.
420+ switch ( currentNode . kind ) {
421+ case SyntaxKind . VariableStatement :
422+ enclosingVariableStatement = < VariableStatement > currentNode ;
423+ break ;
424+
425+ case SyntaxKind . VariableDeclarationList :
426+ case SyntaxKind . VariableDeclaration :
427+ case SyntaxKind . BindingElement :
428+ case SyntaxKind . ObjectBindingPattern :
429+ case SyntaxKind . ArrayBindingPattern :
430+ break ;
431+
432+ default :
433+ enclosingVariableStatement = undefined ;
434+ }
413435 }
414436
415437 currentParent = currentNode ;
@@ -1309,7 +1331,7 @@ namespace ts {
13091331 return setOriginalNode (
13101332 createFunctionDeclaration (
13111333 /*decorators*/ undefined ,
1312- /* modifiers*/ undefined ,
1334+ node . modifiers ,
13131335 node . asteriskToken ,
13141336 node . name ,
13151337 /*typeParameters*/ undefined ,
@@ -1638,13 +1660,13 @@ namespace ts {
16381660 *
16391661 * @param node A VariableDeclaration node.
16401662 */
1641- function visitVariableDeclarationInLetDeclarationList ( node : VariableDeclaration , offset : number ) {
1663+ function visitVariableDeclarationInLetDeclarationList ( node : VariableDeclaration ) {
16421664 // For binding pattern names that lack initializers there is no point to emit
16431665 // explicit initializer since downlevel codegen for destructuring will fail
16441666 // in the absence of initializer so all binding elements will say uninitialized
16451667 const name = node . name ;
16461668 if ( isBindingPattern ( name ) ) {
1647- return visitVariableDeclaration ( node , offset ) ;
1669+ return visitVariableDeclaration ( node ) ;
16481670 }
16491671
16501672 if ( ! node . initializer && shouldEmitExplicitInitializerForLetDeclaration ( node ) ) {
@@ -1661,10 +1683,13 @@ namespace ts {
16611683 *
16621684 * @param node A VariableDeclaration node.
16631685 */
1664- function visitVariableDeclaration ( node : VariableDeclaration , offset : number ) : VisitResult < VariableDeclaration > {
1686+ function visitVariableDeclaration ( node : VariableDeclaration ) : VisitResult < VariableDeclaration > {
16651687 // If we are here it is because the name contains a binding pattern.
16661688 if ( isBindingPattern ( node . name ) ) {
1667- return flattenVariableDestructuring ( context , node , /*value*/ undefined , visitor ) ;
1689+ const recordTempVariablesInLine = ! enclosingVariableStatement
1690+ || ! hasModifier ( enclosingVariableStatement , ModifierFlags . Export ) ;
1691+ return flattenVariableDestructuring ( context , node , /*value*/ undefined , visitor ,
1692+ recordTempVariablesInLine ? undefined : hoistVariableDeclaration ) ;
16681693 }
16691694
16701695 return visitEachChild ( node , visitor , context ) ;
@@ -1740,7 +1765,7 @@ namespace ts {
17401765 // Note also that because an extra statement is needed to assign to the LHS,
17411766 // for-of bodies are always emitted as blocks.
17421767
1743- const expression = node . expression ;
1768+ const expression = visitNode ( node . expression , visitor , isExpression ) ;
17441769 const initializer = node . initializer ;
17451770 const statements : Statement [ ] = [ ] ;
17461771
@@ -1989,7 +2014,7 @@ namespace ts {
19892014 case SyntaxKind . ForOfStatement :
19902015 const initializer = ( < ForStatement | ForInStatement | ForOfStatement > node ) . initializer ;
19912016 if ( initializer && initializer . kind === SyntaxKind . VariableDeclarationList ) {
1992- loopInitializer = < VariableDeclarationList > ( < ForStatement | ForInStatement | ForOfStatement > node ) . initializer ;
2017+ loopInitializer = < VariableDeclarationList > initializer ;
19932018 }
19942019 break ;
19952020 }
0 commit comments