Fix incosistent formatting
This commit is contained in:
Nile Liao 2016-11-17 19:27:35 +08:00
Родитель e5f5862852
Коммит d83823bfd1
4 изменённых файлов: 133 добавлений и 134 удалений

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

@ -26,24 +26,24 @@ namespace SourcemapToolkit.SourcemapParser
{ {
int result; int result;
if (!_base64DecodeMap.TryGetValue(base64Value, out result)) if (!_base64DecodeMap.TryGetValue(base64Value, out result))
{ {
throw new ArgumentOutOfRangeException(nameof(base64Value), "Tried to convert an invalid base64 value"); throw new ArgumentOutOfRangeException(nameof(base64Value), "Tried to convert an invalid base64 value");
} }
return result; return result;
} }
/// <summary> /// <summary>
/// Converts a integer to base64 value /// Converts a integer to base64 value
/// </summary> /// </summary>
internal static char ToBase64(int value) internal static char ToBase64(int value)
{ {
if (value < 0 || value >= Base64Alphabet.Length) if (value < 0 || value >= Base64Alphabet.Length)
{ {
throw new ArgumentOutOfRangeException(nameof(value)); throw new ArgumentOutOfRangeException(nameof(value));
} }
return Base64Alphabet[value]; return Base64Alphabet[value];
} }
} }
} }

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

