From 93e8e59a7e651277f9943b8e9bf4ff6342243a90 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Fri, 18 Sep 2020 23:25:09 +0800 Subject: fix(cli/console): surround non alpha-numeric object keys with quotes (#7550) --- cli/rt/02_console.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'cli/rt') 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, -- cgit v1.2.3