diff --git a/lib/util.js b/lib/util.js index e828229380d9..7a9c06e8bdbb 100644 --- a/lib/util.js +++ b/lib/util.js @@ -515,8 +515,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, diff --git a/test/fixtures/source-map/get-call-sites-function-name-mapped.js b/test/fixtures/source-map/get-call-sites-function-name-mapped.js new file mode 100644 index 000000000000..d400af7f2fb8 --- /dev/null +++ b/test/fixtures/source-map/get-call-sites-function-name-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-function-name-mapped.map diff --git a/test/fixtures/source-map/get-call-sites-function-name-mapped.map b/test/fixtures/source-map/get-call-sites-function-name-mapped.map new file mode 100644 index 000000000000..de70e4512ffa --- /dev/null +++ b/test/fixtures/source-map/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": [] +} diff --git a/test/parallel/test-util-getcallsites-sourcemap.js b/test/parallel/test-util-getcallsites-sourcemap.js index 4499ea106fdd..9259832261ee 100644 --- a/test/parallel/test-util-getcallsites-sourcemap.js +++ b/test/parallel/test-util-getcallsites-sourcemap.js @@ -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');