Improved performance. Added SourcesContent support (#62)

* Improved performance. Added SourcesContent support

* review
This commit is contained in:
Viktor Borisov 2018-02-22 02:04:51 +07:00 коммит произвёл Christian Gonzalez
Родитель 284e424ba0
Коммит b31aa3d50f
7 изменённых файлов: 54 добавлений и 30 удалений

Просмотреть файл

@ -16,7 +16,7 @@
* limitations under the License.
*/
using System.Collections.Generic;
using System.Text;
namespace SourcemapToolkit.SourcemapParser
{
@ -25,7 +25,7 @@ namespace SourcemapToolkit.SourcemapParser
/// </summary>
internal static class Base64VlqEncoder
{
public static void Encode(ICollection<char> output, int value)
public static void Encode(StringBuilder output, int value)
{
int vlq = ToVlqSigned(value);
@ -37,7 +37,7 @@ namespace SourcemapToolkit.SourcemapParser
{
maskResult |= Base64VlqConstants.VlqContinuationBit;
}
output.Add(Base64Converter.ToBase64(maskResult));
output.Append(Base64Converter.ToBase64(maskResult));
} while (vlq > 0);
}

Просмотреть файл

@ -36,6 +36,11 @@ namespace SourcemapToolkit.SourcemapParser
/// </summary>
public List<MappingEntry> ParsedMappings;
/// <summary>
/// A list of content source files
/// </summary>
public List<string> SourcesContent;
public SourceMap Clone()
{
return new SourceMap
@ -45,6 +50,7 @@ namespace SourcemapToolkit.SourcemapParser
Mappings = this.Mappings,
Sources = new List<string>(this.Sources),
Names = new List<string>(this.Names),
SourcesContent = new List<string>(this.SourcesContent),
ParsedMappings = new List<MappingEntry>(this.ParsedMappings.Select(m => m.Clone()))
};
}
@ -81,6 +87,7 @@ namespace SourcemapToolkit.SourcemapParser
Version = this.Version,
Sources = new List<string>(),
Names = new List<string>(),
SourcesContent = new List<string>(),
ParsedMappings = new List<MappingEntry>()
};

Просмотреть файл

@ -3,9 +3,11 @@ using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
namespace SourcemapToolkit.SourcemapParser
{
/// <summary>
/// Class to track the internal state during source map serialize
/// </summary>
@ -85,21 +87,22 @@ namespace SourcemapToolkit.SourcemapParser
Names = sourceMap.Names,
Sources = sourceMap.Sources,
Version = sourceMap.Version,
SourcesContent = sourceMap.SourcesContent
};
if (sourceMap.ParsedMappings != null && sourceMap.ParsedMappings.Count > 0)
{
MappingGenerateState state = new MappingGenerateState(sourceMap.Names, sourceMap.Sources);
List<char> output = new List<char>();
StringBuilder output = new StringBuilder();
foreach (MappingEntry entry in sourceMap.ParsedMappings)
{
SerializeMappingEntry(entry, state, output);
}
output.Add(';');
output.Append(';');
mapToSerialize.Mappings = new string(output.ToArray());
mapToSerialize.Mappings = output.ToString();
}
return JsonConvert.SerializeObject(mapToSerialize,
@ -113,20 +116,25 @@ namespace SourcemapToolkit.SourcemapParser
/// <summary>
/// Convert each mapping entry to VLQ encoded segments
/// </summary>
internal void SerializeMappingEntry(MappingEntry entry, MappingGenerateState state, ICollection<char> output)
internal void SerializeMappingEntry(MappingEntry entry, MappingGenerateState state, StringBuilder output)
{
if (state.LastGeneratedPosition.ZeroBasedLineNumber > entry.GeneratedSourcePosition.ZeroBasedLineNumber)
{
throw new InvalidOperationException($"Invalid sourmap detected. Please check the line {entry.GeneratedSourcePosition.ZeroBasedLineNumber}");
}
// Each line of generated code is separated using semicolons
while (entry.GeneratedSourcePosition.ZeroBasedLineNumber != state.LastGeneratedPosition.ZeroBasedLineNumber)
{
state.LastGeneratedPosition.ZeroBasedColumnNumber = 0;
state.LastGeneratedPosition.ZeroBasedLineNumber++;
state.IsFirstSegment = true;
output.Add(';');
output.Append(';');
}
// The V3 source map format calls for all Base64 VLQ segments to be seperated by commas.
if (!state.IsFirstSegment)
output.Add(',');
output.Append(',');
state.IsFirstSegment = false;

Просмотреть файл

@ -18,6 +18,7 @@ namespace SourcemapToolkit.SourcemapParser
Version = sourceMap.Version,
Mappings = sourceMap.Mappings,
Sources = sourceMap.Sources == null ? null : new List<string>(sourceMap.Sources),
SourcesContent = sourceMap.SourcesContent == null ? null : new List<string>(sourceMap.SourcesContent),
Names = sourceMap.Names == null ? null : new List<string>(sourceMap.Names),
ParsedMappings = new List<MappingEntry>()
};

Просмотреть файл

@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Text;
namespace SourcemapToolkit.SourcemapParser.UnitTests
{
@ -10,33 +11,33 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
public void Base64VlqEncoder_SmallValue_ListWithOnlyOneValue()
{
// Act
List<char> result = new List<char>();
var result = new StringBuilder();
Base64VlqEncoder.Encode(result, 15);
// Assert
Assert.AreEqual("e", new string(result.ToArray()));
Assert.AreEqual("e", result.ToString());
}
[TestMethod]
public void Base64VlqEncoder_LargeValue_ListWithOnlyMultipleValues()
{
// Act
List<char> result = new List<char>();
var result = new StringBuilder();
Base64VlqEncoder.Encode(result, 701);
// Assert
Assert.AreEqual("6rB", new string(result.ToArray()));
Assert.AreEqual("6rB", result.ToString());
}
[TestMethod]
public void Base64VlqEncoder_NegativeValue_ListWithCorrectValue()
{
// Act
List<char> result = new List<char>();
var result = new StringBuilder();
Base64VlqEncoder.Encode(result, -15);
// Assert
Assert.AreEqual("f", new string(result.ToArray()));
Assert.AreEqual("f", result.ToString());
}
}

Просмотреть файл

@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;
namespace SourcemapToolkit.SourcemapParser.UnitTests
{
@ -25,11 +26,11 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
};
// Act
List<char> output = new List<char>();
sourceMapGenerator.SerializeMappingEntry(entry, state, output);
var result = new StringBuilder();
sourceMapGenerator.SerializeMappingEntry(entry, state, result);
// Assert
Assert.IsTrue(output.IndexOf(';') >= 0);
Assert.IsTrue(result.ToString().IndexOf(';') >= 0);
}
[TestMethod]
@ -47,11 +48,11 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
};
// Act
List<char> output = new List<char>();
sourceMapGenerator.SerializeMappingEntry(entry, state, output);
var result = new StringBuilder();
sourceMapGenerator.SerializeMappingEntry(entry, state, result);
// Assert
Assert.AreEqual("U", new string(output.ToArray()));
Assert.AreEqual("U", result.ToString());
}
[TestMethod]
@ -71,11 +72,11 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
};
// Act
List<char> output = new List<char>();
sourceMapGenerator.SerializeMappingEntry(entry, state, output);
var result = new StringBuilder();
sourceMapGenerator.SerializeMappingEntry(entry, state, result);
// Assert
Assert.AreEqual(",UAAK", new string(output.ToArray()));
Assert.AreEqual(",UAAK", result.ToString());
}
[TestMethod]
@ -96,11 +97,11 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
};
// Act
List<char> output = new List<char>();
sourceMapGenerator.SerializeMappingEntry(entry, state, output);
var result = new StringBuilder();
sourceMapGenerator.SerializeMappingEntry(entry, state, result);
// Assert
Assert.AreEqual("KACMA", new string(output.ToArray()));
Assert.AreEqual("KACMA", result.ToString());
}
[TestMethod]

