Augment escapeString to fix downlevel template literal emit.

This commit is contained in:
Daniel Rosenwasser 2014-12-01 17:27:11 -08:00
parent 10702797d9
commit ddb3ca0f48
16 changed files with 91 additions and 10 deletions

View File

@ -591,27 +591,44 @@ module ts {
return path;
}
var escapedCharsRegExp = /[\t\v\f\b\0\r\n\"\\\u2028\u2029\u0085]/g;
var backslashOrDoubleQuote = /[\"\\]/g;
var escapedCharsRegExp = /[\0-\19\t\v\f\b\0\r\n\u2028\u2029\u0085]/g;
var escapedCharsMap: Map<string> = {
"\0": "\\0",
"\t": "\\t",
"\v": "\\v",
"\f": "\\f",
"\b": "\\b",
"\0": "\\0",
"\r": "\\r",
"\n": "\\n",
"\\": "\\\\",
"\"": "\\\"",
"\u2028": "\\u2028", // lineSeparator
"\u2029": "\\u2029", // paragraphSeparator
"\u0085": "\\u0085" // nextLine
};
/** NOTE: This *does not* support the full escape characters, it only supports the subset that can be used in file names
* or string literals. If the information encoded in the map changes, this needs to be revisited. */
/**
* Based heavily on the abstract 'Quote' 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 {
return escapedCharsRegExp.test(s) ? s.replace(escapedCharsRegExp, c => {
return escapedCharsMap[c] || c;
}) : s;
// 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 interface ObjectAllocator {

View File

@ -3,4 +3,4 @@
`\\`
//// [templateStringTermination2.js]
"\";
"\\";

View File

@ -3,4 +3,4 @@
`\\\\`
//// [templateStringTermination4.js]
"\\";
"\\\\";

View File

@ -3,4 +3,4 @@
`\\\\\\`
//// [templateStringTermination5.js]
"\\\";
"\\\\\\";

View File

@ -0,0 +1,7 @@
//// [templateStringWhitespaceEscapes1.ts]
`\t\n\v\f\r`;
//// [templateStringWhitespaceEscapes1.js]
"\t\n\v\f\r";

View File

@ -0,0 +1,5 @@
=== tests/cases/conformance/es6/templates/templateStringWhitespaceEscapes1.ts ===
No type information for this code.
No type information for this code.`\t\n\v\f\r`;
No type information for this code.

View File

@ -0,0 +1,6 @@
//// [templateStringWhitespaceEscapes1_ES6.ts]
`\t\n\v\f\r`;
//// [templateStringWhitespaceEscapes1_ES6.js]
`\t\n\v\f\r`;

View File

@ -0,0 +1,4 @@
=== tests/cases/conformance/es6/templates/templateStringWhitespaceEscapes1_ES6.ts ===
No type information for this code.`\t\n\v\f\r`;
No type information for this code.

View File

@ -0,0 +1,9 @@
//// [templateStringWhitespaceEscapes2.ts]
// <TAB>, <VT>, <FF>, <SP>, <NBSP>, <BOM>
`\u0009\u000B\u000C\u0020\u00A0\uFEFF`;
//// [templateStringWhitespaceEscapes2.js]
// <TAB>, <VT>, <FF>, <SP>, <NBSP>, <BOM>
"\t\v\f  ";

View File

@ -0,0 +1,6 @@
=== tests/cases/conformance/es6/templates/templateStringWhitespaceEscapes2.ts ===
No type information for this code.
No type information for this code.// <TAB>, <VT>, <FF>, <SP>, <NBSP>, <BOM>
No type information for this code.`\u0009\u000B\u000C\u0020\u00A0\uFEFF`;
No type information for this code.

View File

@ -0,0 +1,8 @@
//// [templateStringWhitespaceEscapes2_ES6.ts]
// <TAB>, <VT>, <FF>, <SP>, <NBSP>, <BOM>
`\u0009\u000B\u000C\u0020\u00A0\uFEFF`;
//// [templateStringWhitespaceEscapes2_ES6.js]
// <TAB>, <VT>, <FF>, <SP>, <NBSP>, <BOM>
`\u0009\u000B\u000C\u0020\u00A0\uFEFF`;

View File

@ -0,0 +1,5 @@
=== tests/cases/conformance/es6/templates/templateStringWhitespaceEscapes2_ES6.ts ===
No type information for this code.// <TAB>, <VT>, <FF>, <SP>, <NBSP>, <BOM>
No type information for this code.`\u0009\u000B\u000C\u0020\u00A0\uFEFF`;
No type information for this code.

View File

@ -0,0 +1,3 @@

`\t\n\v\f\r`;

View File

@ -0,0 +1,3 @@
//@target: es6
`\t\n\v\f\r`;

View File

@ -0,0 +1,4 @@

// <TAB>, <VT>, <FF>, <SP>, <NBSP>, <BOM>
`\u0009\u000B\u000C\u0020\u00A0\uFEFF`;

View File

@ -0,0 +1,4 @@
//@target: es6
// <TAB>, <VT>, <FF>, <SP>, <NBSP>, <BOM>
`\u0009\u000B\u000C\u0020\u00A0\uFEFF`;