summaryrefslogtreecommitdiff
path: root/cli/rt
diff options
context:
space:
mode:
authorTuan Le <23419763+tumile@users.noreply.github.com>2020-08-24 12:38:21 -0400
committerGitHub <noreply@github.com>2020-08-24 12:38:21 -0400
commit545ea8e2171b0dc83477b98441241afe39771ed6 (patch)
tree57e40c88a9be51de814ab75267c8d2325e557dbb /cli/rt
parent0cbf9bdbbd5ce4f6b603671bb63c5a136cf523b7 (diff)
fix(console): handle escape sequences when logging objects (#7171)
Diffstat (limited to 'cli/rt')
-rw-r--r--cli/rt/02_console.js32
1 files changed, 23 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,