diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-10-28 23:55:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-29 00:55:51 +0100 |
commit | 484f8ca9c30909914eb3348b2df10d44e2753248 (patch) | |
tree | 352f3e7c7e0e6d3354438d94e77fca1abc911fe6 /runtime | |
parent | 0e641632c38383a1d67aa610867496f64ae423ca (diff) |
fix: provide hints in terminal errors for Node.js globals (#26610)
Add info/hint for terminal errors related to Node.js globals:
- __filename
- __dirname
- Buffer
- global
- setImmediate
- clearImmediate
Closes https://github.com/denoland/deno/issues/17494
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/fmt_errors.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/runtime/fmt_errors.rs b/runtime/fmt_errors.rs index 7c6cf6d39..4cd8a0634 100644 --- a/runtime/fmt_errors.rs +++ b/runtime/fmt_errors.rs @@ -321,6 +321,48 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec<FixSuggestion> { ]), FixSuggestion::docs("https://docs.deno.com/go/commonjs"), ]; + } else if msg.contains("__filename is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "<u>__filename</> global is not available in ES modules." + )), + FixSuggestion::hint(cstr!("Use <u>import.meta.filename</> instead.")), + ]; + } else if msg.contains("__dirname is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "<u>__dirname</> global is not available in ES modules." + )), + FixSuggestion::hint(cstr!("Use <u>import.meta.dirname</> instead.")), + ]; + } else if msg.contains("Buffer is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "<u>Buffer</> is not available in the global scope in Deno." + )), + FixSuggestion::hint(cstr!("Import it explicitly with <u>import { Buffer } from \"node:buffer\";</>.")), + ]; + } else if msg.contains("clearImmediate is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "<u>clearImmediate</> is not available in the global scope in Deno." + )), + FixSuggestion::hint(cstr!("Import it explicitly with <u>import { clearImmediate } from \"node:timers\";</>.")), + ]; + } else if msg.contains("setImmediate is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "<u>setImmediate</> is not available in the global scope in Deno." + )), + FixSuggestion::hint(cstr!("Import it explicitly with <u>import { setImmediate } from \"node:timers\";</>.")), + ]; + } else if msg.contains("global is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "<u>global</> is not available in the global scope in Deno." + )), + FixSuggestion::hint(cstr!("Use <u>globalThis</> instead, or assign <u>globalThis.global = globalThis</>.")), + ]; } else if msg.contains("openKv is not a function") { return vec![ FixSuggestion::info("Deno.openKv() is an unstable API."), |