1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

fix(cli/console): escape non printable characters in object entries (#7533)

This commit is contained in:
Casper Beyer 2020-09-18 01:07:57 +08:00 committed by GitHub
parent c307e3e4be
commit a6f4559174
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View file

@ -462,7 +462,11 @@
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/\t/g, "\\t")
.replace(/\v/g, "\\v");
.replace(/\v/g, "\\v")
.replace(
/[\x00-\x1f\x7f-\x9f]/g,
(c) => "\\x" + c.charCodeAt(0).toString(16).padStart(2, "0"),
);
}
// Print strings when they are inside of arrays or objects with quotes

View file

@ -94,16 +94,25 @@ unitTest(function consoleTestStringifyComplexObjects(): void {
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" ]`,
stringify(
["foo\b", "foo\f", "foo\n", "foo\r", "foo\t", "foo\v", "foo\0"],
),
`[
"foo\\b", "foo\\f",
"foo\\n", "foo\\r",
"foo\\t", "foo\\v",
"foo\\x00"
]`,
);
assertEquals(
stringify({ "foo\b": "bar\n", "bar\r": "baz\t" }),
`{ foo\\b: "bar\\n", bar\\r: "baz\\t" }`,
stringify(
{ "foo\b": "bar\n", "bar\r": "baz\t", "qux\0": "qux\0" },
),
`{ foo\\b: "bar\\n", bar\\r: "baz\\t", qux\\x00: "qux\\x00" }`,
);
assertEquals(
stringify(new Set(["foo\n", "foo\r"])),
`Set { "foo\\n", "foo\\r" }`,
stringify(new Set(["foo\n", "foo\r", "foo\0"])),
`Set { "foo\\n", "foo\\r", "foo\\x00" }`,
);
},
);