Skip to content

Commit 613caf9

Browse files
author
homyakov
committed
More tests for findFunctionName
1 parent 203c03b commit 613caf9

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

‎stacktrace.js‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,30 @@ printStackTrace.implementation.prototype = {
333333
},
334334

335335
findFunctionName: function(source, lineNo) {
336-
// TODO use stringifyArguments
337-
var reFunctionArgNames = /function ([^(]*)\(([^)]*)\)/;
338-
var reGuessFunction = /['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*(function|eval|new Function)/;
336+
// TODO use captured args
337+
// function {name}({args}) m[1]=name m[2]=args
338+
var reFunctionDeclaration = /function\s+([^(]*?)\s*\(([^)]*)\)/;
339+
// {name} = function ({args}) TODO args capture
340+
var reFunctionExpression = /['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*function(?:[^(]*)/;
341+
// {name} = eval()
342+
var reFunctionEvaluation = /['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*(?:eval|new Function)/;
339343
// Walk backwards in the source lines until we find
340344
// the line which matches one of the patterns above
341-
var line = "", maxLines = 10, m;
345+
var code = "", line, maxLines = 10, m;
342346
for (var i = 0; i < maxLines; ++i) {
343-
line = source[lineNo - i] + line;
344-
if (line !== undefined) {
345-
m = reGuessFunction.exec(line);
347+
line = source[lineNo - i];
348+
if (line) {
349+
code = line + code;
350+
m = reFunctionExpression.exec(code);
346351
if (m && m[1]) {
347352
return m[1];
348353
}
349-
m = reFunctionArgNames.exec(line);
354+
m = reFunctionDeclaration.exec(code);
355+
if (m && m[1]) {
356+
//return m[1] + "(" + (m[2] || "") + ")";
357+
return m[1];
358+
}
359+
m = reFunctionEvaluation.exec(code);
350360
if (m && m[1]) {
351361
return m[1];
352362
}

‎test/test-stacktrace.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,21 @@
440440
});
441441

442442
test("findFunctionName", function() {
443-
expect(5);
443+
expect(12);
444444
equals(pst.findFunctionName(['var a = function aa() {', 'var b = 2;', '};'], 2), 'a');
445445
equals(pst.findFunctionName(['var a = function () {', 'var b = 2;', '};'], 2), 'a');
446446
equals(pst.findFunctionName(['var a = function() {', 'var b = 2;', '};'], 2), 'a');
447-
equals(pst.findFunctionName(['function a() {', 'var b = 2;', '};'], 2), 'a');
447+
equals(pst.findFunctionName(['"a": function(){', '};'], 1), 'a');
448+
449+
equals(pst.findFunctionName(['function a() {', 'var b = 2;', '}'], 2), 'a');
450+
equals(pst.findFunctionName(['function a(b,c) {', 'var b = 2;', '}'], 2), 'a');
451+
equals(pst.findFunctionName(['function a () {', '}'], 2), 'a');
452+
equals(pst.findFunctionName(['function\ta\t()\t{', '}'], 2), 'a');
453+
equals(pst.findFunctionName([' function', ' a', ' ()', ' {', ' }'], 3), 'a');
454+
455+
equals(pst.findFunctionName(['var data = new Function("return true;");', ''], 1), 'data');
456+
equals(pst.findFunctionName(['var data = new Function("s,r",', '"return s + r;");'], 1), 'data');
457+
448458
equals(pst.findFunctionName(['var a = 1;', 'var b = 2;', 'var c = 3;'], 2), '(?)');
449459
});
450460

0 commit comments

Comments
 (0)