fix: add hint for missing document global in terminal error (#26218)

This came up on Discord as a question so I thought it's worth adding a
hint for this as it might be a common pitfall.

---------

Signed-off-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
This commit is contained in:
Bartek Iwańczuk 2024-10-15 22:51:39 +01:00 committed by GitHub
parent 3065dadea3
commit ee904ec06c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 67 additions and 1 deletions

1
Cargo.lock generated
View File

@ -1997,6 +1997,7 @@ dependencies = [
name = "deno_runtime"
version = "0.180.0"
dependencies = [
"color-print",
"deno_ast",
"deno_broadcast_channel",
"deno_cache",

View File

@ -106,6 +106,7 @@ cbc = { version = "=0.1.2", features = ["alloc"] }
# Note: Do not use the "clock" feature of chrono, as it links us to CoreFoundation on macOS.
# Instead use util::time::utc_now()
chrono = { version = "0.4", default-features = false, features = ["std", "serde"] }
color-print = "0.3.5"
console_static_text = "=0.8.1"
dashmap = "5.5.3"
data-encoding = "2.3.3"

View File

@ -94,7 +94,7 @@ chrono = { workspace = true, features = ["now"] }
clap = { version = "=4.5.16", features = ["env", "string", "wrap_help", "error-context"] }
clap_complete = "=4.5.24"
clap_complete_fig = "=4.5.2"
color-print = "0.3.5"
color-print.workspace = true
console_static_text.workspace = true
dashmap.workspace = true
data-encoding.workspace = true

View File

@ -100,6 +100,7 @@ deno_websocket.workspace = true
deno_webstorage.workspace = true
node_resolver = { workspace = true, features = ["sync"] }
color-print.workspace = true
dlopen2.workspace = true
encoding_rs.workspace = true
fastwebsockets.workspace = true

View File

@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
//! This mod provides DenoError to unify errors across Deno.
use color_print::cstr;
use deno_core::error::format_frame;
use deno_core::error::JsError;
use deno_terminal::colors::cyan;
@ -367,6 +368,16 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec<FixSuggestion> {
]
)
];
} else if msg.contains("document is not defined") {
return vec![
FixSuggestion::info(cstr!(
"<u>document</> global is not available in Deno."
)),
FixSuggestion::hint_multiline(&[
cstr!("Use a library like <u>happy-dom</>, <u>deno_dom</>, <u>linkedom</> or <u>JSDom</>"),
cstr!("and setup the <u>document</> global according to the library documentation."),
]),
];
}
}

View File

@ -0,0 +1,9 @@
{
"tests": {
"document": {
"args": "run document.js",
"exitCode": 1,
"output": "document.out"
}
}
}

View File

@ -0,0 +1 @@
document.querySelector("div");

View File

@ -0,0 +1,8 @@
error: Uncaught (in promise) ReferenceError: document is not defined
document.querySelector("div");
^
at [WILDCARD]document.js:1:1
info: document global is not available in Deno.
hint: Use a library like happy-dom, deno_dom, linkedom or JSDom
and setup the document global according to the library documentation.

View File

@ -0,0 +1,9 @@
{
"tests": {
"document": {
"args": "test document_test.js",
"exitCode": 1,
"output": "document_test.out"
}
}
}

View File

@ -0,0 +1,3 @@
Deno.test("document query selector", () => {
document.querySelector("div");
});

View File

@ -0,0 +1,22 @@
running 1 test from ./document_test.js
document query selector ... FAILED [WILDCARD]
ERRORS
document query selector => ./document_test.js:1:6
error: ReferenceError: document is not defined
document.querySelector("div");
^
at [WILDCARD]document_test.js:2:3
info: document global is not available in Deno.
hint: Use a library like happy-dom, deno_dom, linkedom or JSDom
and setup the document global according to the library documentation.
FAILURES
document query selector => ./document_test.js:1:6
FAILED | 0 passed | 1 failed ([WILDCARD])
error: Test failed