Skip to content

Commit 0582d53

Browse files
committed
fix
1 parent 69012b9 commit 0582d53

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
"@jridgewell/trace-mapping": "0.3.9"
2323
},
2424
"devDependencies": {
25+
"@types/lodash": "^4.14.182",
2526
"browserify": "^4.2.3",
2627
"coffeescript": "^1.12.7",
2728
"http-server": "^0.11.1",
29+
"lodash": "^4.17.21",
2830
"mocha": "^3.5.3",
2931
"semver": "^7.3.7",
3032
"source-map": "0.6.1",

source-map-support.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,12 @@ function mapEvalOrigin(origin) {
398398
}
399399

400400
// This is copied almost verbatim from the V8 source code at
401-
// https://code.google.com/p/v8/source/browse/trunk/src/messages.js. The
402-
// implementation of wrapCallSite() used to just forward to the actual source
401+
// https://code.google.com/p/v8/source/browse/trunk/src/messages.js
402+
// Update 2022-04-29:
403+
// https://github.com/v8/v8/blob/98f6f100c5ab8e390e51422747c4ef644d5ac6f2/src/builtins/builtins-callsite.cc#L175-L179
404+
// https://github.com/v8/v8/blob/98f6f100c5ab8e390e51422747c4ef644d5ac6f2/src/objects/call-site-info.cc#L795-L804
405+
// https://github.com/v8/v8/blob/98f6f100c5ab8e390e51422747c4ef644d5ac6f2/src/objects/call-site-info.cc#L717-L750
406+
// The implementation of wrapCallSite() used to just forward to the actual source
403407
// code of CallSite.prototype.toString but unfortunately a new release of V8
404408
// did something to the prototype chain and broke the shim. The only fix I
405409
// could find was copy/paste.

test.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var path = require('path');
1616
const { pathToFileURL } = require('url');
1717
var bufferFrom = Buffer.from;
1818
const semver = require('semver');
19+
const { once, mapValues, flow } = require('lodash');
1920

2021
// Helper to create regular expressions from string templates, to use interpolation
2122
function re(...args) {
@@ -34,9 +35,9 @@ function namedExportDeclaration() {
3435
* This varies across CJS / ESM and node versions.
3536
* Example: ` at Module.exports.test (`...
3637
*/
37-
function stackFrameAtTest(sourceMapSupportInstalled = true) {
38+
function stackFrameAtTest() {
3839
if(semver.gte(process.versions.node, '18.0.0')) {
39-
return extension === 'mjs' ? 'Module\\.test' : sourceMapSupportInstalled ? 'Module\\.exports\\.test' : 'exports\\.test';
40+
return extension === 'mjs' ? 'Module\\.test' : '(?:Module\\.)?exports\\.test';
4041
} else {
4142
return extension === 'mjs' ? 'Module\\.test' : 'Module\\.exports\\.test';
4243
}
@@ -194,7 +195,7 @@ async function compareStackTrace(sourceMap, source, expected) {
194195
fs.writeFileSync(`.generated-${id}-separate.${extension}`, `${header}${namedExportDeclaration()} = function() {` +
195196
source.join('\n') + `};//@ sourceMappingURL=.generated-${id}-separate.${extension}.map`);
196197
try {
197-
(await import(`./.generated-${id}-separate.${extension}`)).test();
198+
await (await import(`./.generated-${id}-separate.${extension}`)).test();
198199
} catch (e) {
199200
compareLines(e.stack.split(/\r\n|\n/), rewriteExpectation(expected, `.generated-${id}`, `.generated-${id}-separate`));
200201
}
@@ -204,7 +205,7 @@ async function compareStackTrace(sourceMap, source, expected) {
204205
source.join('\n') + '};//@ sourceMappingURL=data:application/json;base64,' +
205206
bufferFrom(sourceMap.toString()).toString('base64'));
206207
try {
207-
(await import (`./.generated-${id}-inline.${extension}`)).test();
208+
await (await import (`./.generated-${id}-inline.${extension}`)).test();
208209
} catch (e) {
209210
compareLines(e.stack.split(/\r\n|\n/), rewriteExpectation(expected, `.generated-${id}`, `.generated-${id}-inline`));
210211
}
@@ -239,6 +240,8 @@ function installSms() {
239240
emptyCacheBetweenOperations: true // Needed to be able to test for failure
240241
});
241242
}
243+
const installSmsOnce = once(installSms);
244+
242245

243246
function getTestMacros(sourceMapConstructors) {
244247
return {normalThrow, normalThrowWithoutSourceMapSupportInstalled};
@@ -255,7 +258,7 @@ async function normalThrowWithoutSourceMapSupportInstalled() {
255258
'throw new Error("test");'
256259
], [
257260
'Error: test',
258-
re`^ at ${stackFrameAtTest(false)} \(${stackFramePathStartsWith()}(?:.*[/\\])?\.generated-${id}\.${extension}:1:123\)$`
261+
re`^ at ${stackFrameAtTest()} \(${stackFramePathStartsWith()}(?:.*[/\\])?\.generated-${id}\.${extension}:1:123\)$`
259262
]);
260263
}
261264
}
@@ -318,15 +321,14 @@ describe('sourcemap style: file urls with absolute paths and sourceRoot removed,
318321

319322
function tests(sourceMapPostprocessor) {
320323
// let createEmptySourceMap, createMultiLineSourceMap, createMultiLineSourceMapWithSourcesContent, createSecondLineSourceMap, createSingleLineSourceMap, createSourceMapWithGap})
321-
const sourceMapConstructors = sourceMapCreators();
322-
for(const [key, value] of Object.entries(sourceMapConstructors)) {
323-
sourceMapConstructors[key] = (...args) => sourceMapPostprocessor(value(...args));
324-
}
324+
const sourceMapConstructors = mapValues(sourceMapCreators(), v => flow(v, sourceMapPostprocessor));
325325
const {createEmptySourceMap, createMultiLineSourceMap, createMultiLineSourceMapWithSourcesContent, createSecondLineSourceMap, createSingleLineSourceMap, createSourceMapWithGap} = sourceMapConstructors;
326326
const {normalThrow} = getTestMacros(sourceMapConstructors);
327327

328+
// Run as a hook to ensure it runs even when we execute a subset of tests
329+
before(installSmsOnce);
330+
328331
it('normal throw', async function() {
329-
installSms();
330332
await normalThrow();
331333
});
332334

@@ -459,6 +461,18 @@ it('function constructor', async function() {
459461
]);
460462
});
461463

464+
it('async stack frames', async function() {
465+
await compareStackTrace(createMultiLineSourceMap(), [
466+
'async function foo() { await bar(); }',
467+
'async function bar() { await null; throw new Error("test"); }',
468+
'return foo()'
469+
], [
470+
'Error: test',
471+
re`^ at bar \(${stackFramePathStartsWith()}(?:.*[/\\])?line2.js:1002:102\)$`,
472+
re`^ at async foo \(${stackFramePathStartsWith()}(?:.*[/\\])?line1.js:1001:101\)$`
473+
]);
474+
});
475+
462476
it('throw with empty source map', async function() {
463477
await compareStackTrace(createEmptySourceMap(), [
464478
'throw new Error("test");'
@@ -861,6 +875,7 @@ it('supports multiple instances', function(done) {
861875
}
862876

863877
describe('redirects require() of "source-map-support" to this module', function() {
878+
before(installSmsOnce);
864879
it('redirects', async function() {
865880
assert.strictEqual(require.resolve('source-map-support'), require.resolve('.'));
866881
assert.strictEqual(require.resolve('source-map-support/register'), require.resolve('./register'));
@@ -898,6 +913,7 @@ describe('uninstall', function() {
898913
const sourceMapConstructors = sourceMapCreators();
899914
const {normalThrow, normalThrowWithoutSourceMapSupportInstalled} = getTestMacros(sourceMapConstructors);
900915

916+
before(installSmsOnce);
901917
this.beforeEach(function() {
902918
underTest.uninstall();
903919
process.emit = priorProcessEmit;

0 commit comments

Comments
 (0)