Skip to content

Commit c2ffb75

Browse files
committed
Remove SourcePosition class and fix more tests
1 parent e95322a commit c2ffb75

File tree

9 files changed

+91
-85
lines changed

9 files changed

+91
-85
lines changed

SourceMaps.StackTraces.Tests/Helper.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ at Function.<anonymous> (C:\\project files\\spect\\node_modules\\esm\\esm.js:1:2
115115
"bar(1)@http://127.0.0.1:8000/js/file.js:13\n" +
116116
"bar(2)@http://127.0.0.1:8000/js/file.js:16\n" +
117117
"foo()@http://127.0.0.1:8000/js/file.js:20\n" +
118-
"@http://127.0.0.1:8000/js/file.js:24\n",
118+
"@http://127.0.0.1:8000/js/file.js:24",
119119
};
120120

121121
public static readonly JavaScriptError Firefox7 = new JavaScriptError
@@ -129,7 +129,7 @@ at Function.<anonymous> (C:\\project files\\spect\\node_modules\\esm\\esm.js:1:2
129129
"bar(1)@file:///G:/js/file.js:13\n" +
130130
"bar(2)@file:///G:/js/file.js:16\n" +
131131
"foo()@file:///G:/js/file.js:20\n" +
132-
"@file:///G:/js/file.js:24\n",
132+
"@file:///G:/js/file.js:24",
133133
};
134134

