diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index da016db8ae..e838585865 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -2193,14 +2193,14 @@ declare namespace Deno { * console.log(obj); // prints same value as objAsString, e.g. { a: 10, b: "hello" } * ``` * - * You can also register custom inspect functions, via the `customInspect` Deno - * symbol on objects, to control and customize the output. + * You can also register custom inspect functions, via the symbol `Symbol.for("Deno.customInspect")`, + * on objects, to control and customize the output. * * ```ts * class A { * x = 10; * y = "hello"; - * [Deno.customInspect](): string { + * [Symbol.for("Deno.customInspect")](): string { * return "x=" + this.x + ", y=" + this.y; * } * } @@ -2388,9 +2388,13 @@ declare namespace Deno { */ export const args: string[]; - /** A symbol which can be used as a key for a custom method which will be + /** + * @deprecated A symbol which can be used as a key for a custom method which will be * called when `Deno.inspect()` is called, or when the object is logged to - * the console. */ + * the console. + * + * This symbol is deprecated since 1.9. Use `Symbol.for("Deno.customInspect")` instead. + */ export const customInspect: unique symbol; /** The URL of the entrypoint module entered from the command-line. */ diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index 8013da1529..edb1b245f9 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -17,7 +17,7 @@ import { } from "./test_util.ts"; import { stripColor } from "../../../test_util/std/fmt/colors.ts"; -const customInspect = Deno.customInspect; +const customInspect = Symbol.for("Deno.customInspect"); const { Console, cssToAnsi: cssToAnsi_, @@ -879,6 +879,18 @@ unitTest(function consoleTestWithCustomInspector(): void { assertEquals(stringify(new A()), "b"); }); +unitTest(function consoleTestWithCustomInspectorUsingInspectFunc(): void { + class A { + [customInspect]( + inspect: (v: unknown, opts?: Deno.InspectOptions) => string, + ): string { + return "b " + inspect({ c: 1 }); + } + } + + assertEquals(stringify(new A()), "b { c: 1 }"); +}); + unitTest(function consoleTestWithCustomInspectorError(): void { class A { [customInspect](): never { diff --git a/extensions/console/02_console.js b/extensions/console/02_console.js index 3391b520b4..ddfcbf47a5 100644 --- a/extensions/console/02_console.js +++ b/extensions/console/02_console.js @@ -203,7 +203,7 @@ function inspectFunction(value, level, inspectOptions) { const cyan = maybeColor(colors.cyan, inspectOptions); if (customInspect in value && typeof value[customInspect] === "function") { - return String(value[customInspect]()); + return String(value[customInspect](inspect)); } // Might be Function/AsyncFunction/GeneratorFunction/AsyncGeneratorFunction let cstrName = Object.getPrototypeOf(value)?.constructor?.name; @@ -901,22 +901,22 @@ inspectOptions, ) { if (customInspect in value && typeof value[customInspect] === "function") { - return String(value[customInspect]()); + return String(value[customInspect](inspect)); } - // This non-unique symbol is used to support extensions, ie. - // in extensions/web we don't want to depend on unique "Deno.customInspect" - // symbol defined in the public API. Internal only, shouldn't be used - // by users. - const nonUniqueCustomInspect = Symbol.for("Deno.customInspect"); + // This non-unique symbol is used to support op_crates, ie. + // in extensions/web we don't want to depend on public + // Symbol.for("Deno.customInspect") symbol defined in the public API. + // Internal only, shouldn't be used by users. + const privateCustomInspect = Symbol.for("Deno.privateCustomInspect"); if ( - nonUniqueCustomInspect in value && - typeof value[nonUniqueCustomInspect] === "function" + privateCustomInspect in value && + typeof value[privateCustomInspect] === "function" ) { // TODO(nayeemrmn): `inspect` is passed as an argument because custom // inspect implementations in `extensions` need it, but may not have access // to the `Deno` namespace in web workers. Remove when the `Deno` // namespace is always enabled. - return String(value[nonUniqueCustomInspect](inspect)); + return String(value[privateCustomInspect](inspect)); } if (value instanceof Error) { return String(value.stack); @@ -1760,7 +1760,7 @@ } } - const customInspect = Symbol("Deno.customInspect"); + const customInspect = Symbol.for("Deno.customInspect"); function inspect( value, diff --git a/extensions/fetch/20_headers.js b/extensions/fetch/20_headers.js index 26500b602f..59f81a8ff9 100644 --- a/extensions/fetch/20_headers.js +++ b/extensions/fetch/20_headers.js @@ -370,7 +370,7 @@ } } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { const headers = {}; for (const header of this) { headers[header[0]] = header[1]; diff --git a/extensions/url/00_url.js b/extensions/url/00_url.js index 2daae8d453..7b1c6e06ab 100644 --- a/extensions/url/00_url.js +++ b/extensions/url/00_url.js @@ -222,7 +222,7 @@ this[_url] = parts; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { const object = { href: this.href, origin: this.origin, diff --git a/extensions/web/02_event.js b/extensions/web/02_event.js index 7a270881f0..081869efdf 100644 --- a/extensions/web/02_event.js +++ b/extensions/web/02_event.js @@ -143,7 +143,7 @@ }); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return inspect(buildFilteredPropertyInspectObject(this, EVENT_PROPS)); } @@ -1055,7 +1055,7 @@ return "ErrorEvent"; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return inspect(buildFilteredPropertyInspectObject(this, [ ...EVENT_PROPS, "message", @@ -1109,7 +1109,7 @@ this.#reason = reason; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return inspect(buildFilteredPropertyInspectObject(this, [ ...EVENT_PROPS, "wasClean", @@ -1137,7 +1137,7 @@ this.lastEventId = eventInitDict?.lastEventId ?? ""; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return inspect(buildFilteredPropertyInspectObject(this, [ ...EVENT_PROPS, "data", @@ -1167,7 +1167,7 @@ return "CustomEvent"; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return inspect(buildFilteredPropertyInspectObject(this, [ ...EVENT_PROPS, "detail", @@ -1190,7 +1190,7 @@ this.total = eventInitDict?.total ?? 0; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return inspect(buildFilteredPropertyInspectObject(this, [ ...EVENT_PROPS, "lengthComputable", diff --git a/extensions/web/06_streams.js b/extensions/web/06_streams.js index 127436d43a..d26a1a4126 100644 --- a/extensions/web/06_streams.js +++ b/extensions/web/06_streams.js @@ -3304,7 +3304,7 @@ return iterator; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect({ locked: this.locked })}`; } @@ -3424,7 +3424,7 @@ return readableStreamReaderGenericCancel(this, reason); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect({ closed: this.closed })}`; } @@ -3821,7 +3821,7 @@ return this[_writable]; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ readable: this.readable, writable: this.writable }) }`; @@ -4022,7 +4022,7 @@ return acquireWritableStreamDefaultWriter(this); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect({ locked: this.locked })}`; } diff --git a/extensions/web/12_location.js b/extensions/web/12_location.js index 2b6580f8be..75012396a9 100644 --- a/extensions/web/12_location.js +++ b/extensions/web/12_location.js @@ -165,7 +165,7 @@ }, enumerable: true, }, - [Symbol.for("Deno.customInspect")]: { + [Symbol.for("Deno.privateCustomInspect")]: { value: function (inspect) { const object = { hash: this.hash, @@ -322,7 +322,7 @@ value: "WorkerLocation", configurable: true, }, - [Symbol.for("Deno.customInspect")]: { + [Symbol.for("Deno.privateCustomInspect")]: { value: function (inspect) { const object = { hash: this.hash, diff --git a/extensions/webgpu/01_webgpu.js b/extensions/webgpu/01_webgpu.js index 23beaf1cc3..5ff6cde461 100644 --- a/extensions/webgpu/01_webgpu.js +++ b/extensions/webgpu/01_webgpu.js @@ -167,7 +167,7 @@ } } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect({})}`; } } @@ -271,7 +271,7 @@ ); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ name: this.name, @@ -388,7 +388,7 @@ return this[_limits].maxVertexBufferArrayStride; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect(this[_limits])}`; } } @@ -451,7 +451,7 @@ return this[_features][Symbol.iterator](); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect([...this.values()])}`; } } @@ -492,7 +492,7 @@ return this[_message]; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ reason: this[_reason], message: this[_message] }) }`; @@ -1292,7 +1292,7 @@ return scope.error ?? null; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ features: this.features, @@ -1478,7 +1478,7 @@ throw new Error("Not yet implemented"); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -1793,7 +1793,7 @@ this[_cleanup](); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -1932,7 +1932,7 @@ this[_cleanup](); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -1999,7 +1999,7 @@ webidl.illegalConstructor(); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -2042,7 +2042,7 @@ webidl.illegalConstructor(); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -2085,7 +2085,7 @@ webidl.illegalConstructor(); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -2128,7 +2128,7 @@ webidl.illegalConstructor(); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -2171,7 +2171,7 @@ webidl.illegalConstructor(); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -2218,7 +2218,7 @@ throw new Error("Not yet implemented"); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -2312,7 +2312,7 @@ return bindGroupLayout; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -2387,7 +2387,7 @@ return bindGroupLayout; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -3150,7 +3150,7 @@ return commandBuffer; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -3966,7 +3966,7 @@ }); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -4369,7 +4369,7 @@ }); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -4417,7 +4417,7 @@ throw new Error("Not yet implemented"); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -4873,7 +4873,7 @@ throw new Error("Not yet implemented"); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -4917,7 +4917,7 @@ webidl.illegalConstructor(); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, @@ -4971,7 +4971,7 @@ this[_cleanup](); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ label: this.label, diff --git a/runtime/js/40_permissions.js b/runtime/js/40_permissions.js index b36fb643a5..00b1a29741 100644 --- a/runtime/js/40_permissions.js +++ b/runtime/js/40_permissions.js @@ -87,7 +87,7 @@ return dispatched; } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect({ state: this.state, onchange: this.onchange }) }`; diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index 9d05908787..3d17fccf62 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -53,6 +53,8 @@ ftruncateSync: __bootstrap.fs.ftruncateSync, ftruncate: __bootstrap.fs.ftruncate, errors: __bootstrap.errors.errors, + // TODO(kt3k): Remove this export at v2 + // See https://github.com/denoland/deno/issues/9294 customInspect: __bootstrap.console.customInspect, inspect: __bootstrap.console.inspect, env: __bootstrap.os.env, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index d0e86bce7c..db334caeab 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -247,7 +247,7 @@ delete Object.prototype.__proto__; webidl.illegalConstructor(); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect({})}`; } } @@ -270,7 +270,7 @@ delete Object.prototype.__proto__; webidl.illegalConstructor(); } - [Symbol.for("Deno.customInspect")](inspect) { + [Symbol.for("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${inspect({})}`; } }