From a33315aaa70884c10968669c31adf3c6bb142e72 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Tue, 22 Sep 2020 23:10:02 +0800 Subject: fix(cli/console): quote object symbol keys that are invalid identifiers (#7553) --- cli/rt/02_console.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'cli/rt') 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, -- cgit v1.2.3