From 5aa25f08be0be6c097dd35cd5ea2c8c154b06929 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 24 Jan 2024 08:46:59 +1100 Subject: [PATCH] feat: Add deprecation warning for `Deno.customInspect` (#22027) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change sets the removal version of `Deno.customInspect` for Deno v2. Towards #22021 --------- Co-authored-by: Bartek IwaƄczuk --- cli/tsc/dts/lib.deno.ns.d.ts | 4 +-- runtime/js/90_deno_ns.js | 3 --- runtime/js/99_main.js | 52 +++++++++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts index f695a68c29..8d1e28d4be 100644 --- a/cli/tsc/dts/lib.deno.ns.d.ts +++ b/cli/tsc/dts/lib.deno.ns.d.ts @@ -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 */ diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index e404d34d7c..25ba2ef26a 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -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, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 54a4406b2e..a038de5184 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -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], "") ) { 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".