diff options
author | Casper Beyer <caspervonb@pm.me> | 2020-09-18 23:25:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-18 11:25:09 -0400 |
commit | 93e8e59a7e651277f9943b8e9bf4ff6342243a90 (patch) | |
tree | 6de76fd1efd95478a93e1b3a042ed073c0a59b7b | |
parent | ec174170ba090728ebb83b05450d5550a514f43f (diff) |
fix(cli/console): surround non alpha-numeric object keys with quotes (#7550)
-rw-r--r-- | cli/rt/02_console.js | 11 | ||||
-rw-r--r-- | cli/tests/unit/console_test.ts | 2 |
2 files changed, 11 insertions, 2 deletions
diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index f82262a7e..1dd69efef 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -472,6 +472,15 @@ ); } + // Surround a string with quotes when it is required (e.g the string not a valid identifier). + function maybeQuoteString(string) { + if (/^[a-zA-Z_][a-zA-Z_0-9]*$/.test(string)) { + return replaceEscapeSequences(string); + } + + return quoteString(string); + } + // Print strings when they are inside of arrays or objects with quotes function inspectValueWithQuotes( value, @@ -713,7 +722,7 @@ for (const key of stringKeys) { entries.push( - `${replaceEscapeSequences(key)}: ${ + `${maybeQuoteString(key)}: ${ inspectValueWithQuotes( value[key], ctx, diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index 3df4b13e4..97f4ce0d2 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -108,7 +108,7 @@ unitTest( 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" }`, + `{ "foo\\b": "bar\\n", "bar\\r": "baz\\t", "qux\\x00": "qux\\x00" }`, ); assertEquals( stringify(new Set(["foo\n", "foo\r", "foo\0"])), |