Skip to content

Commit 5b1e5b4

Browse files
committed
Support source map pragmas for stack traces
When using source maps to compute stack traces, if the source map is not in memory, fall back to source-map-support's default retrieval function which can get it from a source map pragma in the source file. This means any dependency that has such pragmas (and the required map files) has its errors mapped correctly, e.g. the code under test, or third-party dependencies that ship with source maps. This should also work for inline source maps, though no test case was included.
1 parent 8c5f6e3 commit 5b1e5b4

5 files changed

+45
-2
lines changed

lib/babel.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict';
22
var sourceMapCache = Object.create(null);
33

4-
require('source-map-support').install({
4+
var sourceMapSupport = require('source-map-support');
5+
sourceMapSupport.install({
56
retrieveSourceMap: function (source) {
67
if (sourceMapCache[source]) {
78
return {
89
url: source,
910
map: sourceMapCache[source]
1011
};
1112
}
12-
return null;
13+
return sourceMapSupport.retrieveSourceMap(source);
1314
}
1415
});
1516

test/cli.js

+10
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ test('stack traces for exceptions are corrected using a source map', function (t
202202
});
203203
});
204204

205+
test('stack traces for exceptions are corrected using a source map, found via a pragma', function (t) {
206+
execCli('fixture/source-map-pragma-exception.js', function (err, stdout, stderr) {
207+
t.ok(err);
208+
t.true(/Can't catch me!/.test(stderr));
209+
t.match(stderr, /^.*?at.*?bar\b.*source-with-source-map-pragma.js:8.*$/m);
210+
t.match(stderr, /^.*?at.*?foo\b.*source-with-source-map-pragma.js:4.*$/m);
211+
t.end();
212+
});
213+
});
214+
205215
test('absolute paths in CLI', function (t) {
206216
t.plan(2);
207217

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const test = require('../../');
2+
const foo = require('./source-with-source-map-pragma');
3+
4+
test('throw an uncaught exception', t => {
5+
setImmediate(foo);
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
module.exports = foo;
4+
5+
function foo() {
6+
bar();
7+
}
8+
9+
function bar() {
10+
throw new Error("Can't catch me!");
11+
}
12+
13+
//# sourceMappingURL=./source-with-source-map-pragma.map
14+
15+
/* original source:
16+
module.exports = foo
17+
18+
function foo() {
19+
bar()
20+
}
21+
22+
function bar() {
23+
throw new Error(`Can't catch me!`)
24+
}
25+
*/

test/fixture/source-with-source-map-pragma.map

+1
Original file line numberDiff line numberDiff line change

0 commit comments

Comments
 (0)