Resolve comments
Add sample usage for sourcemap parsing and serialize Use optional parameter for SourceMapGenerator.SerializeMapping and throw exception when input is null Update several typo
This commit is contained in:
Родитель
d83823bfd1
Коммит
ab0b2bab4b
72
README.md
72
README.md
|
@ -4,6 +4,78 @@ This is a C# library for working with JavaScript source maps and deminifying Jav
|
|||
## Source Map Parsing
|
||||
The `SourcemapToolkit.SourcemapParser.dll` provides an API for parsing a souce map into an object that is easy to work with and an API for serializing source map object back to json string.
|
||||
The source map class has a method `GetMappingEntryForGeneratedSourcePosition`, which can be used to find a source map mapping entry that likely corresponds to a piece of generated code.
|
||||
### Example
|
||||
#### Source map string
|
||||
```
|
||||
{
|
||||
"version": 3,
|
||||
"file": "CommonIntl",
|
||||
"mappings": "AACAA,aAAA,CAAc",
|
||||
"sources": ["CommonIntl.js"],
|
||||
"names": ["CommonStrings", "afrikaans"]
|
||||
}
|
||||
```
|
||||
#### Sample source map object
|
||||
|Name|Value|Type
|
||||
|--- | --- | ---
|
||||
|map |{SourcemapToolkit.SourcemapParser.SourceMap}|SourcemapToolkit.SourcemapParser.SourceMap
|
||||
| \|--File|"CommonIntl"|string
|
||||
| \|--Mappings|"AACAA,aAAA,CAAc"|string
|
||||
| \|--Names|Count=2|System.Collections.Generic.List<string>
|
||||
| \|--[0]|"CommonStrings"|string
|
||||
| \|--[1]|"afrikaans"|string
|
||||
| \|--ParsedMappings|Count=3|System.Collections.Generic.List<SourcemapToolkit.SourcemapParser.MappingEntry>
|
||||
| \|--[0]|{SourcemapToolkit.SourcemapParser.MappingEntry}|SourcemapToolkit.SourcemapParser.MappingEntry
|
||||
| \|--GeneratedSourcePosition|{SourcemapToolkit.SourcemapParser.SourcePosition}|SourcemapToolkit.SourcemapParser.SourcePosition
|
||||
| \|--ZeroBasedColumnNumber|0|int
|
||||
| \|--ZeroBasedLineNumber|0|int
|
||||
| \|--OriginalFileName|"CommonIntl.js"|string
|
||||
| \|--OriginalName|"CommonStrings"|string
|
||||
| \|--OriginalSourcePosition|{SourcemapToolkit.SourcemapParser.SourcePosition}|SourcemapToolkit.SourcemapParser.SourcePosition
|
||||
| \|--ZeroBasedColumnNumber|0|int
|
||||
| \|--ZeroBasedLineNumber|1|int
|
||||
| \|--[1]|{SourcemapToolkit.SourcemapParser.MappingEntry}|SourcemapToolkit.SourcemapParser.MappingEntry
|
||||
| \|--GeneratedSourcePosition|{SourcemapToolkit.SourcemapParser.SourcePosition}|SourcemapToolkit.SourcemapParser.SourcePosition
|
||||
| \|--ZeroBasedColumnNumber|13|int
|
||||
| \|--ZeroBasedLineNumber|0|int
|
||||
| \|--OriginalFileName|"CommonIntl.js"|string
|
||||
| \|--OriginalName|null|string
|
||||
| \|--OriginalSourcePosition|{SourcemapToolkit.SourcemapParser.SourcePosition}|SourcemapToolkit.SourcemapParser.SourcePosition
|
||||
| \|--ZeroBasedColumnNumber|0|int
|
||||
| \|--ZeroBasedLineNumber|1|int
|
||||
| \|--[2]|{SourcemapToolkit.SourcemapParser.MappingEntry}|SourcemapToolkit.SourcemapParser.MappingEntry
|
||||
| \|--GeneratedSourcePosition|{SourcemapToolkit.SourcemapParser.SourcePosition}|SourcemapToolkit.SourcemapParser.SourcePosition
|
||||
| \|--ZeroBasedColumnNumber|14|int
|
||||
| \|--ZeroBasedLineNumber|0|int
|
||||
| \|--OriginalFileName|"CommonIntl.js"|string
|
||||
| \|--OriginalName|null|string
|
||||
| \|--OriginalSourcePosition|{SourcemapToolkit.SourcemapParser.SourcePosition}|SourcemapToolkit.SourcemapParser.SourcePosition
|
||||
| \|--ZeroBasedColumnNumber|14|int
|
||||
| \|--ZeroBasedLineNumber|1|int
|
||||
| \|--Sources|Count=1|System.Collections.Generic.List<string>
|
||||
| \|--[0]|"CommonIntl.js"|string
|
||||
### Usage
|
||||
The top level API for source map parsing is the `SourceMapParser.ParseSourceMap` method. The input is a `StreamReader` that can be used to access the contents of the source map.
|
||||
The top level API for source map serializing is the `SourceMapGenerator.SerializeMapping` method. The input is a `SourceMap` that to be serialized and an optional JsonSerializerSettings that can be used to control the json serialization.
|
||||
A sample usage of the library is shown below.
|
||||
|
||||
```csharp
|
||||
// Parse the source map from file
|
||||
SourceMapParser parser = new SourceMapParser();
|
||||
SourceMap sourceMap;
|
||||
using (FileStream stream = new FileStream(@"sample.sourcemap", FileMode.Open))
|
||||
{
|
||||
sourceMap = parser.ParseSourceMap(new StreamReader(stream));
|
||||
}
|
||||
|
||||
// Manipulate the source map
|
||||
...
|
||||
|
||||
// Save to source map to file
|
||||
SourceMapGenerator generator = new SourceMapGenerator();
|
||||
string serializedMap = generator.SerializeMapping(sourceMap);
|
||||
File.WriteAllText(@"updatedSample.sourcemap", serializedMap);
|
||||
```
|
||||
|
||||
## Call Stack Deminification
|
||||
The `SourcemapToolkit.CallstackDeminifier.dll` allows for the deminification of JavaScript call stacks.
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
|
@ -55,22 +56,14 @@ namespace SourcemapToolkit.SourcemapParser
|
|||
|
||||
public class SourceMapGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialize SourceMap object to json string
|
||||
/// </summary>
|
||||
public string SerializeMapping(SourceMap sourceMap)
|
||||
{
|
||||
return SerializeMapping(sourceMap, null /* jsonSerializerSettings */ );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize SourceMap object to json string with given serialize settings
|
||||
/// </summary>
|
||||
public string SerializeMapping(SourceMap sourceMap, JsonSerializerSettings jsonSerializerSettings)
|
||||
public string SerializeMapping(SourceMap sourceMap, JsonSerializerSettings jsonSerializerSettings = null)
|
||||
{
|
||||
if (sourceMap == null)
|
||||
{
|
||||
return null;
|
||||
throw new ArgumentNullException(nameof(sourceMap));
|
||||
}
|
||||
|
||||
SourceMap mapToSerialize = new SourceMap()
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SourcemapToolkit.SourcemapParser.UnitTests
|
||||
|
@ -54,7 +55,7 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
|
|||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeMappingEntry_WithOriginalFileNameNoOriginalName_FourSegment()
|
||||
public void SerializeMappingEntry_WithOriginalFileNameNoOriginalName_FourSegments()
|
||||
{
|
||||
// Arrange
|
||||
SourceMapGenerator sourceMapGenerator = new SourceMapGenerator();
|
||||
|
@ -78,7 +79,7 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
|
|||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeMappingEntry_WithOriginalFileNameAndOriginalName_FiveSegment()
|
||||
public void SerializeMappingEntry_WithOriginalFileNameAndOriginalName_FiveSegments()
|
||||
{
|
||||
// Arrange
|
||||
SourceMapGenerator sourceMapGenerator = new SourceMapGenerator();
|
||||
|
@ -103,7 +104,8 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
|
|||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SerializeMapping_NullInput_ReturnsNull()
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void SerializeMapping_NullInput_ThrowsException()
|
||||
{
|
||||
// Arrange
|
||||
SourceMapGenerator sourceMapGenerator = new SourceMapGenerator();
|
||||
|
@ -111,9 +113,6 @@ namespace SourcemapToolkit.SourcemapParser.UnitTests
|
|||
|
||||
// Act
|
||||
string output = sourceMapGenerator.SerializeMapping(input);
|
||||
|
||||
// Assert
|
||||
Assert.IsNull(output);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
|
|
@ -41,6 +41,10 @@
|
|||
<AssemblyOriginatorKeyFile>..\..\SourceMapToolkit.snk</AssemblyOriginatorKeyFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
</ItemGroup>
|
||||
<Choose>
|
||||
|
|
Загрузка…
Ссылка в новой задаче