Skip to content

Commit 3f4006b

Browse files
committed
fix: function name should be obtained from next frams’s position
1 parent 4a9c45d commit 3f4006b

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

source-map-support.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,13 @@ function cloneCallSite(frame) {
335335
return object;
336336
}
337337

338-
function wrapCallSite(frame) {
338+
function wrapCallSite(frame, state) {
339+
// provides interface backward compatibility
340+
if (state === undefined) {
341+
state = { nextPosition: null, curPosition: null }
342+
}
339343
if(frame.isNative()) {
344+
state.curPosition = null;
340345
return frame;
341346
}
342347

@@ -360,9 +365,15 @@ function wrapCallSite(frame) {
360365
line: line,
361366
column: column
362367
});
368+
state.curPosition = position;
363369
frame = cloneCallSite(frame);
364370
var originalFunctionName = frame.getFunctionName;
365-
frame.getFunctionName = function() { return position.name || originalFunctionName(); };
371+
frame.getFunctionName = function() {
372+
if (state.nextPosition === null) {
373+
return originalFunctionName();
374+
}
375+
return state.nextPosition.name || originalFunctionName();
376+
};
366377
frame.getFileName = function() { return position.source; };
367378
frame.getLineNumber = function() { return position.line; };
368379
frame.getColumnNumber = function() { return position.column + 1; };
@@ -395,9 +406,14 @@ function prepareStackTrace(error, stack) {
395406
var message = error.message || '';
396407
var errorString = name + ": " + message;
397408

398-
return errorString + stack.map(function(frame) {
399-
return '\n at ' + wrapCallSite(frame);
400-
}).join('');
409+
var state = { nextPosition: null, curPosition: null };
410+
var processedStack = [];
411+
for (var i = stack.length - 1; i >= 0; i--) {
412+
processedStack.push('\n at ' + wrapCallSite(stack[i], state));
413+
state.nextPosition = state.curPosition;
414+
}
415+
state.curPosition = state.nextPosition = null;
416+
return errorString + processedStack.reverse().join('');
401417
}
402418

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

562578
retrieveFileHandlers = originalRetrieveFileHandlers.slice(0);
563579
retrieveMapHandlers = originalRetrieveMapHandlers.slice(0);
564-
580+
565581
retrieveSourceMap = handlerExec(retrieveMapHandlers);
566582
retrieveFile = handlerExec(retrieveFileHandlers);
567583
}

test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,12 @@ it('maps original name from source', function() {
331331
generated: { line: 2, column: 8 },
332332
original: { line: 1000, column: 10 },
333333
source: '.original.js',
334-
name: 'myOriginalName'
334+
});
335+
sourceMap.addMapping({
336+
generated: { line: 4, column: 0 },
337+
original: { line: 1002, column: 1 },
338+
source: ".original.js",
339+
name: "myOriginalName"
335340
});
336341
compareStackTrace(sourceMap, [
337342
'function foo() {',
@@ -341,7 +346,7 @@ it('maps original name from source', function() {
341346
], [
342347
'Error: test',
343348
/^ at myOriginalName \((?:.*[/\\])?\.original.js:1000:11\)$/,
344-
/^ at Object\.exports\.test \((?:.*[/\\])?\.generated.js:4:1\)$/
349+
/^ at Object\.exports\.test \((?:.*[/\\])?\.original.js:1002:2\)$/
345350
]);
346351
});
347352

0 commit comments

Comments
 (0)