diff --git a/lib/util.js b/lib/util.js index e828229380d9..1454392baddb 100644 --- a/lib/util.js +++ b/lib/util.js @@ -515,8 +515,8 @@ 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 ?? '', + // The name is optional in a source map, so keep the generated one when absent + functionName: entry.name ?? callSite.functionName, scriptName: entry.originalSource, lineNumber: entry.originalLine + 1, column: entry.originalColumn + 1, diff --git a/test/fixtures/source-map/get-call-sites-named-mapped.js b/test/fixtures/source-map/get-call-sites-named-mapped.js new file mode 100644 index 000000000000..7b169f500c1f --- /dev/null +++ b/test/fixtures/source-map/get-call-sites-named-mapped.js @@ -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-named-mapped.map diff --git a/test/fixtures/source-map/get-call-sites-named-mapped.map b/test/fixtures/source-map/get-call-sites-named-mapped.map new file mode 100644 index 000000000000..88b87c9dc2a6 --- /dev/null +++ b/test/fixtures/source-map/get-call-sites-named-mapped.map @@ -0,0 +1,6 @@ +{ + "version": 3, + "sources": ["get-call-sites-named-original.js"], + "names": [], + "mappings": "AAUA" +} diff --git a/test/fixtures/source-map/get-call-sites-renamed-mapped.js b/test/fixtures/source-map/get-call-sites-renamed-mapped.js new file mode 100644 index 000000000000..ee960b94a19c --- /dev/null +++ b/test/fixtures/source-map/get-call-sites-renamed-mapped.js @@ -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-renamed-mapped.map diff --git a/test/fixtures/source-map/get-call-sites-renamed-mapped.map b/test/fixtures/source-map/get-call-sites-renamed-mapped.map new file mode 100644 index 000000000000..e5003bf5db4f --- /dev/null +++ b/test/fixtures/source-map/get-call-sites-renamed-mapped.map @@ -0,0 +1,6 @@ +{ + "version": 3, + "sources": ["get-call-sites-renamed-original.js"], + "names": ["original"], + "mappings": "AAUAA" +} diff --git a/test/parallel/test-util-getcallsites-sourcemap.js b/test/parallel/test-util-getcallsites-sourcemap.js index 4499ea106fdd..38d8defed97f 100644 --- a/test/parallel/test-util-getcallsites-sourcemap.js +++ b/test/parallel/test-util-getcallsites-sourcemap.js @@ -50,3 +50,34 @@ const fixtures = require('../common/fixtures'); `expected generated file in scriptName, got "${callSite.scriptName}"`, ); } + +// reconstructCallSite keeps the generated function name when the source map +// has no name entry for the call site. +{ + const file = fixtures.path('source-map', 'get-call-sites-named-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()); + // scriptName should point to the original (unmapped) source file + assert.ok( + callSite.scriptName.endsWith('get-call-sites-named-original.js'), + `expected original source in scriptName, got "${callSite.scriptName}"`, + ); + // functionName comes from the generated call site, not the source map + assert.strictEqual(callSite.functionName, 'foo'); +} + +// reconstructCallSite prefers the source map name when the map provides one. +{ + const file = fixtures.path('source-map', 'get-call-sites-renamed-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, 'original'); +}