Skip to content

Commit a2410ad

Browse files
author
homyakov
committed
Fixed bug in findFunctionName with commented code
1 parent 0dff212 commit a2410ad

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

‎stacktrace.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,15 @@ printStackTrace.implementation.prototype = {
380380
var reFunctionEvaluation = /['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*(?:eval|new Function)\b/;
381381
// Walk backwards in the source lines until we find
382382
// the line which matches one of the patterns above
383-
var code = "", line, maxLines = 20, m;
383+
var code = "", line, maxLines = Math.min(lineNo, 20), m, commentPos;
384384
for (var i = 0; i < maxLines; ++i) {
385385
// lineNo is 1-based, source[] is 0-based
386386
line = source[lineNo - i - 1];
387-
// line.replace(/(.*)\/\//, '$1')
388-
// FIXME skip/clean comments? Commented code may lead to false positive
387+
commentPos = line.indexOf('//');
388+
if (commentPos >= 0) {
389+
line = line.substr(0, commentPos);
390+
}
391+
// TODO check other types of comments? Commented code may lead to false positive
389392
if (line) {
390393
code = line + code;
391394
m = reFunctionExpression.exec(code);

‎test/TestStacktrace.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,14 +559,15 @@
559559
});
560560

561561
test("findFunctionName", function() {
562-
expect(12);
562+
expect(13);
563563
equals(pst.findFunctionName(['var a = function aa() {', 'var b = 2;', '};'], 2), 'a');
564564
equals(pst.findFunctionName(['var a = function () {', 'var b = 2;', '};'], 2), 'a');
565565
equals(pst.findFunctionName(['var a = function() {', 'var b = 2;', '};'], 2), 'a');
566566
// FIXME: currently failing because we don't have a way to distinguish which fn is being sought
567567
// equals(pst.findFunctionName(['a:function(){},b:function(){', '};'], 1), 'b');
568568
equals(pst.findFunctionName(['"a": function(){', '};'], 1), 'a');
569569

570+
// different formatting
570571
equals(pst.findFunctionName(['function a() {', 'var b = 2;', '}'], 2), 'a');
571572
equals(pst.findFunctionName(['function a(b,c) {', 'var b = 2;', '}'], 2), 'a');
572573
equals(pst.findFunctionName(['function a () {', '}'], 2), 'a');
@@ -576,8 +577,10 @@
576577
equals(pst.findFunctionName(['var data = new Function("return true;");', ''], 1), 'data');
577578
equals(pst.findFunctionName(['var data = new Function("s,r",', '"return s + r;");'], 1), 'data');
578579

580+
// not found
579581
equals(pst.findFunctionName(['var a = 1;', 'var b = 2;', 'var c = 3;'], 2), '(?)');
580582

583+
// false positive in comment
581584
equals(pst.findFunctionName(['function a() {', ' // function commented()', ' error here', '}'], 3), 'a');
582585
});
583586

0 commit comments

Comments
 (0)