fix(op_crates): Don't use Deno.inspect in op crates (#9332)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Nayeem Rahman 2021-02-09 15:31:46 +00:00 committed by GitHub
parent 47b3e4bada
commit 900953a65a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 48 additions and 63 deletions

View file

@ -0,0 +1,4 @@
new Worker(
new URL("084_worker_custom_inspect_worker.ts", import.meta.url).href,
{ type: "module" },
);

View file

@ -0,0 +1,2 @@
[WILDCARD]ReadableStream { locked: false }
[WILDCARD]

View file

@ -0,0 +1,2 @@
console.log(new ReadableStream());
close();

View file

@ -2685,6 +2685,11 @@ console.log("finish");
assert_eq!(out, ""); assert_eq!(out, "");
} }
itest!(_084_worker_custom_inspect {
args: "run --allow-read 084_worker_custom_inspect.ts",
output: "084_worker_custom_inspect.ts.out",
});
itest!(js_import_detect { itest!(js_import_detect {
args: "run --quiet --reload js_import_detect.ts", args: "run --quiet --reload js_import_detect.ts",
output: "js_import_detect.ts.out", output: "js_import_detect.ts.out",

View file

@ -124,9 +124,6 @@ unitTest(function eventInspectOutput(): void {
]; ];
for (const [event, outputProvider] of cases) { for (const [event, outputProvider] of cases) {
assertEquals( assertEquals(Deno.inspect(event), outputProvider(event));
event[Symbol.for("Deno.customInspect")](),
outputProvider(event),
);
} }
}); });

View file

