util: preserve function names without source map names

Signed-off-by: Hiroki Osame <hiroki.osame@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65108
Fixes: https://github.com/nodejs/node/issues/65104
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
This commit is contained in:
Hiroki Osame authored and Antoine du Hamel committed 2026-08-27 14:17:03 +02:00
1 parent 5ded71f9cc
commit cea9786de8
4 files changed
+22 -2

No files matched your search

+1 -2
View File
@@ -514,8 +514,7 @@ function reconstructCallSite(callSite) {
if (!entry?.originalSource) return;
return {
__proto__: null,
// If the name is not found, it is an empty string to match the behavior of `util.getCallSite()`
functionName: entry.name ?? '',
functionName: entry.name || callSite.functionName,
scriptName: entry.originalSource,
lineNumber: entry.originalLine + 1,
column: entry.originalColumn + 1,
@@ -0,0 +1,2 @@
const{getCallSites}=require('node:util');function foo(){process.stdout.write(JSON.stringify(getCallSites({sourceMap:true})[0]))}foo();
//# sourceMappingURL=get-call-sites-function-name-mapped.map
@@ -0,0 +1,7 @@
{
"version": 3,
"sources": ["get-call-sites-function-name-original.js"],
"sourcesContent": ["const { getCallSites } = require('node:util'); function foo() { process.stdout.write(JSON.stringify(getCallSites({ sourceMap: true })[0])); } foo();"],
"mappings": "AAAA,KAAM,CAAE,YAAa,EAAI,QAAQ,WAAW,EAAG,SAAS,KAAM,CAAE,QAAQ,OAAO,MAAM,KAAK,UAAU,aAAa,CAAE,UAAW,EAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAG,CAAE,IAAI",
"names": []
}
@@ -39,6 +39,18 @@ const fixtures = require('../common/fixtures');
assert.strictEqual(callSite.columnNumber, 1);
}
// A source map may omit names, so preserve the function name from the call site.
{
const file = fixtures.path('source-map', 'get-call-sites-function-name-mapped.js');
const { status, stderr, stdout } = spawnSync(
process.execPath,
['--enable-source-maps', file],
);
assert.strictEqual(status, 0, stderr.toString());
const callSite = JSON.parse(stdout.toString());
assert.strictEqual(callSite.functionName, 'foo');
}
// Without --enable-source-maps the generated file path is preserved.
{
const file = fixtures.path('source-map', 'get-call-sites-mapped.js');