Skip to content

Commit

Permalink
fix: provide hints in terminal errors for Node.js globals (denoland#2…
Browse files Browse the repository at this point in the history
…6610)

Add info/hint for terminal errors related to Node.js globals:
- __filename
- __dirname
- Buffer
- global
- setImmediate
- clearImmediate

Closes denoland#17494
  • Loading branch information
bartlomieju authored Oct 28, 2024
1 parent 0e64163 commit 484f8ca
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 0 deletions.
42 changes: 42 additions & 0 deletions runtime/fmt_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
Expand Down
3 changes: 3 additions & 0 deletions tests/specs/lint/node_globals_no_duplicate_imports/main.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 2,6 @@ error: Uncaught (in promise) ReferenceError: setImmediate is not defined
const _foo = setImmediate;
^
at [WILDCARD]main.ts:3:14

info: setImmediate is not available in the global scope in Deno.
hint: Import it explicitly with import { setImmediate } from "node:timers";.
34 changes: 34 additions & 0 deletions tests/specs/run/node_globals_hints/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 1,34 @@
{
"tests": {
"__dirname": {
"args": "run dirname.js",
"output": "dirname.out",
"exitCode": 1
},
"__filename": {
"args": "run filename.js",
"output": "filename.out",
"exitCode": 1
},
"clearImmediate": {
"args": "run clear_immediate.js",
"output": "clear_immediate.out",
"exitCode": 1
},
"buffer": {
"args": "run buffer.js",
"output": "buffer.out",
"exitCode": 1
},
"global": {
"args": "run global.js",
"output": "global.out",
"exitCode": 1
},
"setImmediate": {
"args": "run set_immediate.js",
"output": "set_immediate.out",
"exitCode": 1
}
}
}
1 change: 1 addition & 0 deletions tests/specs/run/node_globals_hints/buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
Buffer;
7 changes: 7 additions & 0 deletions tests/specs/run/node_globals_hints/buffer.out
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
error: Uncaught (in promise) ReferenceError: Buffer is not defined
Buffer;
^
at [WILDCARD]buffer.js:1:1

info: Buffer is not available in the global scope in Deno.
hint: Import it explicitly with import { Buffer } from "node:buffer";.
1 change: 1 addition & 0 deletions tests/specs/run/node_globals_hints/clear_immediate.js
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
clearImmediate;
7 changes: 7 additions & 0 deletions tests/specs/run/node_globals_hints/clear_immediate.out
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
error: Uncaught (in promise) ReferenceError: clearImmediate is not defined
clearImmediate;
^
at [WILDCARD]clear_immediate.js:1:1

info: clearImmediate is not available in the global scope in Deno.
hint: Import it explicitly with import { clearImmediate } from "node:timers";.
1 change: 1 addition & 0 deletions tests/specs/run/node_globals_hints/dirname.js
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
__dirname;
7 changes: 7 additions & 0 deletions tests/specs/run/node_globals_hints/dirname.out
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
error: Uncaught (in promise) ReferenceError: __dirname is not defined
__dirname;
^
at [WILDCARD]dirname.js:1:1

info: __dirname global is not available in ES modules.
hint: Use import.meta.dirname instead.
1 change: 1 addition & 0 deletions tests/specs/run/node_globals_hints/filename.js
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
__filename;
7 changes: 7 additions & 0 deletions tests/specs/run/node_globals_hints/filename.out
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
error: Uncaught (in promise) ReferenceError: __filename is not defined
__filename;
^
at [WILDCARD]filename.js:1:1

info: __filename global is not available in ES modules.
hint: Use import.meta.filename instead.
1 change: 1 addition & 0 deletions tests/specs/run/node_globals_hints/global.js
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
global;
7 changes: 7 additions & 0 deletions tests/specs/run/node_globals_hints/global.out
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
error: Uncaught (in promise) ReferenceError: global is not defined
global;
^
at [WILDCARD]global.js:1:1

info: global is not available in the global scope in Deno.
hint: Use globalThis instead, or assign globalThis.global = globalThis.
1 change: 1 addition & 0 deletions tests/specs/run/node_globals_hints/set_immediate.js
Original file line number Diff line number Diff line change
@@ -0,0 1 @@
setImmediate;
7 changes: 7 additions & 0 deletions tests/specs/run/node_globals_hints/set_immediate.out
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
error: Uncaught (in promise) ReferenceError: setImmediate is not defined
setImmediate;
^
at [WILDCARD]set_immediate.js:1:1

info: setImmediate is not available in the global scope in Deno.
hint: Import it explicitly with import { setImmediate } from "node:timers";.

0 comments on commit 484f8ca

Please sign in to comment.