mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 23:34:47 -05:00
fix(console): handle escape sequences when logging objects (#7171)
This commit is contained in:
parent
0cbf9bdbbd
commit
545ea8e217
2 changed files with 40 additions and 9 deletions
|
@ -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,
|
||||
|
|
|
@ -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(['\\,"']), `[ '\\\\,"' ]`);
|
||||
|
|
Loading…
Reference in a new issue