Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Apr 29, 2022
1 parent 69012b9 commit 0582d53
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
26 changes: 26 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
"@jridgewell/trace-mapping": "0.3.9"
},
"devDependencies": {
"@types/lodash": "^4.14.182",
"browserify": "^4.2.3",
"coffeescript": "^1.12.7",
"http-server": "^0.11.1",
"lodash": "^4.17.21",
"mocha": "^3.5.3",
"semver": "^7.3.7",
"source-map": "0.6.1",
Expand Down
8 changes: 6 additions & 2 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,12 @@ function mapEvalOrigin(origin) {
}

// This is copied almost verbatim from the V8 source code at
// https://code.google.com/p/v8/source/browse/trunk/src/messages.js. The
// implementation of wrapCallSite() used to just forward to the actual source
// https://code.google.com/p/v8/source/browse/trunk/src/messages.js
// Update 2022-04-29:
// https://github.com/v8/v8/blob/98f6f100c5ab8e390e51422747c4ef644d5ac6f2/src/builtins/builtins-callsite.cc#L175-L179
// https://github.com/v8/v8/blob/98f6f100c5ab8e390e51422747c4ef644d5ac6f2/src/objects/call-site-info.cc#L795-L804
// https://github.com/v8/v8/blob/98f6f100c5ab8e390e51422747c4ef644d5ac6f2/src/objects/call-site-info.cc#L717-L750
// The implementation of wrapCallSite() used to just forward to the actual source
// code of CallSite.prototype.toString but unfortunately a new release of V8
// did something to the prototype chain and broke the shim. The only fix I
// could find was copy/paste.
Expand Down
36 changes: 26 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var path = require('path');
const { pathToFileURL } = require('url');
var bufferFrom = Buffer.from;
const semver = require('semver');
const { once, mapValues, flow } = require('lodash');

// Helper to create regular expressions from string templates, to use interpolation
function re(...args) {
Expand All @@ -34,9 +35,9 @@ function namedExportDeclaration() {
* This varies across CJS / ESM and node versions.
* Example: ` at Module.exports.test (`...
*/
function stackFrameAtTest(sourceMapSupportInstalled = true) {
function stackFrameAtTest() {
if(semver.gte(process.versions.node, '18.0.0')) {
return extension === 'mjs' ? 'Module\\.test' : sourceMapSupportInstalled ? 'Module\\.exports\\.test' : 'exports\\.test';
return extension === 'mjs' ? 'Module\\.test' : '(?:Module\\.)?exports\\.test';
} else {
return extension === 'mjs' ? 'Module\\.test' : 'Module\\.exports\\.test';
}
Expand Down Expand Up @@ -194,7 +195,7 @@ async function compareStackTrace(sourceMap, source, expected) {
fs.writeFileSync(`.generated-${id}-separate.${extension}`, `${header}${namedExportDeclaration()} = function() {` +
source.join('\n') + `};//@ sourceMappingURL=.generated-${id}-separate.${extension}.map`);
try {
(await import(`./.generated-${id}-separate.${extension}`)).test();
await (await import(`./.generated-${id}-separate.${extension}`)).test();
} catch (e) {
compareLines(e.stack.split(/\r\n|\n/), rewriteExpectation(expected, `.generated-${id}`, `.generated-${id}-separate`));
}
Expand All @@ -204,7 +205,7 @@ async function compareStackTrace(sourceMap, source, expected) {
source.join('\n') + '};//@ sourceMappingURL=data:application/json;base64,' +
bufferFrom(sourceMap.toString()).toString('base64'));
try {
(await import (`./.generated-${id}-inline.${extension}`)).test();
await (await import (`./.generated-${id}-inline.${extension}`)).test();
} catch (e) {
compareLines(e.stack.split(/\r\n|\n/), rewriteExpectation(expected, `.generated-${id}`, `.generated-${id}-inline`));
}
Expand Down Expand Up @@ -239,6 +240,8 @@ function installSms() {
emptyCacheBetweenOperations: true // Needed to be able to test for failure
});
}
const installSmsOnce = once(installSms);


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

function tests(sourceMapPostprocessor) {
// let createEmptySourceMap, createMultiLineSourceMap, createMultiLineSourceMapWithSourcesContent, createSecondLineSourceMap, createSingleLineSourceMap, createSourceMapWithGap})
const sourceMapConstructors = sourceMapCreators();
for(const [key, value] of Object.entries(sourceMapConstructors)) {
sourceMapConstructors[key] = (...args) => sourceMapPostprocessor(value(...args));
}
const sourceMapConstructors = mapValues(sourceMapCreators(), v => flow(v, sourceMapPostprocessor));
const {createEmptySourceMap, createMultiLineSourceMap, createMultiLineSourceMapWithSourcesContent, createSecondLineSourceMap, createSingleLineSourceMap, createSourceMapWithGap} = sourceMapConstructors;
const {normalThrow} = getTestMacros(sourceMapConstructors);

// Run as a hook to ensure it runs even when we execute a subset of tests
before(installSmsOnce);

it('normal throw', async function() {
installSms();
await normalThrow();
});

Expand Down Expand Up @@ -459,6 +461,18 @@ it('function constructor', async function() {
]);
});

it('async stack frames', async function() {
await compareStackTrace(createMultiLineSourceMap(), [
'async function foo() { await bar(); }',
'async function bar() { await null; throw new Error("test"); }',
'return foo()'
], [
'Error: test',
re`^ at bar \(${stackFramePathStartsWith()}(?:.*[/\\])?line2.js:1002:102\)$`,
re`^ at async foo \(${stackFramePathStartsWith()}(?:.*[/\\])?line1.js:1001:101\)$`
]);
});

it('throw with empty source map', async function() {
await compareStackTrace(createEmptySourceMap(), [
'throw new Error("test");'
Expand Down Expand Up @@ -861,6 +875,7 @@ it('supports multiple instances', function(done) {
}

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

before(installSmsOnce);
this.beforeEach(function() {
underTest.uninstall();
process.emit = priorProcessEmit;
Expand Down

0 comments on commit 0582d53

Please sign in to comment.