Skip to content

Commit c89b67e

Browse files
legendecasRafaelGSS
authored andcommitted
lib: add type names in source mapped stack traces
The type name is determined by the constructor name of the receiver in a call site. PR-URL: #58976 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 1e37eab commit c89b67e

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

lib/internal/source_map/prepare_stack_trace.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,12 @@ function serializeJSStackFrame(sm, callSite, callerCallSite) {
112112

113113
const typeName = callSite.getTypeName();
114114
const namePrefix = typeName !== null && typeName !== 'global' ? `${typeName}.` : '';
115-
const originalName = `${namePrefix}${fnName || '<anonymous>'}`;
116-
// The original call site may have a different symbol name
117-
// associated with it, use it:
118-
const mappedName = (name && name !== originalName) ?
119-
`${name}` :
120-
`${originalName}`;
121-
const hasName = !!(name || originalName);
115+
const originalName = `${fnName || '<anonymous>'}`;
116+
const mappedName = `${namePrefix}${name || originalName}` || '';
122117
// Replace the transpiled call site with the original:
123-
return `${prefix}${mappedName}${hasName ? ' (' : ''}` +
118+
return `${prefix}${mappedName} (` +
124119
`${originalSourceNoScheme}:${originalLine + 1}:` +
125-
`${originalColumn + 1}${hasName ? ')' : ''}`;
120+
`${originalColumn + 1})`;
126121
}
127122

128123
// Transpilers may have removed the original symbol name used in the stack
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
// Flags: --enable-source-maps
4+
5+
require('../../../common');
6+
Error.stackTraceLimit = 2;
7+
require('../throw-class-method.min.js');
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Error: This is a test
2+
at Foo.bar (*/test/fixtures/source-map/throw-class-method.js:3:11)
3+
at Object.<anonymous> (*/test/fixtures/source-map/throw-class-method.js:12:7)
4+
Error: This is a test
5+
at Bar.bar (*/test/fixtures/source-map/throw-class-method.js:3:11)
6+
at Object.<anonymous> (*/test/fixtures/source-map/throw-class-method.js:19:7)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Foo {
2+
bar() {
3+
throw Error('This is a test');
4+
}
5+
}
6+
7+
class Bar {}
8+
Bar.prototype.bar = Foo.prototype.bar;
9+
10+
try {
11+
const foo = new Foo();
12+
foo.bar();
13+
} catch (e) {
14+
console.error(e);
15+
}
16+
17+
try {
18+
const bar = Object.create(Bar.prototype);
19+
bar.bar();
20+
} catch (e) {
21+
console.error(e);
22+
}
23+
24+
// To recreate:
25+
//
26+
// cd test/fixtures/source-map
27+
// npx terser -o throw-class-method.min.js --source-map "url='throw-class-method.min.js.map'" throw-class-method.js

test/fixtures/source-map/throw-class-method.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/source-map/throw-class-method.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/parallel/test-node-output-sourcemaps.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('sourcemaps output', { concurrency: !process.env.TEST_PARALLEL }, () =>
2727
{ name: 'source-map/output/source_map_sourcemapping_url_string.js' },
2828
{ name: 'source-map/output/source_map_throw_async_stack_trace.mjs' },
2929
{ name: 'source-map/output/source_map_throw_catch.js' },
30+
{ name: 'source-map/output/source_map_throw_class_method.js' },
3031
{ name: 'source-map/output/source_map_throw_construct.mjs' },
3132
{ name: 'source-map/output/source_map_throw_first_tick.js' },
3233
{ name: 'source-map/output/source_map_throw_icu.js' },

0 commit comments

Comments
 (0)