Skip to content

Commit fb98d5d

Browse files
committed
Merge pull request #262 from novemberborn/source-map-pragma
Support source map pragmas for stack traces
2 parents a4f8e24 + 5b1e5b4 commit fb98d5d

7 files changed

+71
-13
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

+20-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ test('uncaught exception will be reported to console', function (t) {
166166
execCli('fixture/uncaught-exception.js', function (err, stdout, stderr) {
167167
t.ok(err);
168168
t.true(/Can't catch me!/.test(stderr));
169-
t.match(stderr, /^.*?at.*?bar\b.*uncaught-exception.js:12.*$/m);
170-
t.match(stderr, /^.*?at.*?foo\b.*uncaught-exception.js:8.*$/m);
171169
// TODO(jamestalmage): This should get printed, but we reject the promise (ending all tests) instead of just ending that one test and reporting.
172170
// t.ok(/1 uncaught exception[^s]/.test(stdout));
173171
t.end();
@@ -194,6 +192,26 @@ test('throwing a anonymous function will report the function to the console', fu
194192
});
195193
});
196194

195+
test('stack traces for exceptions are corrected using a source map', function (t) {
196+
execCli('fixture/source-map-exception.js', function (err, stdout, stderr) {
197+
t.ok(err);
198+
t.true(/Can't catch me!/.test(stderr));
199+
t.match(stderr, /^.*?at.*?bar\b.*source-map-exception.js:12.*$/m);
200+
t.match(stderr, /^.*?at.*?foo\b.*source-map-exception.js:8.*$/m);
201+
t.end();
202+
});
203+
});
204+
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+
197215
test('absolute paths in CLI', function (t) {
198216
t.plan(2);
199217

test/fixture/source-map-exception.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const test = require('../../');
2+
3+
test('throw an uncaught exception', t => {
4+
setImmediate(foo);
5+
});
6+
7+
function foo() {
8+
bar();
9+
}
10+
11+
function bar() {
12+
throw new Error(`Can't catch me!`)
13+
}
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

test/fixture/uncaught-exception.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
const test = require('../../');
22

33
test('throw an uncaught exception', t => {
4-
setImmediate(foo);
4+
setImmediate(() => {
5+
throw new Error(`Can't catch me!`)
6+
});
57
});
6-
7-
function foo() {
8-
bar();
9-
}
10-
11-
function bar() {
12-
throw new Error(`Can't catch me!`)
13-
}

0 commit comments

Comments
 (0)