From 4ec1643ecc56dcf95c7dcc11d7a35c5ebc861dea Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 27 Sep 2017 18:14:45 -0700 Subject: [PATCH] Fall back to old behavior for tagged template emit in global files. --- src/compiler/transformers/es2015.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) mode change 100644 => 100755 src/compiler/transformers/es2015.ts diff --git a/src/compiler/transformers/es2015.ts b/src/compiler/transformers/es2015.ts old mode 100644 new mode 100755 index 9523c614399..00610c6552c --- a/src/compiler/transformers/es2015.ts +++ b/src/compiler/transformers/es2015.ts @@ -3652,8 +3652,10 @@ namespace ts { // Visit the tag expression const tag = visitNode(node.tag, visitor, isExpression); - // Allocate storage for the template site object - const temp = createTempVariable(recordTaggedTemplateString); + // Allocate storage for the template site object if we're in a module. + // In the global scope, any variable we currently generate could conflict with + // variables from outside of the current compilation. + const temp = isExternalModule(currentSourceFile) ? createTempVariable(recordTaggedTemplateString) : undefined; // Build up the template arguments and the raw and cooked strings for the template. // We start out with 'undefined' for the first argument and revisit later @@ -3676,12 +3678,19 @@ namespace ts { } } - // Initialize the template object if necessary - templateArguments[0] = createLogicalOr( - temp, - createAssignment( + const helperCall = createTemplateObjectHelper(context, createArrayLiteral(cookedStrings), createArrayLiteral(rawStrings)); + + // If we're in the global scope, we risk having conflicting variables. + // Since we currently lack the infrastructure to create sufficiently unique names, + // we'll fall back to creating the template object on every invocation. + templateArguments[0] = !temp ? + helperCall : + createLogicalOr( temp, - createTemplateObjectHelper(context, createArrayLiteral(cookedStrings), createArrayLiteral(rawStrings)))); + createAssignment( + temp, + helperCall) + ); return createCall(tag, /*typeArguments*/ undefined, templateArguments); }