@@ -344,15 +344,20 @@ namespace ts {
344
344
// or a (possibly escaped) quoted form of the original text if it's string-like.
345
345
switch ( node . kind ) {
346
346
case SyntaxKind . StringLiteral :
347
- return '"' + escapeText ( node . text ) + '"' ;
347
+ if ( ( < StringLiteral > node ) . singleQuote ) {
348
+ return "'" + escapeText ( node . text , CharacterCodes . singleQuote ) + "'" ;
349
+ }
350
+ else {
351
+ return '"' + escapeText ( node . text , CharacterCodes . doubleQuote ) + '"' ;
352
+ }
348
353
case SyntaxKind . NoSubstitutionTemplateLiteral :
349
- return "`" + escapeText ( node . text ) + "`" ;
354
+ return "`" + escapeText ( node . text , CharacterCodes . backtick ) + "`" ;
350
355
case SyntaxKind . TemplateHead :
351
- return "`" + escapeText ( node . text ) + "${" ;
356
+ return "`" + escapeText ( node . text , CharacterCodes . backtick ) + "${" ;
352
357
case SyntaxKind . TemplateMiddle :
353
- return "}" + escapeText ( node . text ) + "${" ;
358
+ return "}" + escapeText ( node . text , CharacterCodes . backtick ) + "${" ;
354
359
case SyntaxKind . TemplateTail :
355
- return "}" + escapeText ( node . text ) + "`" ;
360
+ return "}" + escapeText ( node . text , CharacterCodes . backtick ) + "`" ;
356
361
case SyntaxKind . NumericLiteral :
357
362
return node . text ;
358
363
}
@@ -2356,7 +2361,9 @@ namespace ts {
2356
2361
// the language service. These characters should be escaped when printing, and if any characters are added,
2357
2362
// the map below must be updated. Note that this regexp *does not* include the 'delete' character.
2358
2363
// There is no reason for this other than that JSON.stringify does not handle it either.
2359
- const escapedCharsRegExp = / [ \\ \" \u0000 - \u001f \t \v \f \b \r \n \u2028 \u2029 \u0085 ] / g;
2364
+ const doubleQuoteEscapedCharsRegExp = / [ \\ \" \u0000 - \u001f \t \v \f \b \r \n \u2028 \u2029 \u0085 ] / g;
2365
+ const singleQuoteEscapedCharsRegExp = / [ \\ \' \u0000 - \u001f \t \v \f \b \r \n \u2028 \u2029 \u0085 ] / g;
2366
+ const backtickQuoteEscapedCharsRegExp = / [ \\ \` \u0000 - \u001f \t \v \f \b \r \n \u2028 \u2029 \u0085 ] / g;
2360
2367
const escapedCharsMap = createMapFromTemplate ( {
2361
2368
"\0" : "\\0" ,
2362
2369
"\t" : "\\t" ,
@@ -2367,18 +2374,23 @@ namespace ts {
2367
2374
"\n" : "\\n" ,
2368
2375
"\\" : "\\\\" ,
2369
2376
"\"" : "\\\"" ,
2377
+ "\'" : "\\\'" ,
2378
+ "\`" : "\\\`" ,
2370
2379
"\u2028" : "\\u2028" , // lineSeparator
2371
2380
"\u2029" : "\\u2029" , // paragraphSeparator
2372
2381
"\u0085" : "\\u0085" // nextLine
2373
2382
} ) ;
2374
2383
2375
-
2376
2384
/**
2377
2385
* Based heavily on the abstract 'Quote'/'QuoteJSONString' operation from ECMA-262 (24.3.2.2),
2378
2386
* but augmented for a few select characters (e.g. lineSeparator, paragraphSeparator, nextLine)
2379
2387
* Note that this doesn't actually wrap the input in double quotes.
2380
2388
*/
2381
- export function escapeString ( s : string ) : string {
2389
+ export function escapeString ( s : string , quoteChar ?: CharacterCodes . doubleQuote | CharacterCodes . singleQuote | CharacterCodes . backtick ) : string {
2390
+ const escapedCharsRegExp =
2391
+ quoteChar === CharacterCodes . backtick ? backtickQuoteEscapedCharsRegExp :
2392
+ quoteChar === CharacterCodes . singleQuote ? singleQuoteEscapedCharsRegExp :
2393
+ doubleQuoteEscapedCharsRegExp ;
2382
2394
return s . replace ( escapedCharsRegExp , getReplacement ) ;
2383
2395
}
2384
2396
@@ -2400,8 +2412,8 @@ namespace ts {
2400
2412
}
2401
2413
2402
2414
const nonAsciiCharacters = / [ ^ \u0000 - \u007F ] / g;
2403
- export function escapeNonAsciiString ( s : string ) : string {
2404
- s = escapeString ( s ) ;
2415
+ export function escapeNonAsciiString ( s : string , quoteChar ?: CharacterCodes . doubleQuote | CharacterCodes . singleQuote | CharacterCodes . backtick ) : string {
2416
+ s = escapeString ( s , quoteChar ) ;
2405
2417
// Replace non-ASCII characters with '\uNNNN' escapes if any exist.
2406
2418
// Otherwise just return the original string.
2407
2419
return nonAsciiCharacters . test ( s ) ?
0 commit comments