From ef3e4a8f74ea65cfed58c1276696f50a8779f816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 22 Sep 2024 23:05:42 +0100 Subject: [PATCH] feat: Show hints when using `window` global (#25805) 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 https://github.com/denoland/deno/issues/25797 --- cli/main.rs | 5 +++++ tests/specs/run/window/__test__.jsonc | 19 +++++++++++++++++++ tests/specs/run/window/window1.js | 1 + tests/specs/run/window/window1.out | 7 +++++++ tests/specs/run/window/window2.js | 1 + tests/specs/run/window/window2.out | 7 +++++++ tests/specs/run/window/window3.js | 1 + tests/specs/run/window/window3.out | 7 +++++++ 8 files changed, 48 insertions(+) create mode 100644 tests/specs/run/window/__test__.jsonc create mode 100644 tests/specs/run/window/window1.js create mode 100644 tests/specs/run/window/window1.out create mode 100644 tests/specs/run/window/window2.js create mode 100644 tests/specs/run/window/window2.out create mode 100644 tests/specs/run/window/window3.js create mode 100644 tests/specs/run/window/window3.out diff --git a/cli/main.rs b/cli/main.rs index a7e1c8342e5059..84c3704ca1d0fa 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -380,6 +380,11 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec { "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`."), + ]; } } diff --git a/tests/specs/run/window/__test__.jsonc b/tests/specs/run/window/__test__.jsonc new file mode 100644 index 00000000000000..1ee4449b57e068 --- /dev/null +++ b/tests/specs/run/window/__test__.jsonc @@ -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" + } + } +} diff --git a/tests/specs/run/window/window1.js b/tests/specs/run/window/window1.js new file mode 100644 index 00000000000000..a480d4c2c6eb90 --- /dev/null +++ b/tests/specs/run/window/window1.js @@ -0,0 +1 @@ +"TextEncoder" in window; diff --git a/tests/specs/run/window/window1.out b/tests/specs/run/window/window1.out new file mode 100644 index 00000000000000..4e1674425279d8 --- /dev/null +++ b/tests/specs/run/window/window1.out @@ -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`. diff --git a/tests/specs/run/window/window2.js b/tests/specs/run/window/window2.js new file mode 100644 index 00000000000000..0fdf5dcb98fff6 --- /dev/null +++ b/tests/specs/run/window/window2.js @@ -0,0 +1 @@ +window.atob; diff --git a/tests/specs/run/window/window2.out b/tests/specs/run/window/window2.out new file mode 100644 index 00000000000000..7d00cd7ed15728 --- /dev/null +++ b/tests/specs/run/window/window2.out @@ -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`. diff --git a/tests/specs/run/window/window3.js b/tests/specs/run/window/window3.js new file mode 100644 index 00000000000000..24fa32b5ac3586 --- /dev/null +++ b/tests/specs/run/window/window3.js @@ -0,0 +1 @@ +window.navigator; diff --git a/tests/specs/run/window/window3.out b/tests/specs/run/window/window3.out new file mode 100644 index 00000000000000..9a157975f80aaf --- /dev/null +++ b/tests/specs/run/window/window3.out @@ -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`.