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

Stack traces for input source maps #273

Merged
merged 2 commits into from
Nov 26, 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
14 changes: 12 additions & 2 deletions lib/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ var options = {
blacklist: hasGenerator ? ['regenerator'] : [],
optional: hasGenerator ? ['asyncToGenerator', 'runtime'] : ['runtime'],
plugins: [powerAssert],
sourceMaps: true
sourceMaps: true,
inputSourceMap: null
};

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

// try to load an input source map for the test file, in case the file was
// already compiled once by the user
var inputSourceMap = sourceMapSupport.retrieveSourceMap(testPath);
if (inputSourceMap) {
// source-map-support returns the source map as a json-encoded string, but
// babel requires an actual object
options.inputSourceMap = JSON.parse(inputSourceMap.map);
}

// include test file
var transpiled = babel.transformFileSync(testPath, options);
sourceMapCache[testPath] = JSON.stringify(transpiled.map);
sourceMapCache[testPath] = transpiled.map;
requireFromString(transpiled.code, testPath, {
appendPaths: module.paths
});
Expand Down
10 changes: 10 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ test('stack traces for exceptions are corrected using a source map', function (t
});
});

test('stack traces for exceptions are corrected using a source map, taking an initial source map for the test file into account', function (t) {
execCli('fixture/initial-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.*initial-source-map-exception.js:12.*$/m);
t.match(stderr, /^.*?at.*?foo\b.*initial-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);
Expand Down
33 changes: 33 additions & 0 deletions test/fixture/initial-source-map-exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

var test = require('../../');

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

function foo() {
bar();
}

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

//# sourceMappingURL=./initial-source-map-exception.map

/* original source:
const test = require('../../');

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

function foo() {
bar();
}

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

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