0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

chore(ext/console): deprecate Deno.customInspect (#10035)

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Yoshiya Hinosawa 2021-06-25 16:19:18 +09:00 committed by GitHub
parent 606611708c
commit d832d2bfd1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 76 additions and 58 deletions

View file

@ -2193,14 +2193,14 @@ declare namespace Deno {
* console.log(obj); // prints same value as objAsString, e.g. { a: 10, b: "hello" } * 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 * You can also register custom inspect functions, via the symbol `Symbol.for("Deno.customInspect")`,
* symbol on objects, to control and customize the output. * on objects, to control and customize the output.
* *
* ```ts * ```ts
* class A { * class A {
* x = 10; * x = 10;
* y = "hello"; * y = "hello";
* [Deno.customInspect](): string { * [Symbol.for("Deno.customInspect")](): string {
* return "x=" + this.x + ", y=" + this.y; * return "x=" + this.x + ", y=" + this.y;
* } * }
* } * }
@ -2388,9 +2388,13 @@ declare namespace Deno {
*/ */
export const args: string[]; 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 * 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; export const customInspect: unique symbol;
/** The URL of the entrypoint module entered from the command-line. */ /** The URL of the entrypoint module entered from the command-line. */

View file

@ -17,7 +17,7 @@ import {
} from "./test_util.ts"; } from "./test_util.ts";
import { stripColor } from "../../../test_util/std/fmt/colors.ts"; import { stripColor } from "../../../test_util/std/fmt/colors.ts";
const customInspect = Deno.customInspect; const customInspect = Symbol.for("Deno.customInspect");
const { const {
Console, Console,
cssToAnsi: cssToAnsi_, cssToAnsi: cssToAnsi_,
@ -879,6 +879,18 @@ unitTest(function consoleTestWithCustomInspector(): void {
assertEquals(stringify(new A()), "b"); 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 { unitTest(function consoleTestWithCustomInspectorError(): void {
class A { class A {
[customInspect](): never { [customInspect](): never {

View file

@ -203,7 +203,7 @@
function inspectFunction(value, level, inspectOptions) { function inspectFunction(value, level, inspectOptions) {
const cyan = maybeColor(colors.cyan, inspectOptions); const cyan = maybeColor(colors.cyan, inspectOptions);
if (customInspect in value && typeof value[customInspect] === "function") { if (customInspect in value && typeof value[customInspect] === "function") {
return String(value[customInspect]()); return String(value[customInspect](inspect));
} }
// Might be Function/AsyncFunction/GeneratorFunction/AsyncGeneratorFunction // Might be Function/AsyncFunction/GeneratorFunction/AsyncGeneratorFunction
let cstrName = Object.getPrototypeOf(value)?.constructor?.name; let cstrName = Object.getPrototypeOf(value)?.constructor?.name;
@ -901,22 +901,22 @@
inspectOptions, inspectOptions,
) { ) {
if (customInspect in value && typeof value[customInspect] === "function") { 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. // This non-unique symbol is used to support op_crates, ie.
// in extensions/web we don't want to depend on unique "Deno.customInspect" // in extensions/web we don't want to depend on public
// symbol defined in the public API. Internal only, shouldn't be used // Symbol.for("Deno.customInspect") symbol defined in the public API.
// by users. // Internal only, shouldn't be used by users.
const nonUniqueCustomInspect = Symbol.for("Deno.customInspect"); const privateCustomInspect = Symbol.for("Deno.privateCustomInspect");
if ( if (
nonUniqueCustomInspect in value && privateCustomInspect in value &&
typeof value[nonUniqueCustomInspect] === "function" typeof value[privateCustomInspect] === "function"
) { ) {
// TODO(nayeemrmn): `inspect` is passed as an argument because custom // TODO(nayeemrmn): `inspect` is passed as an argument because custom
// inspect implementations in `extensions` need it, but may not have access // inspect implementations in `extensions` need it, but may not have access
// to the `Deno` namespace in web workers. Remove when the `Deno` // to the `Deno` namespace in web workers. Remove when the `Deno`
// namespace is always enabled. // namespace is always enabled.
return String(value[nonUniqueCustomInspect](inspect)); return String(value[privateCustomInspect](inspect));
} }
if (value instanceof Error) { if (value instanceof Error) {
return String(value.stack); return String(value.stack);
@ -1760,7 +1760,7 @@
} }
} }
const customInspect = Symbol("Deno.customInspect"); const customInspect = Symbol.for("Deno.customInspect");
function inspect( function inspect(
value, value,

View file

@ -370,7 +370,7 @@
} }
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
const headers = {}; const headers = {};
for (const header of this) { for (const header of this) {
headers[header[0]] = header[1]; headers[header[0]] = header[1];

View file

@ -222,7 +222,7 @@
this[_url] = parts; this[_url] = parts;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
const object = { const object = {
href: this.href, href: this.href,
origin: this.origin, origin: this.origin,

View file

@ -143,7 +143,7 @@
}); });
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return inspect(buildFilteredPropertyInspectObject(this, EVENT_PROPS)); return inspect(buildFilteredPropertyInspectObject(this, EVENT_PROPS));
} }
@ -1055,7 +1055,7 @@
return "ErrorEvent"; return "ErrorEvent";
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return inspect(buildFilteredPropertyInspectObject(this, [ return inspect(buildFilteredPropertyInspectObject(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"message", "message",
@ -1109,7 +1109,7 @@
this.#reason = reason; this.#reason = reason;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return inspect(buildFilteredPropertyInspectObject(this, [ return inspect(buildFilteredPropertyInspectObject(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"wasClean", "wasClean",
@ -1137,7 +1137,7 @@
this.lastEventId = eventInitDict?.lastEventId ?? ""; this.lastEventId = eventInitDict?.lastEventId ?? "";
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return inspect(buildFilteredPropertyInspectObject(this, [ return inspect(buildFilteredPropertyInspectObject(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"data", "data",
@ -1167,7 +1167,7 @@
return "CustomEvent"; return "CustomEvent";
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return inspect(buildFilteredPropertyInspectObject(this, [ return inspect(buildFilteredPropertyInspectObject(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"detail", "detail",
@ -1190,7 +1190,7 @@
this.total = eventInitDict?.total ?? 0; this.total = eventInitDict?.total ?? 0;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return inspect(buildFilteredPropertyInspectObject(this, [ return inspect(buildFilteredPropertyInspectObject(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"lengthComputable", "lengthComputable",

View file

@ -3304,7 +3304,7 @@
return iterator; return iterator;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${inspect({ locked: this.locked })}`; return `${this.constructor.name} ${inspect({ locked: this.locked })}`;
} }
@ -3424,7 +3424,7 @@
return readableStreamReaderGenericCancel(this, reason); return readableStreamReaderGenericCancel(this, reason);
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${inspect({ closed: this.closed })}`; return `${this.constructor.name} ${inspect({ closed: this.closed })}`;
} }
@ -3821,7 +3821,7 @@
return this[_writable]; return this[_writable];
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ readable: this.readable, writable: this.writable }) inspect({ readable: this.readable, writable: this.writable })
}`; }`;
@ -4022,7 +4022,7 @@
return acquireWritableStreamDefaultWriter(this); return acquireWritableStreamDefaultWriter(this);
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${inspect({ locked: this.locked })}`; return `${this.constructor.name} ${inspect({ locked: this.locked })}`;
} }

View file

@ -165,7 +165,7 @@
}, },
enumerable: true, enumerable: true,
}, },
[Symbol.for("Deno.customInspect")]: { [Symbol.for("Deno.privateCustomInspect")]: {
value: function (inspect) { value: function (inspect) {
const object = { const object = {
hash: this.hash, hash: this.hash,
@ -322,7 +322,7 @@
value: "WorkerLocation", value: "WorkerLocation",
configurable: true, configurable: true,
}, },
[Symbol.for("Deno.customInspect")]: { [Symbol.for("Deno.privateCustomInspect")]: {
value: function (inspect) { value: function (inspect) {
const object = { const object = {
hash: this.hash, hash: this.hash,

View file

@ -167,7 +167,7 @@
} }
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${inspect({})}`; return `${this.constructor.name} ${inspect({})}`;
} }
} }
@ -271,7 +271,7 @@
); );
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
name: this.name, name: this.name,
@ -388,7 +388,7 @@
return this[_limits].maxVertexBufferArrayStride; return this[_limits].maxVertexBufferArrayStride;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${inspect(this[_limits])}`; return `${this.constructor.name} ${inspect(this[_limits])}`;
} }
} }
@ -451,7 +451,7 @@
return this[_features][Symbol.iterator](); return this[_features][Symbol.iterator]();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${inspect([...this.values()])}`; return `${this.constructor.name} ${inspect([...this.values()])}`;
} }
} }
@ -492,7 +492,7 @@
return this[_message]; return this[_message];
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ reason: this[_reason], message: this[_message] }) inspect({ reason: this[_reason], message: this[_message] })
}`; }`;
@ -1292,7 +1292,7 @@
return scope.error ?? null; return scope.error ?? null;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
features: this.features, features: this.features,
@ -1478,7 +1478,7 @@
throw new Error("Not yet implemented"); throw new Error("Not yet implemented");
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -1793,7 +1793,7 @@
this[_cleanup](); this[_cleanup]();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -1932,7 +1932,7 @@
this[_cleanup](); this[_cleanup]();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -1999,7 +1999,7 @@
webidl.illegalConstructor(); webidl.illegalConstructor();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -2042,7 +2042,7 @@
webidl.illegalConstructor(); webidl.illegalConstructor();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -2085,7 +2085,7 @@
webidl.illegalConstructor(); webidl.illegalConstructor();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -2128,7 +2128,7 @@
webidl.illegalConstructor(); webidl.illegalConstructor();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -2171,7 +2171,7 @@
webidl.illegalConstructor(); webidl.illegalConstructor();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -2218,7 +2218,7 @@
throw new Error("Not yet implemented"); throw new Error("Not yet implemented");
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -2312,7 +2312,7 @@
return bindGroupLayout; return bindGroupLayout;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -2387,7 +2387,7 @@
return bindGroupLayout; return bindGroupLayout;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -3150,7 +3150,7 @@
return commandBuffer; return commandBuffer;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -3966,7 +3966,7 @@
}); });
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -4369,7 +4369,7 @@
}); });
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -4417,7 +4417,7 @@
throw new Error("Not yet implemented"); throw new Error("Not yet implemented");
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -4873,7 +4873,7 @@
throw new Error("Not yet implemented"); throw new Error("Not yet implemented");
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -4917,7 +4917,7 @@
webidl.illegalConstructor(); webidl.illegalConstructor();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,
@ -4971,7 +4971,7 @@
this[_cleanup](); this[_cleanup]();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ inspect({
label: this.label, label: this.label,

View file

@ -87,7 +87,7 @@
return dispatched; return dispatched;
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
inspect({ state: this.state, onchange: this.onchange }) inspect({ state: this.state, onchange: this.onchange })
}`; }`;

View file

@ -53,6 +53,8 @@
ftruncateSync: __bootstrap.fs.ftruncateSync, ftruncateSync: __bootstrap.fs.ftruncateSync,
ftruncate: __bootstrap.fs.ftruncate, ftruncate: __bootstrap.fs.ftruncate,
errors: __bootstrap.errors.errors, errors: __bootstrap.errors.errors,
// TODO(kt3k): Remove this export at v2
// See https://github.com/denoland/deno/issues/9294
customInspect: __bootstrap.console.customInspect, customInspect: __bootstrap.console.customInspect,
inspect: __bootstrap.console.inspect, inspect: __bootstrap.console.inspect,
env: __bootstrap.os.env, env: __bootstrap.os.env,

View file

@ -247,7 +247,7 @@ delete Object.prototype.__proto__;
webidl.illegalConstructor(); webidl.illegalConstructor();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${inspect({})}`; return `${this.constructor.name} ${inspect({})}`;
} }
} }
@ -270,7 +270,7 @@ delete Object.prototype.__proto__;
webidl.illegalConstructor(); webidl.illegalConstructor();
} }
[Symbol.for("Deno.customInspect")](inspect) { [Symbol.for("Deno.privateCustomInspect")](inspect) {
return `${this.constructor.name} ${inspect({})}`; return `${this.constructor.name} ${inspect({})}`;
} }
} }