@ -1,6 +1,5 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.Serialization; using System.Runtime.Serialization;
@ -10,7 +9,7 @@ namespace SourcemapToolkit.SourcemapParser
/// Class to track the internal state during source map serialize /// Class to track the internal state during source map serialize
/// </summary> /// </summary>
internal class MappingGenerateState internal class MappingGenerateState
{ {
/// <summary> /// <summary>
/// Last location of the code in the transformed code /// Last location of the code in the transformed code
/// </summary> /// </summary>
@ -24,106 +23,106 @@ namespace SourcemapToolkit.SourcemapParser
/// <summary> /// <summary>
/// List that contains the symbol names /// List that contains the symbol names
/// </summary> /// </summary>
public readonly IList<string> Names; public readonly IList<string> Names;
/// <summary> /// <summary>
/// List that contains the file sources /// List that contains the file sources
/// </summary> /// </summary>
public readonly IList<string> Sources; public readonly IList<string> Sources;
/// <summary> /// <summary>
/// Index of last file source /// Index of last file source
/// </summary> /// </summary>
public int LastSourceIndex { get; set; } public int LastSourceIndex { get; set; }
/// <summary> /// <summary>
/// Index of last symbol name /// Index of last symbol name
/// </summary> /// </summary>
public int LastNameIndex { get; set; } public int LastNameIndex { get; set; }
/// <summary> /// <summary>
/// Whether this is the first segment in current line /// Whether this is the first segment in current line
/// </summary> /// </summary>
public bool IsFirstSegment { get; set; } public bool IsFirstSegment { get; set; }
public MappingGenerateState(IList<string> names, IList<string> sources) public MappingGenerateState(IList<string> names, IList<string> sources)
{ {
Names = names; Names = names;
Sources = sources; Sources = sources;
IsFirstSegment = true; IsFirstSegment = true;
} }
} }
public class SourceMapGenerator public class SourceMapGenerator
{ {
/// <summary> /// <summary>
/// Serialize SourceMap object to json string /// Serialize SourceMap object to json string
/// </summary> /// </summary>
public string SerializeMapping(SourceMap sourceMap) public string SerializeMapping(SourceMap sourceMap)
{ {
return SerializeMapping( sourceMap, null /* jsonSerializerSettings */ ); return SerializeMapping(sourceMap, null /* jsonSerializerSettings */ );
} }
/// <summary> /// <summary>
/// Serialize SourceMap object to json string with given serialize settings /// Serialize SourceMap object to json string with given serialize settings
/// </summary> /// </summary>
public string SerializeMapping(SourceMap sourceMap, JsonSerializerSettings jsonSerializerSettings) public string SerializeMapping(SourceMap sourceMap, JsonSerializerSettings jsonSerializerSettings)
{ {
if (sourceMap == null) if (sourceMap == null)
{ {
return null; return null;
} }
SourceMap mapToSerialize = new SourceMap() SourceMap mapToSerialize = new SourceMap()
{ {
File = sourceMap.File, File = sourceMap.File,
Names = sourceMap.Names, Names = sourceMap.Names,
Sources = sourceMap.Sources, Sources = sourceMap.Sources,
Version = sourceMap.Version, Version = sourceMap.Version,
}; };
if (sourceMap.ParsedMappings != null && sourceMap.ParsedMappings.Count > 0) if (sourceMap.ParsedMappings != null && sourceMap.ParsedMappings.Count > 0)
{ {
MappingGenerateState state = new MappingGenerateState(sourceMap.Names, sourceMap.Sources); MappingGenerateState state = new MappingGenerateState(sourceMap.Names, sourceMap.Sources);
List<char> output = new List<char>(); List<char> output = new List<char>();
foreach (MappingEntry entry in sourceMap.ParsedMappings) foreach (MappingEntry entry in sourceMap.ParsedMappings)
{ {
SerializeMappingEntry(entry, state, output); SerializeMappingEntry(entry, state, output);
} }
output.Add(';'); output.Add(';');
mapToSerialize.Mappings = new string(output.ToArray()); mapToSerialize.Mappings = new string(output.ToArray());
} }
return JsonConvert.SerializeObject(mapToSerialize, return JsonConvert.SerializeObject(mapToSerialize,
jsonSerializerSettings ?? new JsonSerializerSettings jsonSerializerSettings ?? new JsonSerializerSettings
{ {
ContractResolver = new CamelCasePropertyNamesContractResolver(), ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore,
}); });
} }
/// <summary> /// <summary>
/// Convert each mapping entry to VLQ encoded segments /// Convert each mapping entry to VLQ encoded segments
/// </summary> /// </summary>
internal void SerializeMappingEntry(MappingEntry entry, MappingGenerateState state, ICollection<char> output) internal void SerializeMappingEntry(MappingEntry entry, MappingGenerateState state, ICollection<char> output)
{ {
// Each line of generated code is separated using semicolons // Each line of generated code is separated using semicolons
while (entry.GeneratedSourcePosition.ZeroBasedLineNumber != state.LastGeneratedPosition.ZeroBasedLineNumber) while (entry.GeneratedSourcePosition.ZeroBasedLineNumber != state.LastGeneratedPosition.ZeroBasedLineNumber)
{ {
state.LastGeneratedPosition.ZeroBasedColumnNumber = 0; state.LastGeneratedPosition.ZeroBasedColumnNumber = 0;
state.LastGeneratedPosition.ZeroBasedLineNumber++; state.LastGeneratedPosition.ZeroBasedLineNumber++;
state.IsFirstSegment = true; state.IsFirstSegment = true;
output.Add(';'); output.Add(';');
} }
// The V3 source map format calls for all Base64 VLQ segments to be seperated by commas. // The V3 source map format calls for all Base64 VLQ segments to be seperated by commas.
if (!state.IsFirstSegment) if (!state.IsFirstSegment)
output.Add(','); output.Add(',');
state.IsFirstSegment = false; state.IsFirstSegment = false;
/* /*
* The following description was taken from the Sourcemap V3 spec https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/mobilebasic?pref=2&pli=1 * The following description was taken from the Sourcemap V3 spec https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/mobilebasic?pref=2&pli=1
@ -149,38 +148,38 @@ namespace SourcemapToolkit.SourcemapParser
* is represented. * is represented.
*/ */
Base64VlqEncoder.Encode(output, entry.GeneratedSourcePosition.ZeroBasedColumnNumber - state.LastGeneratedPosition.ZeroBasedColumnNumber); Base64VlqEncoder.Encode(output, entry.GeneratedSourcePosition.ZeroBasedColumnNumber - state.LastGeneratedPosition.ZeroBasedColumnNumber);
state.LastGeneratedPosition.ZeroBasedColumnNumber = entry.GeneratedSourcePosition.ZeroBasedColumnNumber; state.LastGeneratedPosition.ZeroBasedColumnNumber = entry.GeneratedSourcePosition.ZeroBasedColumnNumber;
if (entry.OriginalFileName != null) if (entry.OriginalFileName != null)
{ {
int sourceIndex = state.Sources.IndexOf(entry.OriginalFileName); int sourceIndex = state.Sources.IndexOf(entry.OriginalFileName);
if (sourceIndex < 0) if (sourceIndex < 0)
{ {
throw new SerializationException("Source map contains original source that cannot be found in provided sources array"); throw new SerializationException("Source map contains original source that cannot be found in provided sources array");
} }
Base64VlqEncoder.Encode(output, sourceIndex - state.LastSourceIndex); Base64VlqEncoder.Encode(output, sourceIndex - state.LastSourceIndex);
state.LastSourceIndex = sourceIndex; state.LastSourceIndex = sourceIndex;
Base64VlqEncoder.Encode(output, entry.OriginalSourcePosition.ZeroBasedLineNumber - state.LastOriginalPosition.ZeroBasedLineNumber); Base64VlqEncoder.Encode(output, entry.OriginalSourcePosition.ZeroBasedLineNumber - state.LastOriginalPosition.ZeroBasedLineNumber);
state.LastOriginalPosition.ZeroBasedLineNumber = entry.OriginalSourcePosition.ZeroBasedLineNumber; state.LastOriginalPosition.ZeroBasedLineNumber = entry.OriginalSourcePosition.ZeroBasedLineNumber;
Base64VlqEncoder.Encode(output, entry.OriginalSourcePosition.ZeroBasedColumnNumber - state.LastOriginalPosition.ZeroBasedColumnNumber); Base64VlqEncoder.Encode(output, entry.OriginalSourcePosition.ZeroBasedColumnNumber - state.LastOriginalPosition.ZeroBasedColumnNumber);
state.LastOriginalPosition.ZeroBasedColumnNumber = entry.OriginalSourcePosition.ZeroBasedColumnNumber; state.LastOriginalPosition.ZeroBasedColumnNumber = entry.OriginalSourcePosition.ZeroBasedColumnNumber;
if (entry.OriginalName != null) if (entry.OriginalName != null)
{ {
int nameIndex = state.Names.IndexOf(entry.OriginalName); int nameIndex = state.Names.IndexOf(entry.OriginalName);
if (nameIndex < 0) if (nameIndex < 0)
{ {
throw new SerializationException("Source map contains original name that cannot be found in provided names array"); throw new SerializationException("Source map contains original name that cannot be found in provided names array");
} }
Base64VlqEncoder.Encode(output, nameIndex - state.LastNameIndex); Base64VlqEncoder.Encode(output, nameIndex - state.LastNameIndex);
state.LastNameIndex = nameIndex; state.LastNameIndex = nameIndex;
} }
} }
} }
} }
} }

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

