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: deprecate window global #22057

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions cli/tests/testdata/npm/compare_globals/main.out
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ false
true
true
true
[WILDCARD]
true
false
false
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/webstorage/logger.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log(window.localStorage);
console.log(globalThis.localStorage);
6 changes: 3 additions & 3 deletions cli/tests/testdata/run/webstorage/serialization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
window.sessionStorage.setItem("hello", "deno");
globalThis.sessionStorage.setItem("hello", "deno");

console.log(window.localStorage);
console.log(window.sessionStorage);
console.log(globalThis.localStorage);
console.log(globalThis.sessionStorage);
2 changes: 1 addition & 1 deletion cli/tests/testdata/run/webstorage/setter.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
window.localStorage.setItem("hello", "deno");
globalThis.localStorage.setItem("hello", "deno");
12 changes: 10 additions & 2 deletions runtime/js/98_global_scope_window.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { core, primordials } from "ext:core/mod.js";
import { core, internals, primordials } from "ext:core/mod.js";
const {
op_bootstrap_language,
op_bootstrap_numcpus,
Expand Down Expand Up @@ -108,7 +108,15 @@ const mainRuntimeGlobalProperties = {
Location: location.locationConstructorDescriptor,
location: location.locationDescriptor,
Window: globalInterfaces.windowConstructorDescriptor,
window: util.getterOnly(() => globalThis),
window: util.getterOnly(() => {
internals.warnOnDeprecatedApi(
"window",
new Error().stack,
"Use `globalThis` or `self` instead.",
"You can provide `window` in the current scope with: `const window = globalThis`.",
);
return globalThis;
}),
self: util.getterOnly(() => globalThis),
Navigator: util.nonEnumerable(Navigator),
navigator: util.getterOnly(() => navigator),
Expand Down
18 changes: 11 additions & 7 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ let globalThis_;
let deprecatedApiWarningDisabled = false;
const ALREADY_WARNED_DEPRECATED = new SafeSet();

function warnOnDeprecatedApi(apiName, stack, suggestion) {
function warnOnDeprecatedApi(apiName, stack, ...suggestions) {
if (deprecatedApiWarningDisabled) {
return;
}
Expand All @@ -116,7 +116,8 @@ function warnOnDeprecatedApi(apiName, stack, suggestion) {
// to the user.
if (
StringPrototypeIncludes(stackLines[0], "(ext:") ||
StringPrototypeIncludes(stackLines[0], "(node:")
StringPrototypeIncludes(stackLines[0], "(node:") ||
StringPrototypeIncludes(stackLines[0], "<anonymous>")
) {
ArrayPrototypeShift(stackLines);
} else {
Expand Down Expand Up @@ -152,11 +153,14 @@ function warnOnDeprecatedApi(apiName, stack, suggestion) {
"%c\u251c This API will be removed in Deno 2.0. Make sure to upgrade to a stable API before then.",
"color: yellow;",
);
console.error("%c\u2502", "color: yellow;");
console.error(
`%c\u251c Suggestion: ${suggestion}`,
"color: yellow;",
);
for (let i = 0; i < suggestions.length; i++) {
const suggestion = suggestions[i];
console.error("%c\u2502", "color: yellow;");
console.error(
`%c\u251c Suggestion: ${suggestion}`,
"color: yellow;",
);
}
if (isFromRemoteDependency) {
console.error("%c\u2502", "color: yellow;");
console.error(
Expand Down