Skip to content

Commit

Permalink
Fixing node header issue.
Browse files Browse the repository at this point in the history
Node adds a header of 62 characters when code is required through a
native module. If we are in node, the line number is 1 and the next file
is the native 'module.js', we subtract that header. I verified the
solution for evanw#36
by reproducing the problem. The stack trace is now:

```
user:node-source-map-support user$ node error.js
1

/Users/user/Documents/node-source-map-support/uglifysanity.js:2
goober();
^
ReferenceError: goober is not defined
at Object.<anonymous>
(/Users/user/Documents/node-source-map-support/uglifysanity.js:2:1)
```
  • Loading branch information
pspeter3 committed May 16, 2015
1 parent 362a9e3 commit 4596a38
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,21 @@ function cloneCallSite(frame) {
return object;
}

function wrapCallSite(frame) {
function wrapCallSite(frame, isFromModule) {
// 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 position = mapSourcePosition({
var position = {
source: source,
line: frame.getLineNumber(),
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
// The 63 is because node has a 62 module header.
column: frame.getColumnNumber() - (isFromModule ? 63 : 1)
};
position = mapSourcePosition(position);
frame = cloneCallSite(frame);
frame.getFileName = function() { return position.source; };
frame.getLineNumber = function() { return position.line; };
Expand Down Expand Up @@ -300,8 +304,14 @@ function prepareStackTrace(error, stack) {
fileContentsCache = {};
sourceMapCache = {};
}
return error + stack.map(function(frame) {
return '\n at ' + wrapCallSite(frame);
return error + stack.map(function(frame, index) {
// Fix position in Node where some (internal) code is prepended.
// See https://github.com/evanw/node-source-map-support/issues/36
var isFromModule = (!isInBrowser() &&
frame.getLineNumber() === 1 &&
index + 1 < stack.length &&
stack[index + 1].getFileName() === 'module.js');
return '\n at ' + wrapCallSite(frame, isFromModule);
}).join('');
}

Expand Down

0 comments on commit 4596a38

Please sign in to comment.