From 4d52753942e9affdc64e6ce884db9bde9653b5fe Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 24 Aug 2017 13:36:16 -0700 Subject: [PATCH] Escape \0 followed by a number as a hex escape to avoid printing an octal literal --- src/compiler/utilities.ts | 7 ++++++- ...uldNotPrintNullEscapesIntoOctalLiterals.js | 15 +++++++++++++++ ...tPrintNullEscapesIntoOctalLiterals.symbols | 8 ++++++++ ...NotPrintNullEscapesIntoOctalLiterals.types | 19 +++++++++++++++++++ ...uldNotPrintNullEscapesIntoOctalLiterals.ts | 6 ++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.js create mode 100644 tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.symbols create mode 100644 tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.types create mode 100644 tests/cases/compiler/shouldNotPrintNullEscapesIntoOctalLiterals.ts diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index d906ab4f6b1e7..6d53f9e8e996c 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -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) { diff --git a/tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.js b/tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.js new file mode 100644 index 0000000000000..4654558135c94 --- /dev/null +++ b/tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.js @@ -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"; diff --git a/tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.symbols b/tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.symbols new file mode 100644 index 0000000000000..3ffb259aed0c9 --- /dev/null +++ b/tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.symbols @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.types b/tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.types new file mode 100644 index 0000000000000..8f76e66255853 --- /dev/null +++ b/tests/baselines/reference/shouldNotPrintNullEscapesIntoOctalLiterals.types @@ -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" + diff --git a/tests/cases/compiler/shouldNotPrintNullEscapesIntoOctalLiterals.ts b/tests/cases/compiler/shouldNotPrintNullEscapesIntoOctalLiterals.ts new file mode 100644 index 0000000000000..fbbab43319a8b --- /dev/null +++ b/tests/cases/compiler/shouldNotPrintNullEscapesIntoOctalLiterals.ts @@ -0,0 +1,6 @@ +"use strict"; +`\x001`; +`\u00001`; +`\u{00000000}1`; +`\u{000000}1`; +`\u{0}1`; \ No newline at end of file