From 35c815ef15a4ff031d500d1efeb39d2d902d7776 Mon Sep 17 00:00:00 2001 From: Ivo Gabe de Wolff Date: Sun, 22 Feb 2015 10:07:32 +0100 Subject: [PATCH] Respond to code review --- src/compiler/emitter.ts | 53 +++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 05969877cf1..0eb3dbd0e54 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2140,6 +2140,9 @@ module ts { } function emitDownlevelRawTemplateLiteral(node: LiteralExpression) { + // Find original source text, since we need to emit the raw strings of the tagged template. + // The raw strings contain the (escaped) strings of what the user wrote. + // Examples: `\n` is converted to "\\n", a template string with a newline to "\n". var text = getSourceTextOfNodeFromSourceFile(currentSourceFile, node); // text contains the original source, it will also contain quotes ("`"), dolar signs and braces ("${" and "}"), @@ -2149,45 +2152,43 @@ module ts { var isLast = node.kind === SyntaxKind.NoSubstitutionTemplateLiteral || node.kind === SyntaxKind.TemplateTail; text = text.substring(1, text.length - (isLast ? 1 : 2)); - // Newline normalization + // Newline normalization: + // ES6 Spec 11.8.6.1 - Static Semantics of TV's and TRV's + // and LineTerminatorSequences are normalized to for both TV and TRV. text = text.replace(/\r\n?/g, "\n"); text = escapeString(text); write('"' + text + '"'); } + function emitDownlevelTaggedTemplateArray(node: TaggedTemplateExpression, callback: (literal: LiteralExpression) => void) { + write("["); + if (node.template.kind === SyntaxKind.NoSubstitutionTemplateLiteral) { + callback(node.template); + } + else { + callback((node.template).head); + forEach((node.template).templateSpans, (child) => { + write(", "); + callback(child.literal); + }); + } + write("]"); + } + function emitDownlevelTaggedTemplate(node: TaggedTemplateExpression) { var tempVariable = createTempVariable(node); recordTempDeclaration(tempVariable); write("("); emit(tempVariable); - write(" = ["); - - if (node.template.kind === SyntaxKind.NoSubstitutionTemplateLiteral) { - emit(node.template); - } - else { - emit((node.template).head); - forEach((node.template).templateSpans, (child) => { - write(", "); - emit(child.literal); - }); - } - write("], "); + write(" = "); + emitDownlevelTaggedTemplateArray(node, emit); + write(", "); emit(tempVariable); - write(".raw = ["); - if (node.template.kind === SyntaxKind.NoSubstitutionTemplateLiteral) { - emitDownlevelRawTemplateLiteral(node.template); - } - else { - emitDownlevelRawTemplateLiteral((node.template).head); - forEach((node.template).templateSpans, (child, index) => { - write(", "); - emitDownlevelRawTemplateLiteral(child.literal); - }); - } - write("], "); + write(".raw = "); + emitDownlevelTaggedTemplateArray(node, emitDownlevelRawTemplateLiteral); + write(", "); emitParenthesizedIf(node.tag, needsParenthesisForPropertyAccessOrInvocation(node.tag)); write("(");