Escape \0 followed by a number as a hex escape to avoid printing an octal literal (#18026)

This commit is contained in:
Wesley Wigham 2017-08-24 15:52:04 -07:00 committed by GitHub
parent 038d256fe5
commit 2f1bd8cff9
5 changed files with 54 additions and 1 deletions

View file

@ -2380,6 +2380,7 @@ 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),
@ -2391,7 +2392,11 @@ namespace ts {
quoteChar === CharacterCodes.backtick ? backtickQuoteEscapedCharsRegExp :
quoteChar === CharacterCodes.singleQuote ? singleQuoteEscapedCharsRegExp :
doubleQuoteEscapedCharsRegExp;
return s.replace(escapedCharsRegExp, getReplacement);
return s.replace(escapedCharsRegExp, getReplacement).replace(escapedNullRegExp, nullReplacement);
}
function nullReplacement(c: string) {
return "\\x00" + c.charAt(c.length - 1);
}
function getReplacement(c: string) {

View file

@ -0,0 +1,15 @@
//// [shouldNotPrintNullEscapesIntoOctalLiterals.ts]
"use strict";
`\x001`;
`\u00001`;
`\u{00000000}1`;
`\u{000000}1`;
`\u{0}1`;
//// [shouldNotPrintNullEscapesIntoOctalLiterals.js]
"use strict";
"\x001";
"\x001";
"\x001";
"\x001";
"\x001";

View file

@ -0,0 +1,8 @@
=== tests/cases/compiler/shouldNotPrintNullEscapesIntoOctalLiterals.ts ===
"use strict";
No type information for this code.`\x001`;
No type information for this code.`\u00001`;
No type information for this code.`\u{00000000}1`;
No type information for this code.`\u{000000}1`;
No type information for this code.`\u{0}1`;
No type information for this code.

View file

@ -0,0 +1,19 @@
=== tests/cases/compiler/shouldNotPrintNullEscapesIntoOctalLiterals.ts ===
"use strict";
>"use strict" : "use strict"
`\x001`;
>`\x001` : "\x001"
`\u00001`;
>`\u00001` : "\x001"
`\u{00000000}1`;
>`\u{00000000}1` : "\x001"
`\u{000000}1`;
>`\u{000000}1` : "\x001"
`\u{0}1`;
>`\u{0}1` : "\x001"

View file

@ -0,0 +1,6 @@
"use strict";
`\x001`;
`\u00001`;
`\u{00000000}1`;
`\u{000000}1`;
`\u{0}1`;