Skip to content

Commit 2f1bd8c

Browse files
authored
Escape \0 followed by a number as a hex escape to avoid printing an octal literal (#18026)
1 parent 038d256 commit 2f1bd8c

5 files changed

+54
-1
lines changed

src/compiler/utilities.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,7 @@ namespace ts {
23802380
"\u2029": "\\u2029", // paragraphSeparator
23812381
"\u0085": "\\u0085" // nextLine
23822382
});
2383+
const escapedNullRegExp = /\\0[0-9]/g;
23832384

23842385
/**
23852386
* Based heavily on the abstract 'Quote'/'QuoteJSONString' operation from ECMA-262 (24.3.2.2),
@@ -2391,7 +2392,11 @@ namespace ts {
23912392
quoteChar === CharacterCodes.backtick ? backtickQuoteEscapedCharsRegExp :
23922393
quoteChar === CharacterCodes.singleQuote ? singleQuoteEscapedCharsRegExp :
23932394
doubleQuoteEscapedCharsRegExp;
2394-
return s.replace(escapedCharsRegExp, getReplacement);
2395+
return s.replace(escapedCharsRegExp, getReplacement).replace(escapedNullRegExp, nullReplacement);
2396+
}
2397+
2398+
function nullReplacement(c: string) {
2399+
return "\\x00" + c.charAt(c.length - 1);
23952400
}
23962401

23972402
function getReplacement(c: string) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [shouldNotPrintNullEscapesIntoOctalLiterals.ts]
2+
"use strict";
3+
`\x001`;
4+
`\u00001`;
5+
`\u{00000000}1`;
6+
`\u{000000}1`;
7+
`\u{0}1`;
8+
9+
//// [shouldNotPrintNullEscapesIntoOctalLiterals.js]
10+
"use strict";
11+
"\x001";
12+
"\x001";
13+
"\x001";
14+
"\x001";
15+
"\x001";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/shouldNotPrintNullEscapesIntoOctalLiterals.ts ===
2+
"use strict";
3+
No type information for this code.`\x001`;
4+
No type information for this code.`\u00001`;
5+
No type information for this code.`\u{00000000}1`;
6+
No type information for this code.`\u{000000}1`;
7+
No type information for this code.`\u{0}1`;
8+
No type information for this code.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/shouldNotPrintNullEscapesIntoOctalLiterals.ts ===
2+
"use strict";
3+
>"use strict" : "use strict"
4+
5+
`\x001`;
6+
>`\x001` : "\x001"
7+
8+
`\u00001`;
9+
>`\u00001` : "\x001"
10+
11+
`\u{00000000}1`;
12+
>`\u{00000000}1` : "\x001"
13+
14+
`\u{000000}1`;
15+
>`\u{000000}1` : "\x001"
16+
17+
`\u{0}1`;
18+
>`\u{0}1` : "\x001"
19+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
`\x001`;
3+
`\u00001`;
4+
`\u{00000000}1`;
5+
`\u{000000}1`;
6+
`\u{0}1`;

0 commit comments

Comments
 (0)