@@ -280,14 +280,11 @@ namespace ts {
280
280
let currentText : string ;
281
281
let hierarchyFacts : HierarchyFacts ;
282
282
let taggedTemplateStringDeclarations : VariableDeclaration [ ] ;
283
+
283
284
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
291
288
}
292
289
293
290
/**
@@ -3652,11 +3649,6 @@ namespace ts {
3652
3649
// Visit the tag expression
3653
3650
const tag = visitNode ( node . tag , visitor , isExpression ) ;
3654
3651
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
-
3660
3652
// Build up the template arguments and the raw and cooked strings for the template.
3661
3653
// We start out with 'undefined' for the first argument and revisit later
3662
3654
// to avoid walking over the template string twice and shifting all our arguments over after the fact.
@@ -3680,17 +3672,21 @@ namespace ts {
3680
3672
3681
3673
const helperCall = createTemplateObjectHelper ( context , createArrayLiteral ( cookedStrings ) , createArrayLiteral ( rawStrings ) ) ;
3682
3674
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 ,
3690
3682
createAssignment (
3691
- temp ,
3683
+ tempVar ,
3692
3684
helperCall )
3693
3685
) ;
3686
+ }
3687
+ else {
3688
+ templateArguments [ 0 ] = helperCall ;
3689
+ }
3694
3690
3695
3691
return createCall ( tag , /*typeArguments*/ undefined , templateArguments ) ;
3696
3692
}
0 commit comments