Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support source map pragmas for stack traces #262

Merged
merged 2 commits into from
Nov 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/babel.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict';
var sourceMapCache = Object.create(null);

require('source-map-support').install({
var sourceMapSupport = require('source-map-support');
sourceMapSupport.install({
retrieveSourceMap: function (source) {
if (sourceMapCache[source]) {
return {
url: source,
map: sourceMapCache[source]
};
}
return null;
return sourceMapSupport.retrieveSourceMap(source);
}
});

Expand Down
22 changes: 20 additions & 2 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ test('uncaught exception will be reported to console', function (t) {
execCli('fixture/uncaught-exception.js', function (err, stdout, stderr) {
t.ok(err);
t.true(/Can't catch me!/.test(stderr));
t.match(stderr, /^.*?at.*?bar\b.*uncaught-exception.js:12.*$/m);
t.match(stderr, /^.*?at.*?foo\b.*uncaught-exception.js:8.*$/m);
// TODO(jamestalmage): This should get printed, but we reject the promise (ending all tests) instead of just ending that one test and reporting.
// t.ok(/1 uncaught exception[^s]/.test(stdout));
t.end();
Expand All @@ -194,6 +192,26 @@ test('throwing a anonymous function will report the function to the console', fu
});
});

test('stack traces for exceptions are corrected using a source map', function (t) {
execCli('fixture/source-map-exception.js', function (err, stdout, stderr) {
t.ok(err);
t.true(/Can't catch me!/.test(stderr));
t.match(stderr, /^.*?at.*?bar\b.*source-map-exception.js:12.*$/m);
t.match(stderr, /^.*?at.*?foo\b.*source-map-exception.js:8.*$/m);
t.end();
});
});

test('stack traces for exceptions are corrected using a source map, found via a pragma', function (t) {
execCli('fixture/source-map-pragma-exception.js', function (err, stdout, stderr) {
t.ok(err);
t.true(/Can't catch me!/.test(stderr));
t.match(stderr, /^.*?at.*?bar\b.*source-with-source-map-pragma.js:8.*$/m);
t.match(stderr, /^.*?at.*?foo\b.*source-with-source-map-pragma.js:4.*$/m);
t.end();
});
});

test('absolute paths in CLI', function (t) {
t.plan(2);

Expand Down
13 changes: 13 additions & 0 deletions test/fixture/source-map-exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const test = require('../../');

test('throw an uncaught exception', t => {
setImmediate(foo);
});

function foo() {
bar();
}

function bar() {
throw new Error(`Can't catch me!`)
}
6 changes: 6 additions & 0 deletions test/fixture/source-map-pragma-exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const test = require('../../');
const foo = require('./source-with-source-map-pragma');

test('throw an uncaught exception', t => {
setImmediate(foo);
});
25 changes: 25 additions & 0 deletions test/fixture/source-with-source-map-pragma.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

module.exports = foo;

function foo() {
bar();
}

function bar() {
throw new Error("Can't catch me!");
}

//# sourceMappingURL=./source-with-source-map-pragma.map

/* original source:
module.exports = foo

function foo() {
bar()
}

function bar() {
throw new Error(`Can't catch me!`)
}
*/
1 change: 1 addition & 0 deletions test/fixture/source-with-source-map-pragma.map

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

12 changes: 3 additions & 9 deletions test/fixture/uncaught-exception.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
const test = require('../../');

test('throw an uncaught exception', t => {
setImmediate(foo);
setImmediate(() => {
throw new Error(`Can't catch me!`)
});
});

function foo() {
bar();
}

function bar() {
throw new Error(`Can't catch me!`)
}