@@ -2193,13 +2193,7 @@ module ts {
21932193 }
21942194
21952195 function emitLiteral ( node : LiteralExpression ) {
2196- var text = languageVersion < ScriptTarget . ES6 && ( isTemplateLiteralKind ( node . kind ) || node . hasExtendedUnicodeEscape )
2197- ? getDoubleQuotedStringTextOfLiteral ( node )
2198- : node . parent
2199- ? getSourceTextOfNodeFromSourceFile ( currentSourceFile , node )
2200- : node . kind === SyntaxKind . NumericLiteral
2201- ? node . text
2202- : getDoubleQuotedStringTextOfLiteral ( node ) ;
2196+ var text = getLiteralText ( node ) ;
22032197
22042198 if ( compilerOptions . sourceMap && ( node . kind === SyntaxKind . StringLiteral || isTemplateLiteralKind ( node . kind ) ) ) {
22052199 writer . writeLiteral ( text ) ;
@@ -2213,11 +2207,41 @@ module ts {
22132207 }
22142208 }
22152209
2216- function getDoubleQuotedStringTextOfLiteral ( node : LiteralExpression ) : string {
2217- var result = escapeString ( node . text ) ;
2218- result = escapeNonAsciiCharacters ( result ) ;
2210+ function getLiteralText ( node : LiteralExpression ) {
2211+ // Any template literal or string literal with an extended escape
2212+ // (e.g. "\u{0067}") will need to be downleveled as a escaped string literal.
2213+ if ( languageVersion < ScriptTarget . ES6 && ( isTemplateLiteralKind ( node . kind ) || node . hasExtendedUnicodeEscape ) ) {
2214+ return getQuotedEscapedLiteralText ( '"' , node . text , '"' ) ;
2215+ }
2216+
2217+ // If we don't need to downlevel, and we can reach the original source text using
2218+ // the node's parent reference, then simply get the text as it was originally written.
2219+ if ( node . parent ) {
2220+ return getSourceTextOfNodeFromSourceFile ( currentSourceFile , node ) ;
2221+ }
22192222
2220- return '"' + result + '"' ;
2223+ // If we can't reach the original source text, use the canonical form of it's a number,
2224+ // or a escaped quoted form of the original text if it's string-like.
2225+ switch ( node . kind ) {
2226+ case SyntaxKind . StringLiteral :
2227+ return getQuotedEscapedLiteralText ( '"' , node . text , '"' ) ;
2228+ case SyntaxKind . NoSubstitutionTemplateLiteral :
2229+ return getQuotedEscapedLiteralText ( '`' , node . text , '`' ) ;
2230+ case SyntaxKind . TemplateHead :
2231+ return getQuotedEscapedLiteralText ( '`' , node . text , '${' ) ;
2232+ case SyntaxKind . TemplateMiddle :
2233+ return getQuotedEscapedLiteralText ( '}' , node . text , '${' ) ;
2234+ case SyntaxKind . TemplateTail :
2235+ return getQuotedEscapedLiteralText ( '}' , node . text , '`' ) ;
2236+ case SyntaxKind . NumericLiteral :
2237+ return node . text ;
2238+ }
2239+
2240+ Debug . fail ( `Literal kind '${ node . kind } ' not accounted for.` ) ;
2241+ }
2242+
2243+ function getQuotedEscapedLiteralText ( leftQuote : string , text : string , rightQuote : string ) {
2244+ return leftQuote + escapeNonAsciiCharacters ( escapeString ( text ) ) + rightQuote ;
22212245 }
22222246
22232247 function emitDownlevelRawTemplateLiteral ( node : LiteralExpression ) {
0 commit comments