Skip to content

Commit

Permalink
fix: function name should be obtained from next frams’s position
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Oct 1, 2019
1 parent 4a9c45d commit 3f4006b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
28 changes: 22 additions & 6 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,13 @@ function cloneCallSite(frame) {
return object;
}

function wrapCallSite(frame) {
function wrapCallSite(frame, state) {
// provides interface backward compatibility
if (state === undefined) {
state = { nextPosition: null, curPosition: null }
}
if(frame.isNative()) {
state.curPosition = null;
return frame;
}

Expand All @@ -360,9 +365,15 @@ function wrapCallSite(frame) {
line: line,
column: column
});
state.curPosition = position;
frame = cloneCallSite(frame);
var originalFunctionName = frame.getFunctionName;
frame.getFunctionName = function() { return position.name || originalFunctionName(); };
frame.getFunctionName = function() {
if (state.nextPosition === null) {
return originalFunctionName();
}
return state.nextPosition.name || originalFunctionName();
};
frame.getFileName = function() { return position.source; };
frame.getLineNumber = function() { return position.line; };
frame.getColumnNumber = function() { return position.column + 1; };
Expand Down Expand Up @@ -395,9 +406,14 @@ function prepareStackTrace(error, stack) {
var message = error.message || '';
var errorString = name + ": " + message;

return errorString + stack.map(function(frame) {
return '\n at ' + wrapCallSite(frame);
}).join('');
var state = { nextPosition: null, curPosition: null };
var processedStack = [];
for (var i = stack.length - 1; i >= 0; i--) {
processedStack.push('\n at ' + wrapCallSite(stack[i], state));
state.nextPosition = state.curPosition;
}
state.curPosition = state.nextPosition = null;
return errorString + processedStack.reverse().join('');
}

// Generate position and snippet of original source with pointer
Expand Down Expand Up @@ -561,7 +577,7 @@ exports.resetRetrieveHandlers = function() {

retrieveFileHandlers = originalRetrieveFileHandlers.slice(0);
retrieveMapHandlers = originalRetrieveMapHandlers.slice(0);

retrieveSourceMap = handlerExec(retrieveMapHandlers);
retrieveFile = handlerExec(retrieveFileHandlers);
}
9 changes: 7 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,12 @@ it('maps original name from source', function() {
generated: { line: 2, column: 8 },
original: { line: 1000, column: 10 },
source: '.original.js',
name: 'myOriginalName'
});
sourceMap.addMapping({
generated: { line: 4, column: 0 },
original: { line: 1002, column: 1 },
source: ".original.js",
name: "myOriginalName"
});
compareStackTrace(sourceMap, [
'function foo() {',
Expand All @@ -341,7 +346,7 @@ it('maps original name from source', function() {
], [
'Error: test',
/^ at myOriginalName \((?:.*[/\\])?\.original.js:1000:11\)$/,
/^ at Object\.exports\.test \((?:.*[/\\])?\.generated.js:4:1\)$/
/^ at Object\.exports\.test \((?:.*[/\\])?\.original.js:1002:2\)$/
]);
});

Expand Down

0 comments on commit 3f4006b

Please sign in to comment.