@ -7,8 +7,6 @@
"use strict"; "use strict";
((window) => { ((window) => {
const customInspect = Symbol.for("Deno.customInspect");
class AssertionError extends Error { class AssertionError extends Error {
constructor(msg) { constructor(msg) {
super(msg); super(msg);
@ -3224,10 +3222,8 @@
return iterator; return iterator;
} }
[customInspect]() { [Symbol.for("Deno.customInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${inspect({ locked: this.locked })}`;
Deno.inspect({ locked: this.locked })
}`;
} }
} }
@ -3308,8 +3304,8 @@
readableStreamReaderGenericRelease(this); readableStreamReaderGenericRelease(this);
} }
[customInspect]() { [Symbol.for("Deno.customInspect")](inspect) {
return `${this.constructor.name} { closed: ${String(this.closed)} }`; return `${this.constructor.name} ${inspect({ closed: this.closed })}`;
} }
} }
@ -3597,12 +3593,9 @@
return this[_writable]; return this[_writable];
} }
[customInspect]() { [Symbol.for("Deno.customInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${
Deno.inspect( inspect({ readable: this.readable, writable: this.writable })
{ readable: this.readable, writable: this.writable },
{ depth: 1 },
)
}`; }`;
} }
} }
@ -3736,10 +3729,8 @@
return acquireWritableStreamDefaultWriter(this); return acquireWritableStreamDefaultWriter(this);
} }
[customInspect]() { [Symbol.for("Deno.customInspect")](inspect) {
return `${this.constructor.name} ${ return `${this.constructor.name} ${inspect({ locked: this.locked })}`;
Deno.inspect({ locked: this.locked })
}`;
} }
} }

View file

@ -143,8 +143,8 @@
}); });
} }
[Symbol.for("Deno.customInspect")]() { [Symbol.for("Deno.customInspect")](inspect) {
return buildCustomInspectOutput(this, EVENT_PROPS); return buildCustomInspectOutput(this, EVENT_PROPS, inspect);
} }
get bubbles() { get bubbles() {
@ -387,14 +387,9 @@
} }
} }
function buildCustomInspectOutput(obj, props) { function buildCustomInspectOutput(object, keys, inspect) {
const inspectObj = {}; const inspectObject = Object.fromEntries(keys.map((k) => [k, object[k]]));
return `${object.constructor.name} ${inspect(inspectObject)}`;
for (const prop of props) {
inspectObj[prop] = obj[prop];
}
return `${obj.constructor.name} ${Deno.inspect(inspectObj)}`;
} }
function defineEnumerableProps( function defineEnumerableProps(
@ -1041,7 +1036,7 @@
return "ErrorEvent"; return "ErrorEvent";
} }
[Symbol.for("Deno.customInspect")]() { [Symbol.for("Deno.customInspect")](inspect) {
return buildCustomInspectOutput(this, [ return buildCustomInspectOutput(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"message", "message",
@ -1049,7 +1044,7 @@
"lineno", "lineno",
"colno", "colno",
"error", "error",
]); ], inspect);
} }
} }
@ -1095,13 +1090,13 @@
this.#reason = reason; this.#reason = reason;
} }
[Symbol.for("Deno.customInspect")]() { [Symbol.for("Deno.customInspect")](inspect) {
return buildCustomInspectOutput(this, [ return buildCustomInspectOutput(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"wasClean", "wasClean",
"code", "code",
"reason", "reason",
]); ], inspect);
} }
} }
@ -1118,13 +1113,13 @@
this.lastEventId = eventInitDict?.lastEventId ?? ""; this.lastEventId = eventInitDict?.lastEventId ?? "";
} }
[Symbol.for("Deno.customInspect")]() { [Symbol.for("Deno.customInspect")](inspect) {
return buildCustomInspectOutput(this, [ return buildCustomInspectOutput(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"data", "data",
"origin", "origin",
"lastEventId", "lastEventId",
]); ], inspect);
} }
} }
@ -1146,11 +1141,11 @@
return "CustomEvent"; return "CustomEvent";
} }
[Symbol.for("Deno.customInspect")]() { [Symbol.for("Deno.customInspect")](inspect) {
return buildCustomInspectOutput(this, [ return buildCustomInspectOutput(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"detail", "detail",
]); ], inspect);
} }
} }
@ -1169,13 +1164,13 @@
this.total = eventInitDict?.total ?? 0; this.total = eventInitDict?.total ?? 0;
} }
[Symbol.for("Deno.customInspect")]() { [Symbol.for("Deno.customInspect")](inspect) {
return buildCustomInspectOutput(this, [ return buildCustomInspectOutput(this, [
...EVENT_PROPS, ...EVENT_PROPS,
"lengthComputable", "lengthComputable",
"loaded", "loaded",
"total", "total",
]); ], inspect);
} }
} }

View file

@ -522,7 +522,7 @@
class URL { class URL {
#searchParams = null; #searchParams = null;
[Symbol.for("Deno.customInspect")]() { [Symbol.for("Deno.customInspect")](inspect) {
const object = { const object = {
href: this.href, href: this.href,
origin: this.origin, origin: this.origin,
@ -536,12 +536,7 @@
hash: this.hash, hash: this.hash,
search: this.search, search: this.search,
}; };
if (typeof globalThis?.Deno?.inspect == "function") { return `${this.constructor.name} ${inspect(object)}`;
return `URL ${Deno.inspect(object)}`;
}
return `URL { ${
Object.entries(object).map(([k, v]) => `${k}: ${v}`).join(", ")
} }`;
} }
#updateSearchParams = () => { #updateSearchParams = () => {

View file

@ -166,7 +166,7 @@
enumerable: true, enumerable: true,
}, },
[Symbol.for("Deno.customInspect")]: { [Symbol.for("Deno.customInspect")]: {
value: function () { value: function (inspect) {
const object = { const object = {
hash: this.hash, hash: this.hash,
host: this.host, host: this.host,
@ -178,12 +178,7 @@
protocol: this.protocol, protocol: this.protocol,
search: this.search, search: this.search,
}; };
if (typeof globalThis?.Deno?.inspect == "function") { return `${this.constructor.name} ${inspect(object)}`;
return `Location ${Deno.inspect(object)}`;
}
return `Location { ${
Object.entries(object).map(([k, v]) => `${k}: ${v}`).join(", ")
} }`;
}, },
}, },
}); });
@ -328,7 +323,7 @@
configurable: true, configurable: true,
}, },
[Symbol.for("Deno.customInspect")]: { [Symbol.for("Deno.customInspect")]: {
value: function () { value: function (inspect) {
const object = { const object = {
hash: this.hash, hash: this.hash,
host: this.host, host: this.host,
@ -340,12 +335,7 @@
protocol: this.protocol, protocol: this.protocol,
search: this.search, search: this.search,
}; };
if (typeof globalThis?.Deno?.inspect == "function") { return `${this.constructor.name} ${inspect(object)}`;
return `WorkerLocation ${Deno.inspect(object)}`;
}
return `WorkerLocation { ${
Object.entries(object).map(([k, v]) => `${k}: ${v}`).join(", ")
} }`;
}, },
}, },
}); });

View file

@ -873,7 +873,11 @@
nonUniqueCustomInspect in value && nonUniqueCustomInspect in value &&
typeof value[nonUniqueCustomInspect] === "function" typeof value[nonUniqueCustomInspect] === "function"
) { ) {
return String(value[nonUniqueCustomInspect]()); // TODO(nayeemrmn): `inspect` is passed as an argument because custom
// inspect implementations in `op_crates` 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));
} }
if (value instanceof Error) { if (value instanceof Error) {
return String(value.stack); return String(value.stack);