0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -05:00

console: Symbol.toStringTag and display Object symbol entries (#4388)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2020-03-16 06:05:39 -07:00 committed by GitHub
parent 62942749e6
commit 49541a04d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 25 deletions

View file

@ -163,10 +163,14 @@ unitTest(function consoleTestStringifyCircular(): void {
"{ a: { b: { c: { d: [Set] } } } }" "{ a: { b: { c: { d: [Set] } } } }"
); );
assertEquals(stringify(nestedObj), nestedObjExpected); assertEquals(stringify(nestedObj), nestedObjExpected);
assertEquals(stringify(JSON), "{}"); assertEquals(stringify(JSON), 'JSON { Symbol(Symbol.toStringTag): "JSON" }');
assertEquals( assertEquals(
stringify(console), stringify(console),
"{ printFunc, log, debug, info, dir, dirxml, warn, error, assert, count, countReset, table, time, timeLog, timeEnd, group, groupCollapsed, groupEnd, clear, trace, indentLevel }" "{ printFunc, log, debug, info, dir, dirxml, warn, error, assert, count, countReset, table, time, timeLog, timeEnd, group, groupCollapsed, groupEnd, clear, trace, indentLevel, Symbol(isConsoleInstance) }"
);
assertEquals(
stringify({ str: 1, [Symbol.for("sym")]: 2, [Symbol.toStringTag]: "TAG" }),
'TAG { str: 1, Symbol(sym): 2, Symbol(Symbol.toStringTag): "TAG" }'
); );
// test inspect is working the same // test inspect is working the same
assertEquals(inspect(nestedObj), nestedObjExpected); assertEquals(inspect(nestedObj), nestedObjExpected);
@ -224,7 +228,10 @@ unitTest(function consoleTestWithCustomInspectorError(): void {
} }
assertEquals(stringify(new B({ a: "a" })), "a"); assertEquals(stringify(new B({ a: "a" })), "a");
assertEquals(stringify(B.prototype), "{}"); assertEquals(
stringify(B.prototype),
"{ Symbol(Deno.customInspect): [Function: [Deno.customInspect]] }"
);
}); });
unitTest(function consoleTestWithIntegerFormatSpecifier(): void { unitTest(function consoleTestWithIntegerFormatSpecifier(): void {

View file

@ -286,24 +286,45 @@ function createRawObjectString(
let baseString = ""; let baseString = "";
const className = getClassInstanceName(value); let shouldShowDisplayName = false;
let shouldShowClassName = false; // @ts-ignore
if (className && className !== "Object" && className !== "anonymous") { let displayName = value[Symbol.toStringTag];
shouldShowClassName = true; if (!displayName) {
displayName = getClassInstanceName(value);
}
if (displayName && displayName !== "Object" && displayName !== "anonymous") {
shouldShowDisplayName = true;
}
const entries: string[] = [];
const stringKeys = Object.keys(value);
const symbolKeys = Object.getOwnPropertySymbols(value);
const numKeys = stringKeys.length + symbolKeys.length;
if (numKeys > OBJ_ABBREVIATE_SIZE) {
for (const key of stringKeys) {
entries.push(key);
}
for (const key of symbolKeys) {
entries.push(key.toString());
} }
const keys = Object.keys(value);
const entries: string[] = keys.map((key): string => {
if (keys.length > OBJ_ABBREVIATE_SIZE) {
return key;
} else { } else {
return `${key}: ${stringifyWithQuotes( for (const key of stringKeys) {
entries.push(
`${key}: ${stringifyWithQuotes(value[key], ctx, level + 1, maxLevel)}`
);
}
for (const key of symbolKeys) {
entries.push(
`${key.toString()}: ${stringifyWithQuotes(
// @ts-ignore
value[key], value[key],
ctx, ctx,
level + 1, level + 1,
maxLevel maxLevel
)}`; )}`
);
}
} }
});
ctx.delete(value); ctx.delete(value);
@ -313,8 +334,8 @@ function createRawObjectString(
baseString = `{ ${entries.join(", ")} }`; baseString = `{ ${entries.join(", ")} }`;
} }
if (shouldShowClassName) { if (shouldShowDisplayName) {
baseString = `${className} ${baseString}`; baseString = `${displayName} ${baseString}`;
} }
return baseString; return baseString;

View file

@ -1 +1 @@
{ isMod4: true } Module { isMod4: true, Symbol(Symbol.toStringTag): "Module" }

View file

@ -1 +1 @@
{ isMod4: true } Module { isMod4: true, Symbol(Symbol.toStringTag): "Module" }

View file

@ -1 +1 @@
{ add_one: [Function: 0], memory: Memory {} } Module { add_one: [Function: 0], memory: WebAssembly.Memory {}, Symbol(Symbol.toStringTag): "Module" }

View file

@ -1 +1 @@
{} Module { Symbol(Symbol.toStringTag): "Module" }