Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): log deprecated message on first usage of window global #19319

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cli/tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
4 changes: 4 additions & 0 deletions cli/tests/testdata/run/window_global_deprecated/main.out
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions cli/tests/testdata/run/window_global_deprecated/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log(window.location);
console.log(window.location);
11 changes: 9 additions & 2 deletions runtime/js/06_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -86,7 +92,8 @@ function getterOnly(getter) {
export {
createResolvable,
getterOnly,
log,
logDebug,
logWarn,
nonEnumerable,
readOnly,
setLogLevel,
Expand Down
4 changes: 2 additions & 2 deletions runtime/js/11_workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 15 additions & 1 deletion runtime/js/98_global_scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down