util: fix error's namespaced node_modules highlighting using inspect

When inspecting errors, node_modules are highlighted with an
underscore. So far namespaced modules only highlighted the namespace
but not the rest of the module name. This is fixed by matching the
full name.
As drive-by it improves the performance slightly by removing the
regular expression in favor of indexOf to identify the right spot.

PR-URL: https://github.com/nodejs/node/pull/59446
Reviewed-By: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
Ruben Bridgewater authored and Richard Lau committed 2025-09-20 03:33:55 +00:00
1 parent f64d0def94
commit a2d2003daa
2 files changed
+40 -12

No files matched your search

+2 -2
View File
@@ -2835,7 +2835,7 @@ assert.strictEqual(
// Use a fake stack to verify the expected colored outcome.
const stack = [
'Error: CWD is grayed out, even cwd that are percent encoded!',
' at A.<anonymous> (/test/node_modules/foo/node_modules/bar/baz.js:2:7)',
' at A.<anonymous> (/test/node_modules/foo/node_modules/@namespace/bar/baz.js:2:7)',
' at Module._compile (node:internal/modules/cjs/loader:827:30)',
' at Fancy (node:vm:697:32)',
// This file is not an actual Node.js core file.
@@ -2860,7 +2860,7 @@ assert.strictEqual(
}
const escapedCWD = util.inspect(process.cwd()).slice(1, -1);
util.inspect(err, { colors: true }).split('\n').forEach((line, i) => {
let expected = stack[i].replace(/node_modules\/([^/]+)/gi, (_, m) => {
let expected = stack[i].replace(/node_modules\/(@[^/]+\/[^/]+|[^/]+)/gi, (_, m) => {
return `node_modules/\u001b[4m${m}\u001b[24m`;
}).replaceAll(new RegExp(`(\\(?${escapedCWD}(\\\\|/))`, 'gi'), (_, m) => {
return `\x1B[90m${m}\x1B[39m`;