Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9e8beff

Browse filesBrowse files
BridgeARmarco-ippolito
authored andcommitted
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: #59446 Reviewed-By: Jordan Harband <ljharb@gmail.com>
1 parent b8d145d commit 9e8beff
Copy full SHA for 9e8beff

File tree

Expand file treeCollapse file tree

2 files changed

+41
-13
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+41
-13
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+39-11Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/;
212212
const numberRegExp = /^(0|[1-9][0-9]*)$/;
213213

214214
const coreModuleRegExp = /^ {4}at (?:[^/\\(]+ \(|)node:(.+):\d+:\d+\)?$/;
215-
const nodeModulesRegExp = /[/\\]node_modules[/\\](.+?)(?=[/\\])/g;
216215

217216
const classRegExp = /^(\s+[^(]*?)\s*{/;
218217
// eslint-disable-next-line node-core/no-unescaped-regexp-dot
@@ -1339,16 +1338,45 @@ function removeDuplicateErrorKeys(ctx, keys, err, stack) {
13391338

13401339
function markNodeModules(ctx, line) {
13411340
let tempLine = '';
1342-
let nodeModule;
1343-
let pos = 0;
1344-
while ((nodeModule = nodeModulesRegExp.exec(line)) !== null) {
1345-
// '/node_modules/'.length === 14
1346-
tempLine += StringPrototypeSlice(line, pos, nodeModule.index + 14);
1347-
tempLine += ctx.stylize(nodeModule[1], 'module');
1348-
pos = nodeModule.index + nodeModule[0].length;
1349-
}
1350-
if (pos !== 0) {
1351-
line = tempLine + StringPrototypeSlice(line, pos);
1341+
let lastPos = 0;
1342+
let searchFrom = 0;
1343+
1344+
while (true) {
1345+
const nodeModulePosition = StringPrototypeIndexOf(line, 'node_modules', searchFrom);
1346+
if (nodeModulePosition === -1) {
1347+
break;
1348+
}
1349+
1350+
// Ensure it's a path segment: must have a path separator before and after
1351+
const separator = line[nodeModulePosition - 1];
1352+
const after = line[nodeModulePosition + 12]; // 'node_modules'.length === 12
1353+
1354+
if ((after !== '/' && after !== '\\') || (separator !== '/' && separator !== '\\')) {
1355+
// Not a proper segment; continue searching
1356+
searchFrom = nodeModulePosition + 1;
1357+
continue;
1358+
}
1359+
1360+
const moduleStart = nodeModulePosition + 13; // Include trailing separator
1361+
1362+
// Append up to and including '/node_modules/'
1363+
tempLine += StringPrototypeSlice(line, lastPos, moduleStart);
1364+
1365+
let moduleEnd = StringPrototypeIndexOf(line, separator, moduleStart);
1366+
if (line[moduleStart] === '@') {
1367+
// Namespaced modules have an extra slash: @namespace/package
1368+
moduleEnd = StringPrototypeIndexOf(line, separator, moduleEnd + 1);
1369+
}
1370+
1371+
const nodeModule = StringPrototypeSlice(line, moduleStart, moduleEnd);
1372+
tempLine += ctx.stylize(nodeModule, 'module');
1373+
1374+
lastPos = moduleEnd;
1375+
searchFrom = moduleEnd;
1376+
}
1377+
1378+
if (lastPos !== 0) {
1379+
line = tempLine + StringPrototypeSlice(line, lastPos);
13521380
}
13531381
return line;
13541382
}
Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,7 +2833,7 @@ assert.strictEqual(
28332833
// Use a fake stack to verify the expected colored outcome.
28342834
const stack = [
28352835
'Error: CWD is grayed out, even cwd that are percent encoded!',
2836-
' at A.<anonymous> (/test/node_modules/foo/node_modules/bar/baz.js:2:7)',
2836+
' at A.<anonymous> (/test/node_modules/foo/node_modules/@namespace/bar/baz.js:2:7)',
28372837
' at Module._compile (node:internal/modules/cjs/loader:827:30)',
28382838
' at Fancy (node:vm:697:32)',
28392839
// This file is not an actual Node.js core file.
@@ -2858,7 +2858,7 @@ assert.strictEqual(
28582858
}
28592859
const escapedCWD = util.inspect(process.cwd()).slice(1, -1);
28602860
util.inspect(err, { colors: true }).split('\n').forEach((line, i) => {
2861-
let expected = stack[i].replace(/node_modules\/([^/]+)/gi, (_, m) => {
2861+
let expected = stack[i].replace(/node_modules\/(@[^/]+\/[^/]+|[^/]+)/gi, (_, m) => {
28622862
return `node_modules/\u001b[4m${m}\u001b[24m`;
28632863
}).replaceAll(new RegExp(`(\\(?${escapedCWD}(\\\\|/))`, 'gi'), (_, m) => {
28642864
return `\x1B[90m${m}\x1B[39m`;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.