lib: source maps filter null prefix

Fixes: https://github.com/nodejs/node/issues/42417

PR-URL: https://github.com/nodejs/node/pull/42522
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Fabian Cook
2022-04-04 04:21:38 +12:00
committed by GitHub
parent b5f0b49b9b
commit 154fa4132b
5 changed files with 44 additions and 2 deletions

View File

@@ -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) ?

View File

@@ -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

View File

@@ -0,0 +1 @@
{"version":3,"file":"throw-async.mjs","sourceRoot":"","sources":["throw-async.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,GAAG,UAAU,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;AAE3C,MAAM,CAAC,KAAK,UAAU,KAAK;IACzB,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAA;AAC1B,CAAC;AAED,MAAM,KAAK,EAAE,CAAC;AAEd,eAAe;AACf,EAAE;AACF,gIAAgI;AAChI,qBAAqB;AACrB,uDAAuD"}

13
test/fixtures/source-map/throw-async.ts vendored Normal file
View File

@@ -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

View File

@@ -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) {