Skip to content

Commit 21c4dc1

Browse files
authored
Merge pull request #220 from Hoten/override-function-name
use original name from source in error trace
2 parents 3914e38 + 5eccbc7 commit 21c4dc1

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

source-map-support.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ function wrapCallSite(frame) {
362362
column: column
363363
});
364364
frame = cloneCallSite(frame);
365+
var originalFunctionName = frame.getFunctionName;
366+
frame.getFunctionName = function() { return position.name || originalFunctionName(); };
365367
frame.getFileName = function() { return position.source; };
366368
frame.getLineNumber = function() { return position.line; };
367369
frame.getColumnNumber = function() { return position.column + 1; };

test.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ it('throw with empty source map', function() {
278278
'throw new Error("test");'
279279
], [
280280
'Error: test',
281-
/^ at Object\.exports\.test \((?:.*[/\\])?.generated.js:1:34\)$/
281+
/^ at Object\.exports\.test \((?:.*[/\\])?\.generated.js:1:34\)$/
282282
]);
283283
});
284284

@@ -293,7 +293,7 @@ it('throw in Timeout with empty source map', function(done) {
293293
' throw new Error("this is the error")',
294294
/^ \^$/,
295295
'Error: this is the error',
296-
/^ at ((null)|(Timeout))\._onTimeout \((?:.*[/\\])?.generated.js:3:11\)$/
296+
/^ at ((null)|(Timeout))\._onTimeout \((?:.*[/\\])?.generated\.js:3:11\)$/
297297
]);
298298
});
299299

@@ -302,7 +302,7 @@ it('throw with source map with gap', function() {
302302
'throw new Error("test");'
303303
], [
304304
'Error: test',
305-
/^ at Object\.exports\.test \((?:.*[/\\])?.generated.js:1:34\)$/
305+
/^ at Object\.exports\.test \((?:.*[/\\])?\.generated\.js:1:34\)$/
306306
]);
307307
});
308308

@@ -311,7 +311,7 @@ it('sourcesContent with data URL', function() {
311311
'throw new Error("test");'
312312
], [
313313
'Error: test',
314-
/^ at Object\.exports\.test \((?:.*[/\\])?original.js:1001:5\)$/
314+
/^ at Object\.exports\.test \((?:.*[/\\])?original\.js:1001:5\)$/
315315
]);
316316
});
317317

@@ -321,7 +321,27 @@ it('finds the last sourceMappingURL', function() {
321321
'throw new Error("test");'
322322
], [
323323
'Error: test',
324-
/^ at Object\.exports\.test \((?:.*[/\\])?original.js:1002:5\)$/
324+
/^ at Object\.exports\.test \((?:.*[/\\])?original\.js:1002:5\)$/
325+
]);
326+
});
327+
328+
it('maps original name from source', function() {
329+
var sourceMap = createEmptySourceMap();
330+
sourceMap.addMapping({
331+
generated: { line: 2, column: 8 },
332+
original: { line: 1000, column: 10 },
333+
source: '.original.js',
334+
name: 'myOriginalName'
335+
});
336+
compareStackTrace(sourceMap, [
337+
'function foo() {',
338+
' throw new Error("test");',
339+
'}',
340+
'foo();'
341+
], [
342+
'Error: test',
343+
/^ at myOriginalName \((?:.*[/\\])?\.original.js:1000:11\)$/,
344+
/^ at Object\.exports\.test \((?:.*[/\\])?\.generated.js:4:1\)$/
325345
]);
326346
});
327347

@@ -337,7 +357,7 @@ it('default options', function(done) {
337357
'this is the original code',
338358
'^',
339359
'Error: this is the error',
340-
/^ at foo \((?:.*[/\\])?.original\.js:1:1\)$/
360+
/^ at foo \((?:.*[/\\])?\.original\.js:1:1\)$/
341361
]);
342362
});
343363

@@ -352,7 +372,7 @@ it('handleUncaughtExceptions is true', function(done) {
352372
'this is the original code',
353373
'^',
354374
'Error: this is the error',
355-
/^ at foo \((?:.*[/\\])?.original\.js:1:1\)$/
375+
/^ at foo \((?:.*[/\\])?\.original\.js:1:1\)$/
356376
]);
357377
});
358378

@@ -414,7 +434,7 @@ it('specifically requested error source', function(done) {
414434
'process.on("uncaughtException", function (e) { console.log("SRC:" + sms.getErrorSource(e)); });',
415435
'process.nextTick(foo);'
416436
], [
417-
/^SRC:.*[/\\].original.js:1$/,
437+
/^SRC:.*[/\\]\.original\.js:1$/,
418438
'this is the original code',
419439
'^'
420440
]);
@@ -488,9 +508,9 @@ it('should consult all retrieve source map providers', function(done) {
488508
'process.nextTick(function() { console.log(count); });',
489509
], [
490510
'Error: this is the error',
491-
/^ at foo \((?:.*[/\\])?original.js:1004:5\)$/,
511+
/^ at foo \((?:.*[/\\])?original\.js:1004:5\)$/,
492512
'Error: this is the error',
493-
/^ at foo \((?:.*[/\\])?original.js:1004:5\)$/,
513+
/^ at foo \((?:.*[/\\])?original\.js:1004:5\)$/,
494514
'1', // The retrieval should only be attempted once
495515
]);
496516
});
@@ -525,9 +545,9 @@ it('should allow for runtime inline source maps', function(done) {
525545
'require("./.generated.jss");',
526546
], [
527547
'Error: this is the error',
528-
/^ at foo \(.*[/\\]original.js:1004:5\)$/,
548+
/^ at foo \(.*[/\\]original\.js:1004:5\)$/,
529549
'Error: this is the error',
530-
/^ at foo \(.*[/\\]original.js:1004:5\)$/,
550+
/^ at foo \(.*[/\\]original\.js:1004:5\)$/,
531551
'0', // The retrieval should only be attempted once
532552
]);
533553
});

0 commit comments

Comments
 (0)