Skip to content

lib: add type names in source mapped stack traces #58976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
13 changes: 4 additions & 9 deletions lib/internal/source_map/prepare_stack_trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,12 @@ function serializeJSStackFrame(sm, callSite, callerCallSite) {

const typeName = callSite.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 mappedName = (name && name !== originalName) ?
`${name}` :
`${originalName}`;
const hasName = !!(name || originalName);
const originalName = `${fnName || '<anonymous>'}`;
const mappedName = `${namePrefix}${name || originalName}` || '';
// Replace the transpiled call site with the original:
return `${prefix}${mappedName}${hasName ? ' (' : ''}` +
return `${prefix}${mappedName} (` +
`${originalSourceNoScheme}:${originalLine + 1}:` +
`${originalColumn + 1}${hasName ? ')' : ''}`;
`${originalColumn + 1})`;
}

// Transpilers may have removed the original symbol name used in the stack
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

// Flags: --enable-source-maps

require('../../../common');
Error.stackTraceLimit = 2;
require('../throw-class-method.min.js');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Trace: This is a test
at Foo.bar (*/test/fixtures/source-map/throw-class-method.js:3:13)
at Object.<anonymous> (*/test/fixtures/source-map/throw-class-method.js:11:5)
Trace: This is a test
at Bar.bar (*/test/fixtures/source-map/throw-class-method.js:3:13)
at Object.<anonymous> (*/test/fixtures/source-map/throw-class-method.js:13:5)
18 changes: 18 additions & 0 deletions test/fixtures/source-map/throw-class-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Foo {
bar() {
console.trace('This is a test');
}
}

class Bar {}
Bar.prototype.bar = Foo.prototype.bar;

const foo = new Foo();
foo.bar();
const bar = Object.create(Bar.prototype);
bar.bar();

// To recreate:
//
// cd test/fixtures/source-map
// npx terser -o throw-class-method.min.js --source-map "url='throw-class-method.min.js.map'" throw-class-method.js
2 changes: 2 additions & 0 deletions test/fixtures/source-map/throw-class-method.min.js

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

1 change: 1 addition & 0 deletions test/fixtures/source-map/throw-class-method.min.js.map

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

1 change: 1 addition & 0 deletions test/parallel/test-node-output-sourcemaps.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('sourcemaps output', { concurrency: !process.env.TEST_PARALLEL }, () =>
{ name: 'source-map/output/source_map_sourcemapping_url_string.js' },
{ name: 'source-map/output/source_map_throw_async_stack_trace.mjs' },
{ name: 'source-map/output/source_map_throw_catch.js' },
{ name: 'source-map/output/source_map_throw_class_method.js' },
{ name: 'source-map/output/source_map_throw_construct.mjs' },
{ name: 'source-map/output/source_map_throw_first_tick.js' },
{ name: 'source-map/output/source_map_throw_icu.js' },
Expand Down
Loading