mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-17 01:49:57 -05:00
Emit non-ASCII characters with unicode escape sequences.
This commit is contained in:
@@ -623,29 +623,6 @@ module ts {
|
||||
"\u0085": "\\u0085" // nextLine
|
||||
};
|
||||
|
||||
/**
|
||||
* Based heavily on the abstract 'Quote'/ 'QuoteJSONString' operation from ECMA-262 (24.3.2.2),
|
||||
* but augmented for a few select characters.
|
||||
* Note that this doesn't actually wrap the input in double quotes.
|
||||
*/
|
||||
export function escapeString(s: string): string {
|
||||
// Prioritize '"' and '\'
|
||||
s = backslashOrDoubleQuote.test(s) ? s.replace(backslashOrDoubleQuote, getReplacement) : s;
|
||||
s = escapedCharsRegExp.test(s) ? s.replace(escapedCharsRegExp, getReplacement) : s;
|
||||
|
||||
return s;
|
||||
|
||||
function getReplacement(c: string) {
|
||||
return escapedCharsMap[c] || unicodeEscape(c);
|
||||
}
|
||||
|
||||
function unicodeEscape(c: string): string {
|
||||
var hexCharCode = c.charCodeAt(0).toString(16);
|
||||
var paddedHexCode = ("0000" + hexCharCode).slice(-4);
|
||||
return "\\u" + paddedHexCode;
|
||||
}
|
||||
}
|
||||
|
||||
export function getDefaultLibFileName(options: CompilerOptions): string {
|
||||
return options.target === ScriptTarget.ES6 ? "lib.es6.d.ts" : "lib.d.ts";
|
||||
}
|
||||
|
||||
@@ -2201,9 +2201,12 @@ module ts {
|
||||
write(text);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getTemplateLiteralAsStringLiteral(node: LiteralExpression): string {
|
||||
return '"' + escapeString(node.text) + '"';
|
||||
var result = escapeString(node.text);
|
||||
result = replaceNonAsciiCharacters(result);
|
||||
|
||||
return '"' + result + '"';
|
||||
}
|
||||
|
||||
function emitDownlevelRawTemplateLiteral(node: LiteralExpression) {
|
||||
|
||||
@@ -1120,7 +1120,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
|
||||
@@ -1202,4 +1202,53 @@ module ts {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var backslashOrDoubleQuote = /[\"\\]/g;
|
||||
var escapedCharsRegExp = /[\u0000-\u001f\t\v\f\b\r\n\u2028\u2029\u0085]/g;
|
||||
var escapedCharsMap: Map<string> = {
|
||||
"\0": "\\0",
|
||||
"\t": "\\t",
|
||||
"\v": "\\v",
|
||||
"\f": "\\f",
|
||||
"\b": "\\b",
|
||||
"\r": "\\r",
|
||||
"\n": "\\n",
|
||||
"\\": "\\\\",
|
||||
"\"": "\\\"",
|
||||
"\u2028": "\\u2028", // lineSeparator
|
||||
"\u2029": "\\u2029", // paragraphSeparator
|
||||
"\u0085": "\\u0085" // nextLine
|
||||
};
|
||||
|
||||
/**
|
||||
* Based heavily on the abstract 'Quote'/ 'QuoteJSONString' operation from ECMA-262 (24.3.2.2),
|
||||
* but augmented for a few select characters.
|
||||
* Note that this doesn't actually wrap the input in double quotes.
|
||||
*/
|
||||
export function escapeString(s: string): string {
|
||||
// Prioritize '"' and '\'
|
||||
s = backslashOrDoubleQuote.test(s) ? s.replace(backslashOrDoubleQuote, getReplacement) : s;
|
||||
s = escapedCharsRegExp.test(s) ? s.replace(escapedCharsRegExp, getReplacement) : s;
|
||||
|
||||
return s;
|
||||
|
||||
function getReplacement(c: string) {
|
||||
return escapedCharsMap[c] || get16BitUnicodeEscapeSequence(c.charCodeAt(0));
|
||||
}
|
||||
}
|
||||
|
||||
function get16BitUnicodeEscapeSequence(charCode: number): string {
|
||||
var hexCharCode = charCode.toString(16);
|
||||
var paddedHexCode = ("0000" + hexCharCode).slice(-4);
|
||||
return "\\u" + paddedHexCode;
|
||||
}
|
||||
|
||||
var nonAsciiCharacters = /[^\u0000-\u007F]/g;
|
||||
export function replaceNonAsciiCharacters(s: string): string {
|
||||
// Replace non-ASCII characters with '\uNNNN' escapes if any exist.
|
||||
// Otherwise just return the original string.
|
||||
return nonAsciiCharacters.test(s) ?
|
||||
s.replace(nonAsciiCharacters, c => get16BitUnicodeEscapeSequence(c.charCodeAt(0))) :
|
||||
s;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user