Просмотреть файл

@ -23,7 +23,8 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
{
File = "generated.js",
Sources = new List<string> { "sourceOne.js" },
ParsedMappings = new List<MappingEntry> { mappingEntry }
ParsedMappings = new List<MappingEntry> { mappingEntry },
SourcesContent = new List<string>{"var a = b"}
};
// Act
@ -32,6 +33,7 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
// Assert
Assert.IsNotNull(linesOnlyMap);
Assert.AreEqual(1, linesOnlyMap.Sources.Count);
Assert.AreEqual(1, linesOnlyMap.SourcesContent.Count);
Assert.AreEqual(1, linesOnlyMap.ParsedMappings.Count);
Assert.AreEqual(1, linesOnlyMap.ParsedMappings[0].GeneratedSourcePosition.ZeroBasedLineNumber);
Assert.AreEqual(0, linesOnlyMap.ParsedMappings[0].GeneratedSourcePosition.ZeroBasedColumnNumber);
@ -55,7 +57,8 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
{
File = "generated.js",
Sources = new List<string> { "sourceOne.js" },
ParsedMappings = new List<MappingEntry> { mappingEntry, mappingEntry2 }
ParsedMappings = new List<MappingEntry> { mappingEntry, mappingEntry2 },
SourcesContent = new List<string>{"var a = b"}
};
// Act
@ -64,6 +67,7 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
// Assert
Assert.IsNotNull(linesOnlyMap);
Assert.AreEqual(1, linesOnlyMap.Sources.Count);
Assert.AreEqual(1, linesOnlyMap.SourcesContent.Count);
Assert.AreEqual(1, linesOnlyMap.ParsedMappings.Count);
Assert.AreEqual(1, linesOnlyMap.ParsedMappings[0].GeneratedSourcePosition.ZeroBasedLineNumber);
Assert.AreEqual(0, linesOnlyMap.ParsedMappings[0].GeneratedSourcePosition.ZeroBasedColumnNumber);
@ -87,7 +91,8 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
{
File = "generated.js",
Sources = new List<string> { "sourceOne.js" },
ParsedMappings = new List<MappingEntry> { mappingEntry, mappingEntry2 }
ParsedMappings = new List<MappingEntry> { mappingEntry, mappingEntry2 },
SourcesContent = new List<string>{"var a = b"}
};
// Act
@ -96,6 +101,7 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
// Assert
Assert.IsNotNull(linesOnlyMap);
Assert.AreEqual(1, linesOnlyMap.Sources.Count);
Assert.AreEqual(1, linesOnlyMap.SourcesContent.Count);
Assert.AreEqual(1, linesOnlyMap.ParsedMappings.Count);
Assert.AreEqual(1, linesOnlyMap.ParsedMappings[0].GeneratedSourcePosition.ZeroBasedLineNumber);
Assert.AreEqual(0, linesOnlyMap.ParsedMappings[0].GeneratedSourcePosition.ZeroBasedColumnNumber);