Skip to content

Commit

Permalink
Add function name parsing for ES6 static functions
Browse files Browse the repository at this point in the history
- Add parsing for static functions
- Fix JSCS warnings

Issue: #37
  • Loading branch information
eriwen committed Dec 10, 2016
1 parent 041212f commit c61d2b0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
11 changes: 11 additions & 0 deletions spec/stacktrace-gps-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ describe('StackTraceGPS', function() {
}
});

it('finds function name within static functions', function(done) {
var stackframe = new StackFrame({args: [], fileName: 'http://localhost:9999/file.js', lineNumber: 2, columnNumber: 8});
var sourceCache = {'http://localhost:9999/file.js': 'class Foo {\nstatic woof() {\n}\n}'};
new StackTraceGPS({sourceCache: sourceCache}).findFunctionName(stackframe).then(callback, done.fail);

function callback(stackframe) {
expect(stackframe.functionName).toEqual('woof');
done();
}
});

it('ignores commented out function definitions', function(done) {
var source = 'var foo = function() {};\n//function bar() {}\nvar baz = eval("XXX")';
jasmine.Ajax.stubRequest('http://localhost:9999/file.js').andReturn({responseText: source});
Expand Down
34 changes: 18 additions & 16 deletions stacktrace-gps.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@

function _findFunctionName(source, lineNumber/*, columnNumber*/) {
var syntaxes = [
// {name} = function ({args}) TODO args capture
/['"]?([$_A-Za-z][$_A-Za-z0-9]*)['"]?\s*[:=]\s*function\b/,
// function {name}({args}) m[1]=name m[2]=args
/function\s+([^('"`]*?)\s*\(([^)]*)\)/,
// {name} = eval()
/['"]?([$_A-Za-z][$_A-Za-z0-9]*)['"]?\s*[:=]\s*(?:eval|new Function)\b/,
// fn_name() {
/\b(?!(?:if|for|switch|while|with|catch)\b)(\S+)\s*\(.*?\)\s*{/,
// {name} = () => {
/['"]?([$_A-Za-z][$_A-Za-z0-9]*)['"]?\s*[:=]\s*\(.*?\)\s*=>/
// {name} = function ({args}) TODO args capture
/['"]?([$_A-Za-z][$_A-Za-z0-9]*)['"]?\s*[:=]\s*function\b/,
// function {name}({args}) m[1]=name m[2]=args
/function\s+([^('"`]*?)\s*\(([^)]*)\)/,
// {name} = eval()
/['"]?([$_A-Za-z][$_A-Za-z0-9]*)['"]?\s*[:=]\s*(?:eval|new Function)\b/,
// fn_name() {
/\b(?!(?:if|for|switch|while|with|catch)\b)(?:(?:static)\s+)?(\S+)\s*\(.*?\)\s*\{/,
// {name} = () => {
/['"]?([$_A-Za-z][$_A-Za-z0-9]*)['"]?\s*[:=]\s*\(.*?\)\s*=>/
];
var lines = source.split('\n');

Expand All @@ -91,10 +91,10 @@
code = line + code;
var len = syntaxes.length;
for (var index = 0; index < len; index++) {
var m = syntaxes[index].exec(code);
if (m && m[1]) {
return m[1];
}
var m = syntaxes[index].exec(code);
if (m && m[1]) {
return m[1];
}
}
}
}
Expand Down Expand Up @@ -153,7 +153,8 @@
args: stackframe.args,
fileName: loc.source,
lineNumber: loc.line,
columnNumber: loc.column}));
columnNumber: loc.column
}));
} else {
reject(new Error('Could not get original source for given stackframe and source map'));
}
Expand Down Expand Up @@ -254,7 +255,8 @@
args: stackframe.args,
fileName: stackframe.fileName,
lineNumber: lineNumber,
columnNumber: columnNumber}));
columnNumber: columnNumber
}));
} else {
resolve(stackframe);
}
Expand Down

0 comments on commit c61d2b0

Please sign in to comment.