@@ -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 containingNonArrowFunction : FunctionLikeDeclaration | ClassElement ;
@@ -210,6 +211,7 @@ namespace ts {
210211 const savedSuperScopeContainer = superScopeContainer ;
211212 const savedCurrentParent = currentParent ;
212213 const savedCurrentNode = currentNode ;
214+ const savedEnclosingVariableStatement = enclosingVariableStatement ;
213215 const savedEnclosingBlockScopeContainer = enclosingBlockScopeContainer ;
214216 const savedEnclosingBlockScopeContainerParent = enclosingBlockScopeContainerParent ;
215217
@@ -227,6 +229,7 @@ namespace ts {
227229 superScopeContainer = savedSuperScopeContainer ;
228230 currentParent = savedCurrentParent ;
229231 currentNode = savedCurrentNode ;
232+ enclosingVariableStatement = savedEnclosingVariableStatement ;
230233 enclosingBlockScopeContainer = savedEnclosingBlockScopeContainer ;
231234 enclosingBlockScopeContainerParent = savedEnclosingBlockScopeContainerParent ;
232235 return visited ;
@@ -320,7 +323,7 @@ namespace ts {
320323 return visitFunctionExpression ( < FunctionExpression > node ) ;
321324
322325 case SyntaxKind . VariableDeclaration :
323- return visitVariableDeclaration ( < VariableDeclaration > node , /*offset*/ undefined ) ;
326+ return visitVariableDeclaration ( < VariableDeclaration > node ) ;
324327
325328 case SyntaxKind . Identifier :
326329 return visitIdentifier ( < Identifier > node ) ;
@@ -432,6 +435,25 @@ namespace ts {
432435 }
433436 break ;
434437 }
438+
439+ // keep track of the enclosing variable statement when in the context of
440+ // variable statements, variable declarations, binding elements, and binding
441+ // patterns.
442+ switch ( currentParent . kind ) {
443+ case SyntaxKind . VariableStatement :
444+ enclosingVariableStatement = < VariableStatement > currentParent ;
445+ break ;
446+
447+ case SyntaxKind . VariableDeclarationList :
448+ case SyntaxKind . VariableDeclaration :
449+ case SyntaxKind . BindingElement :
450+ case SyntaxKind . ObjectBindingPattern :
451+ case SyntaxKind . ArrayBindingPattern :
452+ break ;
453+
454+ default :
455+ enclosingVariableStatement = undefined ;
456+ }
435457 }
436458 }
437459
@@ -1334,7 +1356,7 @@ namespace ts {
13341356 return setOriginalNode (
13351357 createFunctionDeclaration (
13361358 /*decorators*/ undefined ,
1337- /* modifiers*/ undefined ,
1359+ node . modifiers ,
13381360 node . asteriskToken ,
13391361 node . name ,
13401362 /*typeParameters*/ undefined ,
@@ -1663,13 +1685,13 @@ namespace ts {
16631685 *
16641686 * @param node A VariableDeclaration node.
16651687 */
1666- function visitVariableDeclarationInLetDeclarationList ( node : VariableDeclaration , offset : number ) {
1688+ function visitVariableDeclarationInLetDeclarationList ( node : VariableDeclaration ) {
16671689 // For binding pattern names that lack initializers there is no point to emit
16681690 // explicit initializer since downlevel codegen for destructuring will fail
16691691 // in the absence of initializer so all binding elements will say uninitialized
16701692 const name = node . name ;
16711693 if ( isBindingPattern ( name ) ) {
1672- return visitVariableDeclaration ( node , offset ) ;
1694+ return visitVariableDeclaration ( node ) ;
16731695 }
16741696
16751697 if ( ! node . initializer && shouldEmitExplicitInitializerForLetDeclaration ( node ) ) {
@@ -1686,10 +1708,13 @@ namespace ts {
16861708 *
16871709 * @param node A VariableDeclaration node.
16881710 */
1689- function visitVariableDeclaration ( node : VariableDeclaration , offset : number ) : VisitResult < VariableDeclaration > {
1711+ function visitVariableDeclaration ( node : VariableDeclaration ) : VisitResult < VariableDeclaration > {
16901712 // If we are here it is because the name contains a binding pattern.
16911713 if ( isBindingPattern ( node . name ) ) {
1692- return flattenVariableDestructuring ( context , node , /*value*/ undefined , visitor ) ;
1714+ const recordTempVariablesInLine = ! enclosingVariableStatement
1715+ || ! hasModifier ( enclosingVariableStatement , ModifierFlags . Export ) ;
1716+ return flattenVariableDestructuring ( context , node , /*value*/ undefined , visitor ,
1717+ recordTempVariablesInLine ? undefined : hoistVariableDeclaration ) ;
16931718 }
16941719
16951720 return visitEachChild ( node , visitor , context ) ;
0 commit comments