diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index 0b5931616f..ab91ffc099 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -193,6 +193,11 @@ } function inspectFunction(value, _ctx) { + if (customInspect in value && typeof value[customInspect] === "function") { + try { + return String(value[customInspect]()); + } catch {} + } // Might be Function/AsyncFunction/GeneratorFunction const cstrName = Object.getPrototypeOf(value).constructor.name; if (value.name && value.name !== "anonymous") { diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index 38bb852f01..eb2c51aef9 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -831,6 +831,17 @@ unitTest(function consoleTestWithCustomInspectorError(): void { ); }); +unitTest(function consoleTestWithCustomInspectFunction(): void { + function a() {} + Object.assign(a, { + [customInspect]() { + return "b"; + }, + }); + + assertEquals(stringify(a), "b"); +}); + unitTest(function consoleTestWithIntegerFormatSpecifier(): void { assertEquals(stringify("%i"), "%i"); assertEquals(stringify("%i", 42.0), "42");