@@ -3652,8 +3652,10 @@ namespace ts {
36523652 // Visit the tag expression
36533653 const tag = visitNode ( node . tag , visitor , isExpression ) ;
36543654
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 ;
36573659
36583660 // Build up the template arguments and the raw and cooked strings for the template.
36593661 // We start out with 'undefined' for the first argument and revisit later
@@ -3676,12 +3678,19 @@ namespace ts {
36763678 }
36773679 }
36783680
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 (
36833689 temp ,
3684- createTemplateObjectHelper ( context , createArrayLiteral ( cookedStrings ) , createArrayLiteral ( rawStrings ) ) ) ) ;
3690+ createAssignment (
3691+ temp ,
3692+ helperCall )
3693+ ) ;
36853694
36863695 return createCall ( tag , /*typeArguments*/ undefined , templateArguments ) ;
36873696 }
0 commit comments