Skip to content

Commit c61d2b0

Browse files
committed
Add function name parsing for ES6 static functions
- Add parsing for static functions - Fix JSCS warnings Issue: #37
1 parent 041212f commit c61d2b0

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

spec/stacktrace-gps-spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ describe('StackTraceGPS', function() {
164164
}
165165
});
166166

167+
it('finds function name within static functions', function(done) {
168+
var stackframe = new StackFrame({args: [], fileName: 'http://localhost:9999/file.js', lineNumber: 2, columnNumber: 8});
169+
var sourceCache = {'http://localhost:9999/file.js': 'class Foo {\nstatic woof() {\n}\n}'};
170+
new StackTraceGPS({sourceCache: sourceCache}).findFunctionName(stackframe).then(callback, done.fail);
171+
172+
function callback(stackframe) {
173+
expect(stackframe.functionName).toEqual('woof');
174+
done();
175+
}
176+
});
177+
167178
it('ignores commented out function definitions', function(done) {
168179
var source = 'var foo = function() {};\n//function bar() {}\nvar baz = eval("XXX")';
169180
jasmine.Ajax.stubRequest('http://localhost:9999/file.js').andReturn({responseText: source});

stacktrace-gps.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@
6363

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

@@ -91,10 +91,10 @@
9191
code = line + code;
9292
var len = syntaxes.length;
9393
for (var index = 0; index < len; index++) {
94-
var m = syntaxes[index].exec(code);
95-
if (m && m[1]) {
96-
return m[1];
97-
}
94+
var m = syntaxes[index].exec(code);
95+
if (m && m[1]) {
96+
return m[1];
97+
}
9898
}
9999
}
100100
}
@@ -153,7 +153,8 @@
153153
args: stackframe.args,
154154
fileName: loc.source,
155155
lineNumber: loc.line,
156-
columnNumber: loc.column}));
156+
columnNumber: loc.column
157+
}));
157158
} else {
158159
reject(new Error('Could not get original source for given stackframe and source map'));
159160
}
@@ -254,7 +255,8 @@
254255
args: stackframe.args,
255256
fileName: stackframe.fileName,
256257
lineNumber: lineNumber,
257-
columnNumber: columnNumber}));
258+
columnNumber: columnNumber
259+
}));
258260
} else {
259261
resolve(stackframe);
260262
}

0 commit comments

Comments
 (0)