Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/source-map/get-call-sites-named-mapped.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/fixtures/source-map/get-call-sites-named-mapped.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 3,
"sources": ["get-call-sites-named-original.js"],
"names": [],
"mappings": "AAUA"
}
2 changes: 2 additions & 0 deletions test/fixtures/source-map/get-call-sites-renamed-mapped.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/fixtures/source-map/get-call-sites-renamed-mapped.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 3,
"sources": ["get-call-sites-renamed-original.js"],
"names": ["original"],
"mappings": "AAUAA"
}
31 changes: 31 additions & 0 deletions test/parallel/test-util-getcallsites-sourcemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Loading