135135
public static readonly JavaScriptError Firefox14 = new JavaScriptError
@@ -138,7 +138,7 @@ at Function.<anonymous> (C:\\project files\\spect\\node_modules\\esm\\esm.js:1:2
138138
Stack =
139139
"@http://path/to/file.js:48\n" +
140140
"dumpException3@http://path/to/file.js:52\n" +
141-
"onclick@http://path/to/file.js:1\n",
141+
"onclick@http://path/to/file.js:1",
142142
FileName = "http://path/to/file.js",
143143
LineNumber = 48,
144144
};
@@ -150,7 +150,7 @@ at Function.<anonymous> (C:\\project files\\spect\\node_modules\\esm\\esm.js:1:2
150150
Stack =
151151
"foo@http://path/to/file.js:41:13\n" +
152152
"bar@http://path/to/file.js:1:1\n" +
153-
".plugin/e.fn[c]/<@http://path/to/file.js:1:1\n",
153+
".plugin/e.fn[c]/<@http://path/to/file.js:1:1",
154154
FileName = "http://path/to/file.js",
155155
LineNumber = 41,
156156
ColumnNumber = 12,
@@ -182,7 +182,7 @@ at Function.<anonymous> (C:\\project files\\spect\\node_modules\\esm\\esm.js:1:2
182182
"[2]</Bar.prototype._baz/</<@http://path/to/file.js:703:28\n" +
183183
"App.prototype.foo@file:///path/to/file.js:15:2\n" +
184184
"bar@file:///path/to/file.js:20:3\n" +
185-
"@file:///path/to/index.html:23:1\n", // inside <script> tag
185+
"@file:///path/to/index.html:23:1", // inside <script> tag
186186
FileName = "http://path/to/file.js",
187187
ColumnNumber = 0,
188188
LineNumber = 703,
@@ -317,7 +317,7 @@ at Function.<anonymous> (C:\\project files\\spect\\node_modules\\esm\\esm.js:1:2
317317
"at foo (eval at speak (http://localhost:8080/file.js:21:17), <anonymous>:2:96)\n" +
318318
"at eval (eval at speak (http://localhost:8080/file.js:21:17), <anonymous>:4:18)\n" +
319319
"at Object.speak (http://localhost:8080/file.js:21:17)\n" +
320-
"at http://localhost:8080/file.js:31:13\n",
320+
"at http://localhost:8080/file.js:31:13",
321321
};
322322

323323
public static readonly JavaScriptError Phantomjs119 = new JavaScriptError
@@ -342,7 +342,7 @@ at Function.<anonymous> (C:\\project files\\spect\\node_modules\\esm\\esm.js:1:2
342342
"at _currentElement(/home/username/sample-workspace/sampleapp.collect.react/node_modules/react-native/Libraries/Renderer/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js:346:40)\n" +
343343
"at child(/home/username/sample-workspace/sampleapp.collect.react/node_modules/react-native/Libraries/Renderer/src/renderers/shared/stack/reconciler/ReactReconciler.js:68:25)\n" +
344344
"at children(/home/username/sample-workspace/sampleapp.collect.react/node_modules/react-native/Libraries/Renderer/src/renderers/shared/stack/reconciler/ReactMultiChild.js:264:10)\n" +
345-
"at this(/home/username/sample-workspace/sampleapp.collect.react/node_modules/react-native/Libraries/Renderer/src/renderers/native/ReactNativeBaseComponent.js:74:41)\n",
345+
"at this(/home/username/sample-workspace/sampleapp.collect.react/node_modules/react-native/Libraries/Renderer/src/renderers/native/ReactNativeBaseComponent.js:74:41)",
346346
};
347347

348348
public static readonly JavaScriptError AndroidReactNativeProd = new JavaScriptError
@@ -396,8 +396,7 @@ at Function.<anonymous> (C:\\project files\\spect\\node_modules\\esm\\esm.js:1:2
396396
_exampleFunction@/home/test/project/App.js:125:13
397397
_depRunCallbacks@/home/test/project/node_modules/dep/index.js:77:45
398398
tryCallTwo@/home/test/project/node_modules/react-native/node_modules/promise/lib/core.js:45:5
399-
doResolve@/home/test/project/node_modules/react-native/node_modules/promise/lib/core.js:200:13
400-
",
399+
doResolve@/home/test/project/node_modules/react-native/node_modules/promise/lib/core.js:200:13",
401400
};
402401

403402
public static readonly JavaScriptError IosReactNative2 = new JavaScriptError

SourceMaps.StackTraces.Tests/StackTraceParserTests.cs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using FluentAssertions;
45
using Xunit;
56

@@ -8,7 +9,7 @@ namespace SourceMaps.StackTraces.Tests
89
public class StackTraceParserTests
910
{
1011
[Theory]
11-
[MemberData(nameof(StackTraceData))]
12+
[MemberData(nameof(SingleLineStackTraceData))]
1213
public void ParseTests(string stacktrace, List<StackFrame> expected)
1314
{
1415
var actual = StackTraceParser.Parse(stacktrace).Frames;
@@ -31,6 +32,34 @@ public void ParseTests(string stacktrace, List<StackFrame> expected)
3132
}
3233
}
3334

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+
3463
public static IEnumerable<object[]> StackTraceData()
3564
{
3665
// Firefox
@@ -110,6 +139,7 @@ at throwErr (https://localhost:5001/dist/site.js:1:45)
110139
LineNumber = 43,
111140
ColumnNumber = 36,
112141
},
142+
null,
113143
new StackFrame
114144
{
115145
File = "C:\\\\project files\\\\spect\\\\src\\\\index.js",
@@ -1347,7 +1377,7 @@ at throwErr (https://localhost:5001/dist/site.js:1:45)
13471377
},
13481378
new StackFrame
13491379
{
1350-
File = "C:\\projects\\spect\\src\\index.js", Method = "Object.get",
1380+
File = "C:\\\\projects\\\\spect\\\\src\\\\index.js", Method = "Object.get",
13511381
Arguments = Array.Empty<string>(), LineNumber = 43, ColumnNumber = 36
13521382
},
13531383
new StackFrame
@@ -1378,7 +1408,7 @@ at throwErr (https://localhost:5001/dist/site.js:1:45)
13781408
},
13791409
new StackFrame
13801410
{
1381-
File = "C:\\projects\\spect\\node_modules\\esm\\esm.js", Method = "<unknown>",
1411+
File = "C:\\\\projects\\\\spect\\\\node_modules\\\\esm\\\\esm.js", Method = "<unknown>",
13821412
Arguments = Array.Empty<string>(), LineNumber = 1, ColumnNumber = 34176
13831413
},
13841414
new StackFrame
@@ -1391,12 +1421,12 @@ at throwErr (https://localhost:5001/dist/site.js:1:45)
13911421
},
13921422
new StackFrame
13931423
{
1394-
File = "C:\\projects\\spect\\node_modules\\esm\\esm.js", Method = "Function.<anonymous>",
1424+
File = "C:\\\\projects\\\\spect\\\\node_modules\\\\esm\\\\esm.js", Method = "Function.<anonymous>",
13951425
Arguments = Array.Empty<string>(), LineNumber = 1, ColumnNumber = 296856
13961426
},
13971427
new StackFrame
13981428
{
1399-
File = "C:\\projects\\spect\\node_modules\\esm\\esm.js", Method = "Function.<anonymous>",
1429+
File = "C:\\\\projects\\\\spect\\\\node_modules\\\\esm\\\\esm.js", Method = "Function.<anonymous>",
14001430
Arguments = Array.Empty<string>(), LineNumber = 1, ColumnNumber = 296555
14011431
},
14021432
},

SourceMaps.StackTraces/StackTraceParser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static string ReTrace(SourceMapCollection sourceMaps, string stacktrace,
3333

3434
frame.File = originalPosition?.OriginalFileName ?? frame.File;
3535
frame.Method = originalPosition?.OriginalName ?? frame.Method;
36-
frame.LineNumber = originalPosition?.OriginalSourcePosition.LineNumber ?? frame.LineNumber;
37-
frame.ColumnNumber = originalPosition?.OriginalSourcePosition.ColumnNumber ?? frame.ColumnNumber;
36+
frame.LineNumber = originalPosition?.OriginalLineNumber ?? frame.LineNumber;
37+
frame.ColumnNumber = originalPosition?.OriginalColumnNumber ?? frame.ColumnNumber;
3838
}
3939

4040
return trace.ToString();
@@ -113,8 +113,8 @@ internal static bool TryParseWinJs(string line, out StackFrame frame)
113113
return true;
114114
}
115115

116-
private static readonly Regex GeckoRe = new Regex(@"^\s*(.*?)(?:\((.*?)\))?(?:^|@)((?:file|https?|blob|chrome|webpack|resource|\[native).*?|[^@]*bundle)(?::(\d+))?(?::(\d+))?\s*$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
117-
private static readonly Regex GeckoEvalRe = new Regex(@"(\S+) line (\d+)(?: > eval line \d+)* > eval", RegexOptions.IgnoreCase | RegexOptions.Compiled);
116+
private static readonly Regex GeckoRe = new Regex(@"^\s*(.*?)(?:\((.*?)\))?(?:^|@)((?:file|https?|blob|chrome|webpack|resource|\[native|/).*?|[^@]*bundle)(?::(\d+))?(?::(\d+))?\s*$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
117+
private static readonly Regex GeckoEvalRe = new Regex(@"(?:^\s*(?:.*?)(?:\((?:.*?)\))?@|^)(\S+) line (\d+)(?: > eval line \d+)* > eval", RegexOptions.IgnoreCase | RegexOptions.Compiled);
118118
internal static bool TryParseGecko(string line, out StackFrame frame)
119119
{
120120
frame = null;

SourceMaps.Tests/Helper.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,11 @@ public static void AssertMapping(
153153
string name,
154154
SourceMap map)
155155
{
156-
var mapping = map.OriginalPositionFor(generatedLine, generatedColumn).Value;
157-
Assert.Equal(name, mapping.OriginalName);
158-
Assert.Equal(originalLine ?? 0, mapping.OriginalSourcePosition.LineNumber);
159-
Assert.Equal(originalColumn ?? 0, mapping.OriginalSourcePosition.ColumnNumber);
156+
var mapping = map.OriginalPositionFor(generatedLine, generatedColumn);
157+
Assert.NotNull(mapping);
158+
Assert.Equal(name, mapping?.OriginalName);
159+
Assert.Equal(originalLine ?? 0, mapping?.OriginalLineNumber);
160+
Assert.Equal(originalColumn ?? 0, mapping?.OriginalColumnNumber);
160161

161162
var expectedSource = (originalSource, map.SourceRoot) switch
162163
{
@@ -166,7 +167,7 @@ public static void AssertMapping(
166167
_ => null,
167168
};
168169

169-
Assert.Equal(expectedSource, mapping.OriginalFileName);
170+
Assert.Equal(expectedSource, mapping?.OriginalFileName);
170171
}
171172

172173
public static void AssertEqualMaps(SourceMap expected, SourceMap actual)

SourceMaps.Tests/SourceMapParserTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ public void Parse_MappingsAndEndOfLines()
107107
{
108108
ParsedMappings = new List<SourceMapMappingEntry>
109109
{
110-
new SourceMapMappingEntry(new SourcePosition(1, 1), new SourcePosition(1, 1), null, "bar.js"),
111-
new SourceMapMappingEntry(new SourcePosition(2, 2), new SourcePosition(2, 2), null, "bar.js"),
112-
new SourceMapMappingEntry(new SourcePosition(1, 1), new SourcePosition(1, 1), null, "baz.js"),
110+
new SourceMapMappingEntry(1, 1, 1, 1, null, "bar.js"),
111+
new SourceMapMappingEntry(2, 2, 2, 2, null, "bar.js"),
112+
new SourceMapMappingEntry(1, 1, 1, 1, null, "baz.js"),
113113
}
114114
};
115115

@@ -133,15 +133,15 @@ public void Parse_OriginalPositionFor()
133133
{
134134
ParsedMappings = new List<SourceMapMappingEntry>
135135
{
136-
new SourceMapMappingEntry(new SourcePosition(2, 2), new SourcePosition(1, 1), null,
136+
new SourceMapMappingEntry(2, 2, 1, 1, null,
137137
"foo/bar/bang.coffee"),
138138
}
139139
};
140140

141141
var pos = map.OriginalPositionFor(2, 2).Value;
142142
Assert.Equal("foo/bar/bang.coffee", pos.OriginalFileName);
143-
Assert.Equal(1, pos.OriginalSourcePosition.LineNumber);
144-
Assert.Equal(1, pos.OriginalSourcePosition.ColumnNumber);
143+
Assert.Equal(1, pos.OriginalLineNumber);
144+
Assert.Equal(1, pos.OriginalColumnNumber);
145145
}
146146

147147
[Theory]

SourceMaps/SourceMap.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ public class SourceMap
3434
if (ParsedMappings == null)
3535
return null;
3636

37-
var generatedSourcePosition = new SourcePosition(generatedLineNumber, generatedColumnNumber);
38-
3937
var index = ParsedMappings.BinarySearch(
40-
new SourceMapMappingEntry(generatedSourcePosition, default, null, null),
41-
Comparer<SourceMapMappingEntry>.Create((a, b) => a.GeneratedSourcePosition.CompareTo(b.GeneratedSourcePosition)));
38+
new SourceMapMappingEntry(generatedLineNumber, generatedColumnNumber, null, null, null, null),
39+
Comparer<SourceMapMappingEntry>.Create((a, b) =>
40+
{
41+
var lineNumberComparison = a.GeneratedLineNumber.CompareTo(b.GeneratedLineNumber);
42+
if (lineNumberComparison != 0) return lineNumberComparison;
43+
return a.GeneratedColumnNumber.CompareTo(b.GeneratedColumnNumber);
44+
}));
4245

4346
if (index < 0)
4447
{
4548
if (~index - 1 >= 0 &&
46-
ParsedMappings[~index - 1].GeneratedSourcePosition.Equals(generatedSourcePosition))
49+
ParsedMappings[~index - 1].GeneratedLineNumber == generatedLineNumber &&
50+
ParsedMappings[~index - 1].GeneratedColumnNumber == generatedColumnNumber)
4751
{
4852
index = ~index - 1;
4953
}

SourceMaps/SourceMapMappingEntry.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,31 @@ namespace SourceMaps
44
{
55
public struct SourceMapMappingEntry : IEquatable<SourceMapMappingEntry>
66
{
7-
public readonly SourcePosition GeneratedSourcePosition;
8-
public readonly SourcePosition OriginalSourcePosition;
7+
public readonly int GeneratedLineNumber;
8+
public readonly int GeneratedColumnNumber;
9+
public readonly int? OriginalLineNumber;
10+
public readonly int? OriginalColumnNumber;
911
public readonly string OriginalName;
1012
public readonly string OriginalFileName;
1113

12-
public SourceMapMappingEntry(SourcePosition generatedSourcePosition, SourcePosition originalSourcePosition, string originalName, string originalFileName)
14+
public SourceMapMappingEntry(int generatedLineNumber, int generatedColumnNumber, int? originalLineNumber, int? originalColumnNumber, string originalName, string originalFileName)
1315
{
14-
this.GeneratedSourcePosition = generatedSourcePosition;
15-
this.OriginalSourcePosition = originalSourcePosition;
16+
this.GeneratedLineNumber = generatedLineNumber;
17+
this.GeneratedColumnNumber = generatedColumnNumber;
18+
this.OriginalLineNumber = originalLineNumber;
19+
this.OriginalColumnNumber = originalColumnNumber;
1620
this.OriginalName = originalName;
1721
this.OriginalFileName = originalFileName;
1822
}
1923

2024
public bool Equals(SourceMapMappingEntry other)
2125
{
22-
return GeneratedSourcePosition.Equals(other.GeneratedSourcePosition) && OriginalSourcePosition.Equals(other.OriginalSourcePosition) && OriginalName == other.OriginalName && OriginalFileName == other.OriginalFileName;
26+
return GeneratedLineNumber == other.GeneratedLineNumber &&
27+
GeneratedColumnNumber == other.GeneratedColumnNumber &&
28+
OriginalLineNumber == other.OriginalLineNumber &&
29+
OriginalColumnNumber == other.OriginalColumnNumber &&
30+
OriginalName == other.OriginalName &&
31+
OriginalFileName == other.OriginalFileName;
2332
}
2433

2534
public override bool Equals(object obj)
@@ -31,8 +40,10 @@ public override int GetHashCode()
3140
{
3241
unchecked
3342
{
34-
var hashCode = GeneratedSourcePosition.GetHashCode();
35-
hashCode = (hashCode * 397) ^ OriginalSourcePosition.GetHashCode();
43+
var hashCode = GeneratedLineNumber;
44+
hashCode = (hashCode * 397) ^ GeneratedColumnNumber;
45+
hashCode = (hashCode * 397) ^ OriginalLineNumber.GetHashCode();
46+
hashCode = (hashCode * 397) ^ OriginalColumnNumber.GetHashCode();
3647
hashCode = (hashCode * 397) ^ (OriginalName != null ? OriginalName.GetHashCode() : 0);
3748
hashCode = (hashCode * 397) ^ (OriginalFileName != null ? OriginalFileName.GetHashCode() : 0);
3849
return hashCode;

SourceMaps/SourceMapParser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ public MappingParserState(
9494

9595
public SourceMapMappingEntry GetCurrentSourceMapMappingEntry(List<string> names, List<string> sources)
9696
=> new SourceMapMappingEntry(
97-
new SourcePosition(GeneratedLineNumber + 1, GeneratedColumnNumber + 1), // Add 1 to line/column numbers as they are zero-based in source maps
98-
new SourcePosition((OriginalSourceStartingLineNumber ?? -1) + 1, (OriginalSourceStartingColumnNumber ?? -1) + 1),
97+
GeneratedLineNumber + 1,
98+
GeneratedColumnNumber + 1,
99+
OriginalSourceStartingLineNumber.HasValue ? OriginalSourceStartingLineNumber.Value + 1 : (int?)null,
100+
OriginalSourceStartingColumnNumber.HasValue ? OriginalSourceStartingColumnNumber.Value + 1 : (int?)null,
99101
NamesListIndex.HasValue ? names[NamesListIndex.Value] : null,
100102
SourcesListIndex.HasValue ? sources[SourcesListIndex.Value] : null);
101103
}

SourceMaps/SourcePosition.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)