mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
fix(console): fix inspection of Function (#7930)
This commit fixes the inspection of functions. The current implementation gets the name of the type of the function from "f.__proto__.constructor.name", and it throws when the prototype is set to null. This commit checks the prototype before accessing its constructor name and uses the generic name 'Function' if the prototype is not available.
This commit is contained in:
parent
86dc55134e
commit
265a9fb932
2 changed files with 22 additions and 2 deletions
|
@ -196,8 +196,13 @@
|
||||||
return String(value[customInspect]());
|
return String(value[customInspect]());
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
// Might be Function/AsyncFunction/GeneratorFunction
|
// Might be Function/AsyncFunction/GeneratorFunction/AsyncGeneratorFunction
|
||||||
const cstrName = Object.getPrototypeOf(value).constructor.name;
|
let cstrName = Object.getPrototypeOf(value)?.constructor?.name;
|
||||||
|
if (!cstrName) {
|
||||||
|
// If prototype is removed or broken,
|
||||||
|
// use generic 'Function' instead.
|
||||||
|
cstrName = "Function";
|
||||||
|
}
|
||||||
if (value.name && value.name !== "anonymous") {
|
if (value.name && value.name !== "anonymous") {
|
||||||
// from MDN spec
|
// from MDN spec
|
||||||
return `[${cstrName}: ${value.name}]`;
|
return `[${cstrName}: ${value.name}]`;
|
||||||
|
|
|
@ -347,6 +347,21 @@ unitTest(function consoleTestStringifyCircular(): void {
|
||||||
});
|
});
|
||||||
/* eslint-enable @typescript-eslint/explicit-function-return-type */
|
/* eslint-enable @typescript-eslint/explicit-function-return-type */
|
||||||
|
|
||||||
|
unitTest(function consoleTestStringifyFunctionWithPrototypeRemoved(): void {
|
||||||
|
const f = function f() {};
|
||||||
|
Reflect.setPrototypeOf(f, null);
|
||||||
|
assertEquals(stringify(f), "[Function: f]");
|
||||||
|
const af = async function af() {};
|
||||||
|
Reflect.setPrototypeOf(af, null);
|
||||||
|
assertEquals(stringify(af), "[Function: af]");
|
||||||
|
const gf = function gf() {};
|
||||||
|
Reflect.setPrototypeOf(gf, null);
|
||||||
|
assertEquals(stringify(gf), "[Function: gf]");
|
||||||
|
const agf = function agf() {};
|
||||||
|
Reflect.setPrototypeOf(agf, null);
|
||||||
|
assertEquals(stringify(agf), "[Function: agf]");
|
||||||
|
});
|
||||||
|
|
||||||
unitTest(function consoleTestStringifyWithDepth(): void {
|
unitTest(function consoleTestStringifyWithDepth(): void {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
const nestedObj: any = { a: { b: { c: { d: { e: { f: 42 } } } } } };
|
const nestedObj: any = { a: { b: { c: { d: { e: { f: 42 } } } } } };
|
||||||
|
|
Loading…
Reference in a new issue