From 49bf45249b0982ac3f17ccdbae4667593b1c53c4 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 30 May 2023 13:29:18 -0400 Subject: [PATCH 1/3] feat(runtime): log deprecated message on first usage of `window` global --- cli/tests/integration/run_tests.rs | 10 ++++++++++ .../run/window_global_deprecated/main.out | 4 ++++ .../run/window_global_deprecated/main.ts | 2 ++ runtime/js/06_util.js | 11 +++++++++-- runtime/js/11_workers.js | 4 ++-- runtime/js/98_global_scope.js | 16 +++++++++++++++- runtime/js/99_main.js | 2 +- 7 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 cli/tests/testdata/run/window_global_deprecated/main.out create mode 100644 cli/tests/testdata/run/window_global_deprecated/main.ts diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 31b541e1c5e18b..4455b68d8413c8 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -2931,6 +2931,16 @@ itest!(delete_window { output_str: Some("true\n"), }); +itest!(window_global_deprecated { + args: "run run/window_global_deprecated/main.ts", + output: "run/window_global_deprecated/main.out", +}); + +itest!(window_global_deprecated_quiet { + args: "run --quiet run/window_global_deprecated/main.ts", + output_str: Some("undefined\nundefined\n"), +}); + itest!(colors_without_global_this { args: "run run/colors_without_globalThis.js", output_str: Some("true\n"), diff --git a/cli/tests/testdata/run/window_global_deprecated/main.out b/cli/tests/testdata/run/window_global_deprecated/main.out new file mode 100644 index 00000000000000..915edf1c9c5e96 --- /dev/null +++ b/cli/tests/testdata/run/window_global_deprecated/main.out @@ -0,0 +1,4 @@ +[deprecated] The `window` global will be removed in Deno 2.0 (https://github.com/denoland/deno/issues/13367). Use `globalThis` instead. + at file:///[WILDCARD]/testdata/run/window_global_deprecated/main.ts:1:13 +undefined +undefined diff --git a/cli/tests/testdata/run/window_global_deprecated/main.ts b/cli/tests/testdata/run/window_global_deprecated/main.ts new file mode 100644 index 00000000000000..54898318e2d165 --- /dev/null +++ b/cli/tests/testdata/run/window_global_deprecated/main.ts @@ -0,0 +1,2 @@ +console.log(window.location); +console.log(window.location); diff --git a/runtime/js/06_util.js b/runtime/js/06_util.js index 971957b7ec9eda..66f8597338623f 100644 --- a/runtime/js/06_util.js +++ b/runtime/js/06_util.js @@ -24,7 +24,7 @@ function setLogLevel(level, source) { } } -function log(...args) { +function logDebug(...args) { if (logLevel >= LogLevel.Debug) { // if we destructure `console` off `globalThis` too early, we don't bind to // the right console, therefore we don't log anything out. @@ -35,6 +35,12 @@ function log(...args) { } } +function logWarn(...args) { + if (logLevel >= LogLevel.Warn) { + globalThis.console.warn(...new SafeArrayIterator(args)); + } +} + function createResolvable() { let resolve; let reject; @@ -86,7 +92,8 @@ function getterOnly(getter) { export { createResolvable, getterOnly, - log, + logDebug, + logWarn, nonEnumerable, readOnly, setLogLevel, diff --git a/runtime/js/11_workers.js b/runtime/js/11_workers.js index e04690005343e0..89fc040df50a1f 100644 --- a/runtime/js/11_workers.js +++ b/runtime/js/11_workers.js @@ -16,7 +16,7 @@ import * as webidl from "ext:deno_webidl/00_webidl.js"; import { URL } from "ext:deno_url/00_url.js"; import { getLocationHref } from "ext:deno_web/12_location.js"; import { serializePermissions } from "ext:runtime/10_permissions.js"; -import { log } from "ext:runtime/06_util.js"; +import { logDebug } from "ext:runtime/06_util.js"; import { defineEventHandler, ErrorEvent, @@ -160,7 +160,7 @@ class Worker extends EventTarget { break; } case 3: { // Close - log(`Host got "close" message from worker: ${this.#name}`); + logDebug(`Host got "close" message from worker: ${this.#name}`); this.#status = "CLOSED"; return; } diff --git a/runtime/js/98_global_scope.js b/runtime/js/98_global_scope.js index 1084f5c2482965..b89e19d317cc84 100644 --- a/runtime/js/98_global_scope.js +++ b/runtime/js/98_global_scope.js @@ -245,11 +245,25 @@ ObjectDefineProperties(WorkerNavigator.prototype, { }); const WorkerNavigatorPrototype = WorkerNavigator.prototype; +let hasLoggedWindow = false; + const mainRuntimeGlobalProperties = { Location: location.locationConstructorDescriptor, location: location.locationDescriptor, Window: globalInterfaces.windowConstructorDescriptor, - window: util.getterOnly(() => globalThis), + window: util.getterOnly(() => { + if (!hasLoggedWindow) { + hasLoggedWindow = true; + const stack = new Error().stack; + util.logWarn( + "%c[deprecated] The `window` global will be removed in Deno 2.0 (https://github.com/denoland/deno/issues/13367). Use `globalThis` instead." + + (stack == null ? "" : `\n${stack.split("\n").slice(2)}`), + "color: yellow", + ); + } + + return globalThis; + }), self: util.getterOnly(() => globalThis), Navigator: util.nonEnumerable(Navigator), navigator: util.getterOnly(() => navigator), diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 8fd9a6bd94444d..550faad10a700a 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -527,7 +527,7 @@ function bootstrapMainRuntime(runtimeOptions) { // `Deno` with `Deno` namespace from "./deno.ts". ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs)); - util.log("args", args); + util.logDebug("args", args); } function bootstrapWorkerRuntime( From b9a6757517888ea85e1bc615065f18c3ec7f54ca Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 30 May 2023 13:29:57 -0400 Subject: [PATCH 2/3] Run [ci] From d83ff6d978c4b25b02a5cf1e5240a62f1c617629 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 26 Jun 2023 14:32:24 -0400 Subject: [PATCH 3/3] Lint. --- runtime/js/98_global_scope.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/runtime/js/98_global_scope.js b/runtime/js/98_global_scope.js index b89e19d317cc84..c80bad3bf627fb 100644 --- a/runtime/js/98_global_scope.js +++ b/runtime/js/98_global_scope.js @@ -3,7 +3,9 @@ const core = globalThis.Deno.core; const primordials = globalThis.__bootstrap.primordials; const { + ArraySplit, ObjectDefineProperties, + StringSlice, SymbolFor, } = primordials; @@ -257,7 +259,7 @@ const mainRuntimeGlobalProperties = { const stack = new Error().stack; util.logWarn( "%c[deprecated] The `window` global will be removed in Deno 2.0 (https://github.com/denoland/deno/issues/13367). Use `globalThis` instead." + - (stack == null ? "" : `\n${stack.split("\n").slice(2)}`), + (stack == null ? "" : `\n${StringSlice(ArraySplit(stack, "\n"), 2)}`), "color: yellow", ); }