Respond to code review

This commit is contained in:
Ivo Gabe de Wolff
2015-02-22 10:07:32 +01:00
parent f8832598b9
commit 35c815ef15
+27 -26
View File
@@ -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
// <CR><LF> and <CR> LineTerminatorSequences are normalized to <LF> 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(<LiteralExpression>node.template);
}
else {
callback((<TemplateExpression>node.template).head);
forEach((<TemplateExpression>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((<TemplateExpression>node.template).head);
forEach((<TemplateExpression>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(<LiteralExpression>node.template);
}
else {
emitDownlevelRawTemplateLiteral((<TemplateExpression>node.template).head);
forEach((<TemplateExpression>node.template).templateSpans, (child, index) => {
write(", ");
emitDownlevelRawTemplateLiteral(child.literal);
});
}
write("], ");
write(".raw = ");
emitDownlevelTaggedTemplateArray(node, emitDownlevelRawTemplateLiteral);
write(", ");
emitParenthesizedIf(node.tag, needsParenthesisForPropertyAccessOrInvocation(node.tag));
write("(");