Skip to content

Commit 4b3205f

Browse files
committed
Add JSC stack trace parser to fix last failing tests
1 parent 414f565 commit 4b3205f

File tree

2 files changed

+23
-36
lines changed

2 files changed

+23
-36
lines changed

SourceMaps.StackTraces.Tests/StackTraceParserTests.cs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SourceMaps.StackTraces.Tests
99
public class StackTraceParserTests
1010
{
1111
[Theory]
12-
[MemberData(nameof(SingleLineStackTraceData))]
12+
[MemberData(nameof(StackTraceData))]
1313
public void ParseTests(string stacktrace, List<StackFrame> expected)
1414
{
1515
var actual = StackTraceParser.Parse(stacktrace).Frames;
@@ -32,34 +32,6 @@ public void ParseTests(string stacktrace, List<StackFrame> expected)
3232
}
3333
}
3434

35-
public static IEnumerable<object[]> SingleLineStackTraceData()
36-
{
37-
foreach (var data in StackTraceData())
38-
{
39-
var stacktrace = (string) data[0];
40-
var expected = (List<StackFrame>) data[1];
41-
42-
var lines = stacktrace.Split('\n');
43-
if (expected.Count != lines.Length && expected.Count != lines.Length - 1)
44-
throw new Exception($"Invalid number of stack frames: {stacktrace}");
45-
46-
var offset = 0;
47-
if (expected.Count == lines.Length - 1)
48-
offset = 1;
49-
50-
for (var i = 0; i+offset < lines.Length; i++)
51-
{
52-
var line = lines[i + offset];
53-
var frame = expected[i];
54-
55-
if (string.IsNullOrWhiteSpace(line))
56-
throw new Exception($"Invalid stack trace line:{i+offset}\n{stacktrace}");
57-
58-
yield return new object[] {line.Trim(), new List<StackFrame> {frame}};
59-
}
60-
}
61-
}
62-
6335
public static IEnumerable<object[]> StackTraceData()
6436
{
6537
// Firefox
@@ -139,7 +111,6 @@ at throwErr (https://localhost:5001/dist/site.js:1:45)
139111
LineNumber = 43,
140112
ColumnNumber = 36,
141113
},
142-
null,
143114
new StackFrame
144115
{
145116
File = "C:\\\\project files\\\\spect\\\\src\\\\index.js",
@@ -1381,11 +1352,6 @@ at throwErr (https://localhost:5001/dist/site.js:1:45)
13811352
Arguments = Array.Empty<string>(), LineNumber = 43, ColumnNumber = 36
13821353
},
13831354
new StackFrame
1384-
{
1385-
File = null, Method = "<anonymous>",
1386-
Arguments = Array.Empty<string>(), LineNumber = null, ColumnNumber = null
1387-
},
1388-
new StackFrame
13891355
{
13901356
File = "C:\\\\projects\\\\spect\\\\src\\\\index.js",
13911357
Method = "(anonymous function).then",

SourceMaps.StackTraces/StackTraceParser.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public static StackTrace Parse(string stacktrace)
5252
TryParseChrome(line, out frame) ||
5353
TryParseWinJs(line, out frame) ||
5454
TryParseGecko(line, out frame) ||
55-
TryParseNode(line, out frame);
55+
TryParseNode(line, out frame) ||
56+
TryParseJsc(line, out frame);
5657

5758
if (success)
5859
result.Append(frame);
@@ -163,5 +164,25 @@ internal static bool TryParseNode(string line, out StackFrame frame)
163164

164165
return true;
165166
}
167+
168+
private static readonly Regex JscRe = new Regex(@"^\s*(?:([^@]*)(?:\((.*?)\))?@)?(\S.*?):(\d+)(?::(\d+))?\s*$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
169+
170+
internal static bool TryParseJsc(string line, out StackFrame frame)
171+
{
172+
frame = null;
173+
174+
var match = JscRe.Match(line);
175+
if (!match.Success)
176+
return false;
177+
178+
frame = new StackFrame();
179+
frame.File = match.Groups[3].Value;
180+
frame.Method = !string.IsNullOrEmpty(match.Groups[1].Value) ? match.Groups[1].Value : "<unknown>";
181+
frame.Arguments = Array.Empty<string>();
182+
frame.LineNumber = int.Parse(match.Groups[4].Value);
183+
frame.ColumnNumber = !string.IsNullOrEmpty(match.Groups[5].Value) ? int.Parse(match.Groups[5].Value) : (int?)null;
184+
185+
return true;
186+
}
166187
}
167188
}

0 commit comments

Comments
 (0)