@@ -280,14 +280,11 @@ namespace ts {
280280 let currentText : string ;
281281 let hierarchyFacts : HierarchyFacts ;
282282 let taggedTemplateStringDeclarations : VariableDeclaration [ ] ;
283+
283284 function recordTaggedTemplateString ( temp : Identifier ) {
284- const decl = createVariableDeclaration ( temp ) ;
285- if ( ! taggedTemplateStringDeclarations ) {
286- taggedTemplateStringDeclarations = [ decl ] ;
287- }
288- else {
289- taggedTemplateStringDeclarations . push ( decl ) ;
290- }
285+ taggedTemplateStringDeclarations = append (
286+ taggedTemplateStringDeclarations ,
287+ createVariableDeclaration ( temp ) ) ; t
291288 }
292289
293290 /**
@@ -3652,11 +3649,6 @@ namespace ts {
36523649 // Visit the tag expression
36533650 const tag = visitNode ( node . tag , visitor , isExpression ) ;
36543651
3655- // Allocate storage for the template site object if we're in a module.
3656- // In the global scope, any variable we currently generate could conflict with
3657- // variables from outside of the current compilation.
3658- const temp = isExternalModule ( currentSourceFile ) ? createTempVariable ( recordTaggedTemplateString ) : undefined ;
3659-
36603652 // Build up the template arguments and the raw and cooked strings for the template.
36613653 // We start out with 'undefined' for the first argument and revisit later
36623654 // to avoid walking over the template string twice and shifting all our arguments over after the fact.
@@ -3680,17 +3672,21 @@ namespace ts {
36803672
36813673 const helperCall = createTemplateObjectHelper ( context , createArrayLiteral ( cookedStrings ) , createArrayLiteral ( rawStrings ) ) ;
36823674
3683- // If we're in the global scope, we risk having conflicting variables .
3684- // Since we currently lack the infrastructure to create sufficiently unique names,
3685- // we'll fall back to creating the template object on every invocation .
3686- templateArguments [ 0 ] = ! temp ?
3687- helperCall :
3688- createLogicalOr (
3689- temp ,
3675+ // Create a variable to cache the template object if we're in a module .
3676+ // Do not do this in the global scope, as any variable we currently generate could conflict with
3677+ // variables from outside of the current compilation. In the future, we can revisit this behavior .
3678+ if ( isExternalModule ( currentSourceFile ) ) {
3679+ const tempVar = createTempVariable ( recordTaggedTemplateString ) ;
3680+ templateArguments [ 0 ] = createLogicalOr (
3681+ tempVar ,
36903682 createAssignment (
3691- temp ,
3683+ tempVar ,
36923684 helperCall )
36933685 ) ;
3686+ }
3687+ else {
3688+ templateArguments [ 0 ] = helperCall ;
3689+ }
36943690
36953691 return createCall ( tag , /*typeArguments*/ undefined , templateArguments ) ;
36963692 }
0 commit comments