10000 Add support for parsing ES6 style functions. · stacktracejs/stacktrace-gps@0f081dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f081dc

Browse files
amjitheriwen
authored andcommitted
Add support for parsing ES6 style functions.
1 parent 1861a31 commit 0f081dc

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

spec/stacktrace-gps-spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,32 @@ describe('StackTraceGPS', function() {
9090
}
9191
});
9292

93+
it('resolves source for class method', function(done) {
94+
var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 2, 0);
95+
var sourceCache = {'http://localhost:9999/file.js': 'class Foo {\nbar () {\n}\n }\n'};
96+
new StackTraceGPS({sourceCache: sourceCache})
97+
.findFunctionName(stackframe)
98+
.then(callback, done.fail);
99+
100+
function callback(stackframe) {
101+
expect(stackframe).toEqual(new StackFrame('bar', [], 'http://localhost:9999/file.js', 2, 0));
102+
done();
103+
}
104+
});
105+
106+
it('resolves source for fat arrow functions', function(done) {
107+
var stackframe = new StackFrame(undefined, [], 'http://localhost:9999/file.js', 1, 4);
108+
var sourceCache = {'http://localhost:9999/file.js': 'var meow = () => { }'};
109+
new StackTraceGPS({sourceCache: sourceCache})
110+
.findFunctionName(stackframe)
111+
.then(callback, done.fail);
112+
113+
function callback(stackframe) {
114+
expect(stackframe).toEqual(new StackFrame('meow', [], 'http://localhost:9999/file.js', 1, 4));
115+
done();
116+
}
117+
});
118+
93119
it('finds function name within function expression', function(done) {
94120
var source = 'var foo = function() {};\nfunction bar() {}\nvar baz = eval("XXX")';
95121
jasmine.Ajax.stubRequest('http://localhost:9999/file.js').andReturn({responseText: source});

stacktrace-gps.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,16 @@
6969
/function\s+([^(]*?)\s*\(([^)]*)\)/,
7070
// {name} = eval()
7171
/['"]?([$_A-Za-z][$_A-Za-z0-9]*)['"]?\s*[:=]\s*(?:eval|new Function)\b/,
72-
]
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*=>/
76+
];
7377
var lines = source.split('\n');
7478

7579
// Walk backwards in the source lines until we find the line which matches one of the patterns above
7680
var code = '';
7781
var maxLines = Math.min(lineNumber, 20);
78-
var m;
7982
for (var i = 0; i < maxLines; ++i) {
8083
// lineNo is 1-based, source[] is 0-based
8184
var line = lines[lineNumber - i - 1];
@@ -87,8 +90,8 @@
8790
if (line) {
8891
code = line + code;
8992
var len = syntaxes.length;
90-
for (var i = 0; i < len; i++) {
91-
var m = syntaxes[i].exec(code);
93+
for (var index = 0; index < len; index++) {
94+
var m = syntaxes[index].exec(code);
9295
if (m && m[1]) {
9396
return m[1];
9497
}

0 commit comments

Comments
 (0)
0