summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2020-03-16 06:05:39 -0700
committerGitHub <noreply@github.com>2020-03-16 09:05:39 -0400
commit49541a04d25ba7308759824213b6053a7b412e3e (patch)
treed10b03759aa704d76818228cc0fe154272622eaf
parent62942749e6982b9aee24c22a5abdbc44b7f0e6b6 (diff)
console: Symbol.toStringTag and display Object symbol entries (#4388)
-rw-r--r--cli/js/tests/console_test.ts13
-rw-r--r--cli/js/web/console.ts57
-rw-r--r--cli/tests/041_dyn_import_eval.out2
-rw-r--r--cli/tests/042_dyn_import_evalcontext.ts.out2
-rw-r--r--cli/tests/055_import_wasm_via_network.ts.out2
-rw-r--r--cli/tests/fix_js_imports.ts.out2
6 files changed, 53 insertions, 25 deletions
diff --git a/cli/js/tests/console_test.ts b/cli/js/tests/console_test.ts
index 256c7da80..b64016078 100644
--- a/cli/js/tests/console_test.ts
+++ b/cli/js/tests/console_test.ts
@@ -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 {
diff --git a/cli/js/web/console.ts b/cli/js/web/console.ts
index 7f79dd255..0812a4e21 100644
--- a/cli/js/web/console.ts
+++ b/cli/js/web/console.ts
@@ -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;
diff --git a/cli/tests/041_dyn_import_eval.out b/cli/tests/041_dyn_import_eval.out
index 1dfef2e98..5ed886856 100644
--- a/cli/tests/041_dyn_import_eval.out
+++ b/cli/tests/041_dyn_import_eval.out
@@ -1 +1 @@
-{ isMod4: true }
+Module { isMod4: true, Symbol(Symbol.toStringTag): "Module" }
diff --git a/cli/tests/042_dyn_import_evalcontext.ts.out b/cli/tests/042_dyn_import_evalcontext.ts.out
index 1dfef2e98..5ed886856 100644
--- a/cli/tests/042_dyn_import_evalcontext.ts.out
+++ b/cli/tests/042_dyn_import_evalcontext.ts.out
@@ -1 +1 @@
-{ isMod4: true }
+Module { isMod4: true, Symbol(Symbol.toStringTag): "Module" }
diff --git a/cli/tests/055_import_wasm_via_network.ts.out b/cli/tests/055_import_wasm_via_network.ts.out
index 4c3589e02..ec832c585 100644
--- a/cli/tests/055_import_wasm_via_network.ts.out
+++ b/cli/tests/055_import_wasm_via_network.ts.out
@@ -1 +1 @@
-{ add_one: [Function: 0], memory: Memory {} }
+Module { add_one: [Function: 0], memory: WebAssembly.Memory {}, Symbol(Symbol.toStringTag): "Module" }
diff --git a/cli/tests/fix_js_imports.ts.out b/cli/tests/fix_js_imports.ts.out
index 0967ef424..2981f7445 100644
--- a/cli/tests/fix_js_imports.ts.out
+++ b/cli/tests/fix_js_imports.ts.out
@@ -1 +1 @@
-{}
+Module { Symbol(Symbol.toStringTag): "Module" }