From e1b61d6794facf2d8da4d13273685dc0a5248aed Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Thu, 24 Sep 2020 02:10:35 +0800 Subject: [PATCH] fix(cli/console): quote non-alphanumeric symbols (#7641) This quotes and escapes symbol descriptions that contains characters outside of the basic alpha-numeric identifier range. --- cli/rt/02_console.js | 2 +- cli/tests/unit/console_test.ts | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index ae17d41999..0b5931616f 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -414,7 +414,7 @@ case "undefined": // undefined is dim return dim(String(value)); case "symbol": // Symbols are green - return green(String(value)); + return green(maybeQuoteSymbol(value)); case "bigint": // Bigints are yellow return yellow(`${value}n`); case "function": // Function string is cyan diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index 1163705a90..38bb852f01 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -102,6 +102,32 @@ unitTest( "foo\\n", "foo\\r", "foo\\t", "foo\\v", "foo\\x00" +]`, + ); + assertEquals( + stringify( + [ + Symbol(), + Symbol(""), + Symbol("foo\b"), + Symbol("foo\f"), + Symbol("foo\n"), + Symbol("foo\r"), + Symbol("foo\t"), + Symbol("foo\v"), + Symbol("foo\0"), + ], + ), + `[ + Symbol(), + Symbol(""), + Symbol("foo\\b"), + Symbol("foo\\f"), + Symbol("foo\\n"), + Symbol("foo\\r"), + Symbol("foo\\t"), + Symbol("foo\\v"), + Symbol("foo\\x00") ]`, ); assertEquals( @@ -247,7 +273,7 @@ unitTest(function consoleTestStringifyCircular(): void { ); assertEquals(stringify(new WeakSet()), "WeakSet { [items unknown] }"); assertEquals(stringify(new WeakMap()), "WeakMap { [items unknown] }"); - assertEquals(stringify(Symbol(1)), "Symbol(1)"); + assertEquals(stringify(Symbol(1)), `Symbol("1")`); assertEquals(stringify(null), "null"); assertEquals(stringify(undefined), "undefined"); assertEquals(stringify(new Extended()), "Extended { a: 1, b: 2 }");