feat: Add deprecation warning for Deno.customInspect (#22027)

This change sets the removal version of `Deno.customInspect` for Deno
v2.

Towards #22021

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Asher Gomez 2024-01-24 08:46:59 +11:00 committed by GitHub
parent ee0cfd3fd5
commit 5aa25f08be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 17 deletions

View file

@ -5034,8 +5034,8 @@ declare namespace Deno {
* called when `Deno.inspect()` is called, or when the object is logged to
* the console.
*
* @deprecated This symbol is deprecated since 1.9. Use
* `Symbol.for("Deno.customInspect")` instead.
* @deprecated Use `Symbol.for("Deno.customInspect")` instead. This symbol
* will be removed in Deno 2.0.
*
* @category Console and Debugging
*/

View file

@ -83,9 +83,6 @@ const denoNs = {
futime: fs.futime,
futimeSync: fs.futimeSync,
errors: errors.errors,
// TODO(kt3k): Remove this export at v2
// See https://github.com/denoland/deno/issues/9294
customInspect: console.customInspect,
inspect: console.inspect,
env: os.env,
exit: os.exit,

View file

@ -43,6 +43,7 @@ import * as version from "ext:runtime/01_version.ts";
import * as os from "ext:runtime/30_os.js";
import * as timers from "ext:deno_web/02_timers.js";
import {
customInspect,
getDefaultInspectOptions,
getNoColor,
inspectArgs,
@ -111,12 +112,13 @@ function warnOnDeprecatedApi(apiName, stack, suggestion) {
// to make it more useful.
const stackLines = StringPrototypeSplit(stack, "\n");
ArrayPrototypeShift(stackLines);
while (true) {
while (stackLines.length > 0) {
// Filter out internal frames at the top of the stack - they are not useful
// to the user.
if (
StringPrototypeIncludes(stackLines[0], "(ext:") ||
StringPrototypeIncludes(stackLines[0], "(node:")
StringPrototypeIncludes(stackLines[0], "(node:") ||
StringPrototypeIncludes(stackLines[0], "<anonymous>")
) {
ArrayPrototypeShift(stackLines);
} else {
@ -127,6 +129,7 @@ function warnOnDeprecatedApi(apiName, stack, suggestion) {
// event loop tick or promise handler calling a user function - again not
// useful to the user.
if (
stackLines.length > 0 &&
StringPrototypeIncludes(stackLines[stackLines.length - 1], "(ext:core/")
) {
ArrayPrototypePop(stackLines);
@ -168,16 +171,17 @@ function warnOnDeprecatedApi(apiName, stack, suggestion) {
"color: yellow;",
);
}
console.error("%c\u2502", "color: yellow;");
console.error("%c\u2514 Stack trace:", "color: yellow;");
for (let i = 0; i < stackLines.length; i++) {
console.error(
`%c ${i == stackLines.length - 1 ? "\u2514" : "\u251c"}\u2500 ${
StringPrototypeTrim(stackLines[i])
}`,
"color: yellow;",
);
if (stackLines.length > 0) {
console.error("%c\u2502", "color: yellow;");
console.error("%c\u2514 Stack trace:", "color: yellow;");
for (let i = 0; i < stackLines.length; i++) {
console.error(
`%c ${i == stackLines.length - 1 ? "\u2514" : "\u251c"}\u2500 ${
StringPrototypeTrim(stackLines[i])
}`,
"color: yellow;",
);
}
}
console.error();
}
@ -626,6 +630,18 @@ function bootstrapMainRuntime(runtimeOptions) {
noColor: util.getterOnly(() => ops.op_bootstrap_no_color()),
args: util.getterOnly(opArgs),
mainModule: util.getterOnly(opMainModule),
// TODO(kt3k): Remove this export at v2
// See https://github.com/denoland/deno/issues/9294
customInspect: {
get() {
warnOnDeprecatedApi(
"Deno.customInspect",
new Error().stack,
'Use `Symbol.for("Deno.customInspect")` instead.',
);
return customInspect;
},
},
});
// TODO(bartlomieju): deprecate --unstable
@ -769,6 +785,18 @@ function bootstrapWorkerRuntime(
pid: util.getterOnly(opPid),
noColor: util.getterOnly(() => ops.op_bootstrap_no_color()),
args: util.getterOnly(opArgs),
// TODO(kt3k): Remove this export at v2
// See https://github.com/denoland/deno/issues/9294
customInspect: {
get() {
warnOnDeprecatedApi(
"Deno.customInspect",
new Error().stack,
'Use `Symbol.for("Deno.customInspect")` instead.',
);
return customInspect;
},
},
});
// Setup `Deno` global - we're actually overriding already
// existing global `Deno` with `Deno` namespace from "./deno.ts".