Skip to content

Commit

Permalink
lib: source maps filter null prefix
Browse files Browse the repository at this point in the history
Fixes: nodejs#42417

PR-URL: nodejs#42522
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
fabiancook authored Apr 3, 2022
1 parent b5f0b49 commit 154fa41
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/internal/source_map/prepare_stack_trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ const prepareStackTrace = (globalThis, error, trace) => {
}
// Construct call site name based on: v8.dev/docs/stack-trace-api:
const fnName = t.getFunctionName() ?? t.getMethodName();
const originalName = `${t.getTypeName() !== 'global' ?
`${t.getTypeName()}.` : ''}${fnName || '<anonymous>'}`;
const typeName = t.getTypeName();
const namePrefix = typeName !== null && typeName !== 'global' ? `${typeName}.` : '';
const originalName = `${namePrefix}${fnName || '<anonymous>'}`;
// The original call site may have a different symbol name
// associated with it, use it:
const prefix = (name && name !== originalName) ?
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/source-map/throw-async.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const message = 'Message ' + Math.random();
export async function Throw() {
throw new Error(message);
}
await Throw();
// To recreate:
//
// npx tsc --module esnext --target es2017 --outDir test/fixtures/source-map --sourceMap test/fixtures/source-map/throw-async.ts
// + rename js to mjs
// + rename js to mjs in source map comment in mjs file
//# sourceMappingURL=throw-async.mjs.map
1 change: 1 addition & 0 deletions test/fixtures/source-map/throw-async.mjs.map

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

13 changes: 13 additions & 0 deletions test/fixtures/source-map/throw-async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const message = 'Message ' + Math.random();

export async function Throw() {
throw new Error(message)
}

await Throw();

// To recreate:
//
// npx tsc --module esnext --target es2017 --outDir test/fixtures/source-map --sourceMap test/fixtures/source-map/throw-async.ts
// + rename js to mjs
// + rename js to mjs in source map comment in mjs file
16 changes: 16 additions & 0 deletions test/parallel/test-source-map-enable.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,22 @@ function nextdir() {
assert.ok(sourceMap);
}

// Does not include null for async/await with esm
// Refs: https://github.com/nodejs/node/issues/42417
{
const output = spawnSync(process.execPath, [
'--enable-source-maps',
require.resolve('../fixtures/source-map/throw-async.mjs'),
]);
// Error in original context of source content:
assert.match(
output.stderr.toString(),
/throw new Error\(message\)\r?\n.*\^/
);
// Rewritten stack trace:
assert.match(output.stderr.toString(), /at Throw \([^)]+throw-async\.ts:4:9\)/);
}

function getSourceMapFromCache(fixtureFile, coverageDirectory) {
const jsonFiles = fs.readdirSync(coverageDirectory);
for (const jsonFile of jsonFiles) {
Expand Down

0 comments on commit 154fa41

Please sign in to comment.