Skip to content

Commit

Permalink
Addressed CR feedback, also properly accounted for template literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRosenwasser committed Mar 3, 2015
1 parent 5ec68eb commit f9cc013
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
46 changes: 35 additions & 11 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2193,13 +2193,7 @@ module ts {
}

function emitLiteral(node: LiteralExpression) {
var text = languageVersion < ScriptTarget.ES6 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)
? getDoubleQuotedStringTextOfLiteral(node)
: node.parent
? getSourceTextOfNodeFromSourceFile(currentSourceFile, node)
: node.kind === SyntaxKind.NumericLiteral
? node.text
: getDoubleQuotedStringTextOfLiteral(node);
var text = getLiteralText(node);

if (compilerOptions.sourceMap && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
writer.writeLiteral(text);
Expand All @@ -2213,11 +2207,41 @@ module ts {
}
}

function getDoubleQuotedStringTextOfLiteral(node: LiteralExpression): string {
var result = escapeString(node.text);
result = escapeNonAsciiCharacters(result);
function getLiteralText(node: LiteralExpression) {
// Any template literal or string literal with an extended escape
// (e.g. "\u{0067}") will need to be downleveled as a escaped string literal.
if (languageVersion < ScriptTarget.ES6 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)) {
return getQuotedEscapedLiteralText('"', node.text, '"');
}

// If we don't need to downlevel, and we can reach the original source text using
// the node's parent reference, then simply get the text as it was originally written.
if (node.parent) {
return getSourceTextOfNodeFromSourceFile(currentSourceFile, node);
}

return '"' + result + '"';
// If we can't reach the original source text, use the canonical form of it's a number,
// or a escaped quoted form of the original text if it's string-like.
switch (node.kind) {
case SyntaxKind.StringLiteral:
return getQuotedEscapedLiteralText('"', node.text, '"');
case SyntaxKind.NoSubstitutionTemplateLiteral:
return getQuotedEscapedLiteralText('`', node.text, '`');
case SyntaxKind.TemplateHead:
return getQuotedEscapedLiteralText('`', node.text, '${');
case SyntaxKind.TemplateMiddle:
return getQuotedEscapedLiteralText('}', node.text, '${');
case SyntaxKind.TemplateTail:
return getQuotedEscapedLiteralText('}', node.text, '`');
case SyntaxKind.NumericLiteral:
return node.text;
}

Debug.fail(`Literal kind '${node.kind}' not accounted for.`);
}

function getQuotedEscapedLiteralText(leftQuote: string, text: string, rightQuote: string) {
return leftQuote + escapeNonAsciiCharacters(escapeString(text)) + rightQuote;
}

function emitDownlevelRawTemplateLiteral(node: LiteralExpression) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ module ts {
newEndN = Math.max(newEnd2, newEnd2 + (newEnd1 - oldEnd2));
}

return createTextChangeRange(createTextSpanFromBounds(oldStartN, oldEndN), /*newLength*/ newEndN - oldStartN);
return createTextChangeRange(createTextSpanFromBounds(oldStartN, oldEndN), /*newLength:*/ newEndN - oldStartN);
}

// @internal
Expand Down

1 comment on commit f9cc013

@CyrusNajmabadi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

👍

Please sign in to comment.