From aa49fdc5ff137549a0d8eb2f76dce9e34ddeea5f Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Thu, 3 Nov 2022 21:53:06 -0700 Subject: print: Pretty-print formatting for Symbols This formats Symbols as the source text with which they should be able to be constructed. It makes a distinction between registered symbols (created with Symbol.for()), well-known symbols (always existing), and regular symbols (created with Symbol()). --- modules/script/_bootstrap/default.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'modules') diff --git a/modules/script/_bootstrap/default.js b/modules/script/_bootstrap/default.js index 161418c0..e24a2dd4 100644 --- a/modules/script/_bootstrap/default.js +++ b/modules/script/_bootstrap/default.js @@ -40,6 +40,8 @@ return value.toString(); case 'string': return JSON.stringify(value); + case 'symbol': + return formatSymbol(value); default: return value.toString(); } @@ -71,6 +73,9 @@ case 'string': formattedObject.push(`${key}: "${value}"`); break; + case 'symbol': + formattedObject.push(`${key}: ${formatSymbol(value)}`); + break; default: formattedObject.push(`${key}: ${value}`); break; @@ -100,6 +105,23 @@ return funcOutput; } + function formatSymbol(sym) { + // Try to format Symbols in the same way that they would be constructed. + + // First check if this is a global registered symbol + const globalKey = Symbol.keyFor(sym); + if (globalKey !== undefined) + return `Symbol.for("${globalKey}")`; + + const descr = sym.description; + // Special-case the 'well-known' (built-in) Symbols + if (descr.startsWith('Symbol.')) + return descr; + + // Otherwise, it's just a regular symbol + return `Symbol("${descr}")`; + } + Object.defineProperties(exports, { ARGV: { configurable: false, -- cgit v1.2.1