From 8bd7c936f9a9a63334e5f512d6b6c1f8b47a42b8 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Wed, 7 Oct 2020 21:08:54 +0100 Subject: fix(cli/rt/console): Don't require a prototype to detect a class instance (#7869) --- cli/rt/02_console.js | 13 ++++--------- cli/tests/unit/console_test.ts | 9 +++++++-- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'cli') diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index 8c853deb9..72192eda4 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -175,18 +175,13 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ function getClassInstanceName(instance) { - if (typeof instance !== "object") { + if (typeof instance != "object") { return ""; } - if (!instance) { - return ""; - } - - const proto = Object.getPrototypeOf(instance); - if (proto && proto.constructor) { - return proto.constructor.name; // could be "Object" or "Array" + const constructor = instance?.constructor; + if (typeof constructor == "function") { + return constructor.name ?? ""; } - return ""; } diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index 2270f9724..46798fdf3 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -302,7 +302,7 @@ unitTest(function consoleTestStringifyCircular(): void { stringify(new Uint8Array([1, 2, 3])), "Uint8Array(3) [ 1, 2, 3 ]", ); - assertEquals(stringify(Uint8Array.prototype), "TypedArray {}"); + assertEquals(stringify(Uint8Array.prototype), "Uint8Array {}"); assertEquals( stringify({ a: { b: { c: { d: new Set([1]) } } } }), "{ a: { b: { c: { d: [Set] } } } }", @@ -832,7 +832,7 @@ unitTest(function consoleTestWithCustomInspectorError(): void { assertEquals(stringify(new B({ a: "a" })), "a"); assertEquals( stringify(B.prototype), - "{ [Symbol(Deno.customInspect)]: [Function: [Deno.customInspect]] }", + "B { [Symbol(Deno.customInspect)]: [Function: [Deno.customInspect]] }", ); }); @@ -1506,6 +1506,11 @@ unitTest(function inspectGetterError(): void { ); }); +unitTest(function inspectPrototype(): void { + class A {} + assertEquals(Deno.inspect(A.prototype), "A {}"); +}); + unitTest(function inspectSorted(): void { assertEquals( stripColor(Deno.inspect({ b: 2, a: 1 }, { sorted: true })), -- cgit v1.2.3