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: https://github.com/nodejs/node/pull/58976
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Chengzhong Wu 2025-08-01 18:11:57 +01:00 committed by GitHub
parent 7f37d39a87
commit f33e0fcc83
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 48 additions and 9 deletions

View file

@ -0,0 +1,7 @@
'use strict';
// Flags: --enable-source-maps
require('../../../common');
Error.stackTraceLimit = 2;
require('../throw-class-method.min.js');

View file

@ -0,0 +1,6 @@
Error: This is a test
at Foo.bar (*/test/fixtures/source-map/throw-class-method.js:3:11)
at Object.<anonymous> (*/test/fixtures/source-map/throw-class-method.js:12:7)
Error: This is a test
at Bar.bar (*/test/fixtures/source-map/throw-class-method.js:3:11)
at Object.<anonymous> (*/test/fixtures/source-map/throw-class-method.js:19:7)

View file

@ -0,0 +1,27 @@
class Foo {
bar() {
throw Error('This is a test');
}
}
class Bar {}
Bar.prototype.bar = Foo.prototype.bar;
try {
const foo = new Foo();
foo.bar();
} catch (e) {
console.error(e);
}
try {
const bar = Object.create(Bar.prototype);
bar.bar();
} catch (e) {
console.error(e);
}
// 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

View file

@ -0,0 +1,2 @@
class Foo{bar(){throw Error("This is a test")}}class Bar{}Bar.prototype.bar=Foo.prototype.bar;try{const foo=new Foo;foo.bar()}catch(e){console.error(e)}try{const bar=Object.create(Bar.prototype);bar.bar()}catch(e){console.error(e)}
//# sourceMappingURL=throw-class-method.min.js.map

View file

@ -0,0 +1 @@
{"version":3,"names":["Foo","bar","Error","Bar","prototype","foo","e","console","error","Object","create"],"sources":["throw-class-method.js"],"mappings":"AAAA,MAAMA,IACJ,GAAAC,GACE,MAAMC,MAAM,iBACd,EAGF,MAAMC,KACNA,IAAIC,UAAUH,IAAMD,IAAII,UAAUH,IAElC,IACE,MAAMI,IAAM,IAAIL,IAChBK,IAAIJ,KACN,CAAE,MAAOK,GACPC,QAAQC,MAAMF,EAChB,CAEA,IACE,MAAML,IAAMQ,OAAOC,OAAOP,IAAIC,WAC9BH,IAAIA,KACN,CAAE,MAAOK,GACPC,QAAQC,MAAMF,EAChB","ignoreList":[]}

View file

@ -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' },