Skip to content

Use single replacer for string escaping #22335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2466,7 +2466,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",
Expand All @@ -2481,7 +2480,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),
Expand All @@ -2493,14 +2491,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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This technically only needs to be 0 through 7 (isOctalDigit), but I'm fine with this.

// 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));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Original file line number Diff line number Diff line change
@@ -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))

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

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