Use single replacer for string escaping (#22335)

This commit is contained in:
Wesley Wigham
2018-03-07 17:36:31 -08:00
committed by GitHub
parent a826c787f6
commit 45eaf42006
5 changed files with 32 additions and 8 deletions

View File

@@ -2471,7 +2471,6 @@ namespace ts {
const singleQuoteEscapedCharsRegExp = /[\\\'\u0000-\u001f\t\v\f\b\r\n\u2028\u2029\u0085]/g;
const backtickQuoteEscapedCharsRegExp = /[\\\`\u0000-\u001f\t\v\f\b\r\n\u2028\u2029\u0085]/g;
const escapedCharsMap = createMapFromTemplate({
"\0": "\\0",
"\t": "\\t",
"\v": "\\v",
"\f": "\\f",
@@ -2486,7 +2485,6 @@ namespace ts {
"\u2029": "\\u2029", // paragraphSeparator
"\u0085": "\\u0085" // nextLine
});
const escapedNullRegExp = /\\0[0-9]/g;
/**
* Based heavily on the abstract 'Quote'/'QuoteJSONString' operation from ECMA-262 (24.3.2.2),
@@ -2498,14 +2496,19 @@ namespace ts {
quoteChar === CharacterCodes.backtick ? backtickQuoteEscapedCharsRegExp :
quoteChar === CharacterCodes.singleQuote ? singleQuoteEscapedCharsRegExp :
doubleQuoteEscapedCharsRegExp;
return s.replace(escapedCharsRegExp, getReplacement).replace(escapedNullRegExp, nullReplacement);
return s.replace(escapedCharsRegExp, getReplacement);
}
function nullReplacement(c: string) {
return "\\x00" + c.charAt(c.length - 1);
}
function getReplacement(c: string) {
function getReplacement(c: string, offset: number, input: string) {
if (c.charCodeAt(0) === CharacterCodes.nullCharacter) {
const lookAhead = input.charCodeAt(offset + c.length);
if (lookAhead >= CharacterCodes._0 && lookAhead <= CharacterCodes._9) {
// If the null character is followed by digits, print as a hex escape to prevent the result from parsing as an octal (which is forbidden in strict mode)
return "\\x00";
}
// Otherwise, keep printing a literal \0 for the null character
return "\\0";
}
return escapedCharsMap.get(c) || get16BitUnicodeEscapeSequence(c.charCodeAt(0));
}

View File

@@ -0,0 +1,8 @@
//// [nonstrictTemplateWithNotOctalPrintsAsIs.ts]
// https://github.com/Microsoft/TypeScript/issues/21828
const d2 = `\\0041`;
//// [nonstrictTemplateWithNotOctalPrintsAsIs.js]
// https://github.com/Microsoft/TypeScript/issues/21828
var d2 = "\\0041";

View File

@@ -0,0 +1,5 @@
=== tests/cases/compiler/nonstrictTemplateWithNotOctalPrintsAsIs.ts ===
// https://github.com/Microsoft/TypeScript/issues/21828
const d2 = `\\0041`;
>d2 : Symbol(d2, Decl(nonstrictTemplateWithNotOctalPrintsAsIs.ts, 1, 5))

View File

@@ -0,0 +1,6 @@
=== tests/cases/compiler/nonstrictTemplateWithNotOctalPrintsAsIs.ts ===
// https://github.com/Microsoft/TypeScript/issues/21828
const d2 = `\\0041`;
>d2 : "\\0041"
>`\\0041` : "\\0041"

View File

@@ -0,0 +1,2 @@
// https://github.com/Microsoft/TypeScript/issues/21828
const d2 = `\\0041`;