Skip to content

Commit 84fb575

Browse files
committed
Merge pull request #273 from novemberborn/stack-traces-for-input-sourcemaps
Stack traces for input source maps
2 parents 7764336 + b3cee51 commit 84fb575

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

lib/babel.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ var options = {
4848
blacklist: hasGenerator ? ['regenerator'] : [],
4949
optional: hasGenerator ? ['asyncToGenerator', 'runtime'] : ['runtime'],
5050
plugins: [powerAssert],
51-
sourceMaps: true
51+
sourceMaps: true,
52+
inputSourceMap: null
5253
};
5354

5455
// check if test files required ava and show error, when they didn't
@@ -58,9 +59,18 @@ process.on('uncaughtException', function (exception) {
5859
send('uncaughtException', {exception: serializeError(exception)});
5960
});
6061

62+
// try to load an input source map for the test file, in case the file was
63+
// already compiled once by the user
64+
var inputSourceMap = sourceMapSupport.retrieveSourceMap(testPath);
65+
if (inputSourceMap) {
66+
// source-map-support returns the source map as a json-encoded string, but
67+
// babel requires an actual object
68+
options.inputSourceMap = JSON.parse(inputSourceMap.map);
69+
}
70+
6171
// include test file
6272
var transpiled = babel.transformFileSync(testPath, options);
63-
sourceMapCache[testPath] = JSON.stringify(transpiled.map);
73+
sourceMapCache[testPath] = transpiled.map;
6474
requireFromString(transpiled.code, testPath, {
6575
appendPaths: module.paths
6676
});

test/cli.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ test('stack traces for exceptions are corrected using a source map', function (t
205205
});
206206
});
207207

208+
test('stack traces for exceptions are corrected using a source map, taking an initial source map for the test file into account', function (t) {
209+
execCli('fixture/initial-source-map-exception.js', function (err, stdout, stderr) {
210+
t.ok(err);
211+
t.true(/Can't catch me!/.test(stderr));
212+
t.match(stderr, /^.*?at.*?bar\b.*initial-source-map-exception.js:12.*$/m);
213+
t.match(stderr, /^.*?at.*?foo\b.*initial-source-map-exception.js:8.*$/m);
214+
t.end();
215+
});
216+
});
217+
208218
test('stack traces for exceptions are corrected using a source map, found via a pragma', function (t) {
209219
execCli('fixture/source-map-pragma-exception.js', function (err, stdout, stderr) {
210220
t.ok(err);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
var test = require('../../');
4+
5+
test('throw an uncaught exception', function (t) {
6+
setImmediate(foo);
7+
});
8+
9+
function foo() {
10+
bar();
11+
}
12+
13+
function bar() {
14+
throw new Error('Can\'t catch me!');
15+
}
16+
17+
//# sourceMappingURL=./initial-source-map-exception.map
18+
19+
/* original source:
20+
const test = require('../../');
21+
22+
test('throw an uncaught exception', t => {
23+
setImmediate(foo);
24+
});
25+
26+
function foo() {
27+
bar();
28+
}
29+
30+
function bar() {
31+
throw new Error(`Can't catch me!`)
32+
}
33+
*/

test/fixture/initial-source-map-exception.map

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

0 commit comments

Comments
 (0)