@ -11,10 +11,10 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
{ {
// Act // Act
List<char> result = new List<char>(); List<char> result = new List<char>();
Base64VlqEncoder.Encode( result, 15 ); Base64VlqEncoder.Encode(result, 15);
// Assert // Assert
Assert.AreEqual( "e", new string( result.ToArray() ) ); Assert.AreEqual("e", new string(result.ToArray()));
} }
[TestMethod] [TestMethod]
@ -22,10 +22,10 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
{ {
// Act // Act
List<char> result = new List<char>(); List<char> result = new List<char>();
Base64VlqEncoder.Encode( result, 701 ); Base64VlqEncoder.Encode(result, 701);
// Assert // Assert
Assert.AreEqual( "6rB", new string( result.ToArray() ) ); Assert.AreEqual("6rB", new string(result.ToArray()));
} }
[TestMethod] [TestMethod]
@ -33,10 +33,10 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
{ {
// Act // Act
List<char> result = new List<char>(); List<char> result = new List<char>();
Base64VlqEncoder.Encode( result, -15 ); Base64VlqEncoder.Encode(result, -15);
// Assert // Assert
Assert.AreEqual( "f", new string( result.ToArray() ) ); Assert.AreEqual("f", new string(result.ToArray()));
} }
} }

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

@ -12,23 +12,23 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
// Arrange // Arrange
SourceMapGenerator sourceMapGenerator = new SourceMapGenerator(); SourceMapGenerator sourceMapGenerator = new SourceMapGenerator();
MappingGenerateState state = new MappingGenerateState( new List<string>() { "Name" }, new List<string>() { "Source" } ); MappingGenerateState state = new MappingGenerateState(new List<string>() { "Name" }, new List<string>() { "Source" });
state.LastGeneratedPosition.ZeroBasedColumnNumber = 1; state.LastGeneratedPosition.ZeroBasedColumnNumber = 1;
MappingEntry entry = new MappingEntry() MappingEntry entry = new MappingEntry()
{ {
GeneratedSourcePosition = new SourcePosition() { ZeroBasedLineNumber = 1, ZeroBasedColumnNumber = 0 }, GeneratedSourcePosition = new SourcePosition() { ZeroBasedLineNumber = 1, ZeroBasedColumnNumber = 0 },
OriginalFileName = state.Sources[ 0 ], OriginalFileName = state.Sources[0],
OriginalName = state.Names[ 0 ], OriginalName = state.Names[0],
OriginalSourcePosition = new SourcePosition() { ZeroBasedLineNumber = 1, ZeroBasedColumnNumber = 0 }, OriginalSourcePosition = new SourcePosition() { ZeroBasedLineNumber = 1, ZeroBasedColumnNumber = 0 },
}; };
// Act // Act
List<char> output = new List<char>(); List<char> output = new List<char>();
sourceMapGenerator.SerializeMappingEntry( entry, state, output ); sourceMapGenerator.SerializeMappingEntry(entry, state, output);
// Assert // Assert
Assert.IsTrue( output.IndexOf( ';' ) >= 0 ); Assert.IsTrue(output.IndexOf(';') >= 0);
} }
[TestMethod] [TestMethod]
@ -37,7 +37,7 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
// Arrange // Arrange
SourceMapGenerator sourceMapGenerator = new SourceMapGenerator(); SourceMapGenerator sourceMapGenerator = new SourceMapGenerator();
MappingGenerateState state = new MappingGenerateState( new List<string>() { "Name" }, new List<string>() { "Source" } ); MappingGenerateState state = new MappingGenerateState(new List<string>() { "Name" }, new List<string>() { "Source" });
MappingEntry entry = new MappingEntry() MappingEntry entry = new MappingEntry()
{ {
@ -47,10 +47,10 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
// Act // Act
List<char> output = new List<char>(); List<char> output = new List<char>();
sourceMapGenerator.SerializeMappingEntry( entry, state, output ); sourceMapGenerator.SerializeMappingEntry(entry, state, output);
// Assert // Assert
Assert.AreEqual( "U", new string( output.ToArray() ) ); Assert.AreEqual("U", new string(output.ToArray()));
} }
[TestMethod] [TestMethod]
@ -59,7 +59,7 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
// Arrange // Arrange
SourceMapGenerator sourceMapGenerator = new SourceMapGenerator(); SourceMapGenerator sourceMapGenerator = new SourceMapGenerator();
MappingGenerateState state = new MappingGenerateState( new List<string>() { "Name" }, new List<string>() { "Source" } ); MappingGenerateState state = new MappingGenerateState(new List<string>() { "Name" }, new List<string>() { "Source" });
state.IsFirstSegment = false; state.IsFirstSegment = false;
MappingEntry entry = new MappingEntry() MappingEntry entry = new MappingEntry()
@ -71,10 +71,10 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
// Act // Act
List<char> output = new List<char>(); List<char> output = new List<char>();
sourceMapGenerator.SerializeMappingEntry( entry, state, output ); sourceMapGenerator.SerializeMappingEntry(entry, state, output);
// Assert // Assert
Assert.AreEqual( ",UAAK", new string( output.ToArray() ) ); Assert.AreEqual(",UAAK", new string(output.ToArray()));
} }
[TestMethod] [TestMethod]
@ -83,23 +83,23 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
// Arrange // Arrange
SourceMapGenerator sourceMapGenerator = new SourceMapGenerator(); SourceMapGenerator sourceMapGenerator = new SourceMapGenerator();
MappingGenerateState state = new MappingGenerateState( new List<string>() { "Name" }, new List<string>() { "Source" } ); MappingGenerateState state = new MappingGenerateState(new List<string>() { "Name" }, new List<string>() { "Source" });
state.LastGeneratedPosition.ZeroBasedLineNumber = 1; state.LastGeneratedPosition.ZeroBasedLineNumber = 1;
MappingEntry entry = new MappingEntry() MappingEntry entry = new MappingEntry()
{ {
GeneratedSourcePosition = new SourcePosition() { ZeroBasedLineNumber = 1, ZeroBasedColumnNumber = 5 }, GeneratedSourcePosition = new SourcePosition() { ZeroBasedLineNumber = 1, ZeroBasedColumnNumber = 5 },
OriginalSourcePosition = new SourcePosition() { ZeroBasedLineNumber = 1, ZeroBasedColumnNumber = 6 }, OriginalSourcePosition = new SourcePosition() { ZeroBasedLineNumber = 1, ZeroBasedColumnNumber = 6 },
OriginalFileName = state.Sources[ 0 ], OriginalFileName = state.Sources[0],
OriginalName = state.Names[ 0 ], OriginalName = state.Names[0],
}; };
// Act // Act
List<char> output = new List<char>(); List<char> output = new List<char>();
sourceMapGenerator.SerializeMappingEntry( entry, state, output ); sourceMapGenerator.SerializeMappingEntry(entry, state, output);
// Assert // Assert
Assert.AreEqual( "KACMA", new string( output.ToArray() ) ); Assert.AreEqual("KACMA", new string(output.ToArray()));
} }
[TestMethod] [TestMethod]
@ -110,10 +110,10 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
SourceMap input = null; SourceMap input = null;
// Act // Act
string output = sourceMapGenerator.SerializeMapping( input ); string output = sourceMapGenerator.SerializeMapping(input);
// Assert // Assert
Assert.IsNull( output ); Assert.IsNull(output);
} }
[TestMethod] [TestMethod]
@ -152,10 +152,10 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
}; };
// Act // Act
string output = sourceMapGenerator.SerializeMapping( input ); string output = sourceMapGenerator.SerializeMapping(input);
// Assert // Assert
Assert.AreEqual( "{\"version\":3,\"file\":\"CommonIntl\",\"mappings\":\"AACAA,aAAA,CAAc;\",\"sources\":[\"input/CommonIntl.js\"],\"names\":[\"CommonStrings\",\"afrikaans\"]}", output ); Assert.AreEqual("{\"version\":3,\"file\":\"CommonIntl\",\"mappings\":\"AACAA,aAAA,CAAc;\",\"sources\":[\"input/CommonIntl.js\"],\"names\":[\"CommonStrings\",\"afrikaans\"]}", output);
} }
} }
} }