@@ -3652,8 +3652,10 @@ namespace ts {
3652
3652
// Visit the tag expression
3653
3653
const tag = visitNode ( node . tag , visitor , isExpression ) ;
3654
3654
3655
- // Allocate storage for the template site object
3656
- const temp = createTempVariable ( recordTaggedTemplateString ) ;
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 ;
3657
3659
3658
3660
// Build up the template arguments and the raw and cooked strings for the template.
3659
3661
// We start out with 'undefined' for the first argument and revisit later
@@ -3676,12 +3678,19 @@ namespace ts {
3676
3678
}
3677
3679
}
3678
3680
3679
- // Initialize the template object if necessary
3680
- templateArguments [ 0 ] = createLogicalOr (
3681
- temp ,
3682
- createAssignment (
3681
+ const helperCall = createTemplateObjectHelper ( context , createArrayLiteral ( cookedStrings ) , createArrayLiteral ( rawStrings ) ) ;
3682
+
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 (
3683
3689
temp ,
3684
- createTemplateObjectHelper ( context , createArrayLiteral ( cookedStrings ) , createArrayLiteral ( rawStrings ) ) ) ) ;
3690
+ createAssignment (
3691
+ temp ,
3692
+ helperCall )
3693
+ ) ;
3685
3694
3686
3695
return createCall ( tag , /*typeArguments*/ undefined , templateArguments ) ;
3687
3696
}
0 commit comments