diff options
author | Casper Beyer <caspervonb@pm.me> | 2020-09-22 23:10:02 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-22 11:10:02 -0400 |
commit | a33315aaa70884c10968669c31adf3c6bb142e72 (patch) | |
tree | 590600e88bc9f8d39b3f569f1ce63e384fd35389 | |
parent | c30c782c2c22d725c007623dbf43009999a2b184 (diff) |
fix(cli/console): quote object symbol keys that are invalid identifiers (#7553)
-rw-r--r-- | cli/rt/02_console.js | 15 | ||||
-rw-r--r-- | cli/tests/unit/console_test.ts | 18 |
2 files changed, 32 insertions, 1 deletions
diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index 1dd69efef..5de0bfa60 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -481,6 +481,19 @@ return quoteString(string); } + // Surround a symbol's description in quotes when it is required (e.g the description has non printable characters). + function maybeQuoteSymbol(symbol) { + if (symbol.description === undefined) { + return symbol.toString(); + } + + if (/^[a-zA-Z_][a-zA-Z_.0-9]*$/.test(symbol.description)) { + return symbol.toString(); + } + + return `Symbol(${quoteString(symbol.description)})`; + } + // Print strings when they are inside of arrays or objects with quotes function inspectValueWithQuotes( value, @@ -734,7 +747,7 @@ } for (const key of symbolKeys) { entries.push( - `${replaceEscapeSequences(key.toString())}: ${ + `${maybeQuoteSymbol(key)}: ${ inspectValueWithQuotes( value[key], ctx, diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index 97f4ce0d2..483ecfab6 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -111,6 +111,24 @@ unitTest( `{ "foo\\b": "bar\\n", "bar\\r": "baz\\t", "qux\\x00": "qux\\x00" }`, ); assertEquals( + stringify( + { + [Symbol("foo\b")]: `Symbol("foo\n")`, + [Symbol("bar\n")]: `Symbol("bar\n")`, + [Symbol("bar\r")]: `Symbol("bar\r")`, + [Symbol("baz\t")]: `Symbol("baz\t")`, + [Symbol("qux\0")]: `Symbol("qux\0")`, + }, + ), + `{ + Symbol("foo\\b"): 'Symbol("foo\\n\")', + Symbol("bar\\n"): 'Symbol("bar\\n\")', + Symbol("bar\\r"): 'Symbol("bar\\r\")', + Symbol("baz\\t"): 'Symbol("baz\\t\")', + Symbol("qux\\x00"): 'Symbol(\"qux\\x00")' +}`, + ); + assertEquals( stringify(new Set(["foo\n", "foo\r", "foo\0"])), `Set { "foo\\n", "foo\\r", "foo\\x00" }`, ); |