Skip to content

Commit

Permalink
Better fix for #36 (thanks @pspeter3).
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-f committed May 26, 2015
1 parent 6f539e6 commit 00cba19
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,6 @@ function retrieveSourceMap(source) {
}

function mapSourcePosition(position) {
// Fix position in Node where some (internal) code is prepended.
// See https://github.com/evanw/node-source-map-support/issues/36
if (!isInBrowser() && position.line === 1) {
position.column -= 62
}

var sourceMap = sourceMapCache[position.source];
if (!sourceMap) {
// Call the (overrideable) retrieveSourceMap function to get the source map.
Expand Down Expand Up @@ -271,16 +265,25 @@ function cloneCallSite(frame) {
return object;
}

function wrapCallSite(frame) {
function wrapCallSite(frame, fromModule) {
// Most call sites will return the source file from getFileName(), but code
// passed to eval() ending in "//# sourceURL=..." will return the source file
// from getScriptNameOrSourceURL() instead
var source = frame.getFileName() || frame.getScriptNameOrSourceURL();
if (source) {
var line = frame.getLineNumber();
var column = frame.getColumnNumber() - 1;

// Fix position in Node where some (internal) code is prepended.
// See https://github.com/evanw/node-source-map-support/issues/36
if (fromModule && line === 1) {
column -= 63;
}

var position = mapSourcePosition({
source: source,
line: frame.getLineNumber(),
column: frame.getColumnNumber() - 1
line: line,
column: column
});
frame = cloneCallSite(frame);
frame.getFileName = function() { return position.source; };
Expand Down Expand Up @@ -310,8 +313,15 @@ function prepareStackTrace(error, stack) {
fileContentsCache = {};
sourceMapCache = {};
}

var fromModule =
!isInBrowser() &&
stack.length &&
stack[stack.length - 1].getFileName() === 'module.js'
;

return error + stack.map(function(frame) {
return '\n at ' + wrapCallSite(frame);
return '\n at ' + wrapCallSite(frame, fromModule);
}).join('');
}

Expand Down

0 comments on commit 00cba19

Please sign in to comment.