Skip to content

Commit

Permalink
fix(cli/console): escape non printable characters in object entries (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervonb committed Sep 17, 2020
1 parent c307e3e commit a6f4559
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
6 changes: 5 additions & 1 deletion cli/rt/02_console.js
Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions cli/tests/unit/console_test.ts
Expand Up @@ -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" }`,
);
},
);
Expand Down

0 comments on commit a6f4559

Please sign in to comment.