0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

fix(cli/rt/console): Don't require a prototype to detect a class instance (#7869)

This commit is contained in:
Nayeem Rahman 2020-10-07 21:08:54 +01:00 committed by GitHub
parent 46e9758962
commit 8bd7c936f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View file

@ -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 constructor = instance?.constructor;
if (typeof constructor == "function") {
return constructor.name ?? "";
}
const proto = Object.getPrototypeOf(instance);
if (proto && proto.constructor) {
return proto.constructor.name; // could be "Object" or "Array"
}
return "";
}

View file

@ -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 })),