0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -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] } } } }"
);
assertEquals(stringify(nestedObj), nestedObjExpected);
assertEquals(stringify(JSON), "{}");
assertEquals(stringify(JSON), 'JSON { Symbol(Symbol.toStringTag): "JSON" }');
assertEquals(
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
assertEquals(inspect(nestedObj), nestedObjExpected);
@ -224,7 +228,10 @@ unitTest(function consoleTestWithCustomInspectorError(): void {
}
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 {

View file

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