Skip to content

Commit

Permalink
feat: Show hints when using window global (#25805)
Browse files Browse the repository at this point in the history
This commit adds better handling for terminal errors when
`window` global is used. This global is removed in Deno 2,
and while we have lints to help with that, an information and
hints are helpful to guide users to working code.

Ref #25797
  • Loading branch information
bartlomieju authored Sep 22, 2024
1 parent 0cb00a6 commit ef3e4a8
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec<FixSuggestion> {
"Run again with `--unstable-broadcast-channel` flag to enable this API.",
),
];
} else if msg.contains("window is not defined") {
return vec![
FixSuggestion::info("window global is not available in Deno 2."),
FixSuggestion::hint("Replace `window` with `globalThis`."),
];
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/specs/run/window/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"tests": {
"window1": {
"args": "run window1.js",
"exitCode": 1,
"output": "window1.out"
},
"window2": {
"args": "run window2.js",
"exitCode": 1,
"output": "window2.out"
},
"window3": {
"args": "run window3.js",
"exitCode": 1,
"output": "window3.out"
}
}
}
1 change: 1 addition & 0 deletions tests/specs/run/window/window1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"TextEncoder" in window;
7 changes: 7 additions & 0 deletions tests/specs/run/window/window1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Uncaught (in promise) ReferenceError: window is not defined
"TextEncoder" in window;
^
at [WILDCARD]window1.js:1:18

info: window global is not available in Deno 2.
hint: Replace `window` with `globalThis`.
1 change: 1 addition & 0 deletions tests/specs/run/window/window2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.atob;
7 changes: 7 additions & 0 deletions tests/specs/run/window/window2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Uncaught (in promise) ReferenceError: window is not defined
window.atob;
^
at [WILDCARD]window2.js:1:1

info: window global is not available in Deno 2.
hint: Replace `window` with `globalThis`.
1 change: 1 addition & 0 deletions tests/specs/run/window/window3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.navigator;
7 changes: 7 additions & 0 deletions tests/specs/run/window/window3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: Uncaught (in promise) ReferenceError: window is not defined
window.navigator;
^
at [WILDCARD]window3.js:1:1

info: window global is not available in Deno 2.
hint: Replace `window` with `globalThis`.

0 comments on commit ef3e4a8

Please sign in to comment.