diff options
-rw-r--r-- | cli/rt/02_console.js | 32 | ||||
-rw-r--r-- | cli/tests/unit/console_test.ts | 17 |
2 files changed, 40 insertions, 9 deletions
diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index 1cf47afbe..e3492772d 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -439,16 +439,30 @@ const QUOTES = ['"', "'", "`"]; /** Surround the string in quotes. - * - * The quote symbol is chosen by taking the first of the `QUOTES` array which - * does not occur in the string. If they all occur, settle with `QUOTES[0]`. - * - * Insert a backslash before any occurrence of the chosen quote symbol and - * before any backslash. */ + * + * The quote symbol is chosen by taking the first of the `QUOTES` array which + * does not occur in the string. If they all occur, settle with `QUOTES[0]`. + * + * Insert a backslash before any occurrence of the chosen quote symbol and + * before any backslash. + */ function quoteString(string) { const quote = QUOTES.find((c) => !string.includes(c)) ?? QUOTES[0]; const escapePattern = new RegExp(`(?=[${quote}\\\\])`, "g"); - return `${quote}${string.replace(escapePattern, "\\")}${quote}`; + string = string.replace(escapePattern, "\\"); + string = replaceEscapeSequences(string); + return `${quote}${string}${quote}`; + } + + // Replace escape sequences that can modify output. + function replaceEscapeSequences(string) { + return string + .replace(/[\b]/g, "\\b") + .replace(/\f/g, "\\f") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/\t/g, "\\t") + .replace(/\v/g, "\\v"); } // Print strings when they are inside of arrays or objects with quotes @@ -683,7 +697,7 @@ for (const key of stringKeys) { entries.push( - `${key}: ${ + `${replaceEscapeSequences(key)}: ${ inspectValueWithQuotes( value[key], ctx, @@ -695,7 +709,7 @@ } for (const key of symbolKeys) { entries.push( - `${key.toString()}: ${ + `${replaceEscapeSequences(key.toString())}: ${ inspectValueWithQuotes( value[key], ctx, diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index c36db8f4e..60d2143c0 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -57,6 +57,23 @@ unitTest(function consoleTestStringifyComplexObjects(): void { assertEquals(stringify({ foo: "bar" }), `{ foo: "bar" }`); }); +unitTest( + function consoleTestStringifyComplexObjectsWithEscapedSequences(): void { + assertEquals( + stringify(["foo\b", "foo\f", "foo\n", "foo\r", "foo\t", "foo\v"]), + `[ "foo\\b", "foo\\f", "foo\\n", "foo\\r", "foo\\t", "foo\\v" ]`, + ); + assertEquals( + stringify({ "foo\b": "bar\n", "bar\r": "baz\t" }), + `{ foo\\b: "bar\\n", bar\\r: "baz\\t" }`, + ); + assertEquals( + stringify(new Set(["foo\n", "foo\r"])), + `Set { "foo\\n", "foo\\r" }`, + ); + }, +); + unitTest(function consoleTestStringifyQuotes(): void { assertEquals(stringify(["\\"]), `[ "\\\\" ]`); assertEquals(stringify(['\\,"']), `[ '\\\\,"' ]`); |