diff --git a/lib/internal/source_map/prepare_stack_trace.js b/lib/internal/source_map/prepare_stack_trace.js index 3e4b0825e7b3a5..814ea396f60144 100644 --- a/lib/internal/source_map/prepare_stack_trace.js +++ b/lib/internal/source_map/prepare_stack_trace.js @@ -112,17 +112,12 @@ function serializeJSStackFrame(sm, callSite, callerCallSite) { const typeName = callSite.getTypeName(); const namePrefix = typeName !== null && typeName !== 'global' ? `${typeName}.` : ''; - const originalName = `${namePrefix}${fnName || ''}`; - // 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 || ''}`; + 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 diff --git a/test/fixtures/source-map/output/source_map_throw_class_method.js b/test/fixtures/source-map/output/source_map_throw_class_method.js new file mode 100644 index 00000000000000..c8c4b722a41829 --- /dev/null +++ b/test/fixtures/source-map/output/source_map_throw_class_method.js @@ -0,0 +1,7 @@ +'use strict'; + +// Flags: --enable-source-maps + +require('../../../common'); +Error.stackTraceLimit = 2; +require('../throw-class-method.min.js'); diff --git a/test/fixtures/source-map/output/source_map_throw_class_method.snapshot b/test/fixtures/source-map/output/source_map_throw_class_method.snapshot new file mode 100644 index 00000000000000..a6b7984d4c7c76 --- /dev/null +++ b/test/fixtures/source-map/output/source_map_throw_class_method.snapshot @@ -0,0 +1,6 @@ +Error: This is a test + at Foo.bar (*/test/fixtures/source-map/throw-class-method.js:3:11) + at Object. (*/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. (*/test/fixtures/source-map/throw-class-method.js:19:7) diff --git a/test/fixtures/source-map/throw-class-method.js b/test/fixtures/source-map/throw-class-method.js new file mode 100644 index 00000000000000..2bfa9ce06675dd --- /dev/null +++ b/test/fixtures/source-map/throw-class-method.js @@ -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 diff --git a/test/fixtures/source-map/throw-class-method.min.js b/test/fixtures/source-map/throw-class-method.min.js new file mode 100644 index 00000000000000..eba49ea1c24b54 --- /dev/null +++ b/test/fixtures/source-map/throw-class-method.min.js @@ -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 \ No newline at end of file diff --git a/test/fixtures/source-map/throw-class-method.min.js.map b/test/fixtures/source-map/throw-class-method.min.js.map new file mode 100644 index 00000000000000..5be313d68fd551 --- /dev/null +++ b/test/fixtures/source-map/throw-class-method.min.js.map @@ -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":[]} \ No newline at end of file diff --git a/test/parallel/test-node-output-sourcemaps.mjs b/test/parallel/test-node-output-sourcemaps.mjs index 81c36934ba0f3e..c11c2c36735dae 100644 --- a/test/parallel/test-node-output-sourcemaps.mjs +++ b/test/parallel/test-node-output-sourcemaps.mjs @@ -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' },