A pure MoonBit ECMA-426 Source Map library and command-line toolkit.
Dependencies
moon update
moon test --target all --deny-warn
moon run --target native cmd/main -- validate examples/demo.min.js.mapmoon run --target native cmd/main -- \
lookup examples/demo.min.js.map --line 0 --column 4 --format jsonmoon run --target native cmd/main -- \
symbolize examples/demo.min.js.map examples/frames.txt///|
test "parse, validate and query" {
let json =
#|{"version":3,"sources":["src/main.ts"],"names":["main"],"mappings":"AAAAA"}
let document = @moon_sourcemap.parse_document(json)
assert_false(
@moon_sourcemap.diagnostics_have_errors(@moon_sourcemap.validate(document)),
)
let decoded = @moon_sourcemap.decode_document(document)
let mapping = @moon_sourcemap.original_position_for(
decoded,
generated=@moon_sourcemap.Position::new(line=0, column=0),
)
assert_true(mapping is Some(_))
}///|
test "build a map" {
let builder = @moon_sourcemap.SourceMapBuilder::new(file="bundle.js")
let source = builder.add_source(
url="src/main.ts",
content="export const answer = 42",
)
builder.add_mapping(
generated=@moon_sourcemap.Position::new(line=0, column=0),
source_index=source,
original_line=0,
original_column=0,
name="answer",
)
let document = builder.build()
assert_eq(document.version, 3)
}| 能力 | API |
|---|---|
| JSON 双向转换 | parse_document, encode_document |
| mappings 编解码 | decode_mappings, encode_mappings |
| 严格校验 | validate, validate_json, Diagnostic |
| 位置查询 | original_position_for, get_original_positions, generated_positions_for |
| 批量索引 | SourceMapConsumer |
| Index Map | decode_document, flatten |
| 生成 | SourceMapBuilder, encode_decoded |
| 两级构建 | compose, compose_with_options |
| 栈帧恢复 | parse_stack_frame, symbolize, symbolize_frame |
| 规范化变换 | canonicalize, slice_generated, concatenate |
moon-sourcemap validate <map> [--format text|json]
moon-sourcemap inspect <map> [--format text|json]
moon-sourcemap lookup <map> --line N --column N [--bias glb|lub | --all] [--strict]
moon-sourcemap flatten <map> -o <output>
moon-sourcemap compose <outer> <inner> -o <output>
moon-sourcemap symbolize <map> <frames> [--context N] [--strict] [--format text|json]moon fmt --check
moon check --target all --deny-warn
moon build --target all --deny-warn
moon test --target all --deny-warn
moon info
git diff --exit-code
moon packagepub(all) suberror SourceMapError {
InvalidJson(message~ : String)
InvalidField(path~ : String, message~ : String)
InvalidVlq(offset~ : Int, message~ : String)
InvalidMappings(line~ : Int, segment~ : Int, offset~ : Int, message~ : String)
InvalidDocument(message~ : String)
Io(message~ : String)
} derive(Eq, Debug)pub(all) struct ComposeOptions {
bias : LookupBias
missing : MissingMappingPolicy
preserve_outer_names : Bool
}pub(all) struct ComposeStats {
outer_mappings : Int
composed_mappings : Int
generated_only : Int
dropped : Int
}pub(all) struct ContextualSymbolizationReport {
frames : Array[ContextualSymbolizedFrame]
matched : Int
unmatched : Int
with_context : Int
} derive(Eq, Debug)fn ContextualSymbolizationReport::has_complete_context(self : ContextualSymbolizationReport) -> Boolfn ContextualSymbolizationReport::unmatched_frames(self : ContextualSymbolizationReport) -> Array[StackFrame]pub(all) struct ContextualSymbolizedFrame {
result : SymbolizedFrame
context : SourceContext?
} derive(Eq, Debug)fn DecodedSourceMap::new(file? : String, sources? : Array[SourceEntry], mappings? : Array[Mapping]) -> DecodedSourceMappub(all) struct Diagnostic {
severity : DiagnosticSeverity
code : String
message : String
path : String
mapping_line : Int?
segment : Int?
offset : Int?
} derive(Eq, Debug)fn Diagnostic::error(code~ : String, message~ : String, path? : String, mapping_line? : Int, segment? : Int, offset? : Int) -> Diagnosticfn Diagnostic::warning(code~ : String, message~ : String, path? : String, mapping_line? : Int, segment? : Int, offset? : Int) -> Diagnosticpub(all) struct IndexSourceMap {
version : Int
file : String?
sections : Array[IndexSection]
} derive(Eq, Debug)pub(all) struct Mapping {
generated : Position
original : OriginalPosition?
name : String?
} derive(Eq, Debug)pub(all) struct RegisteredSourceMap {
name : String
generated_file : String
aliases : Array[String]
map : DecodedSourceMap
}pub(all) struct RegistrySymbolizationReport {
frames : Array[RegistrySymbolizedFrame]
matched : Int
map_missing : Int
mapping_missing : Int
generated_only : Int
} derive(Eq, Debug)pub(all) struct RegistrySymbolizedFrame {
frame : StackFrame
map_name : String?
result : SymbolizedFrame?
context : SourceContext?
miss_reason : RegistryMissReason?
} derive(Eq, Debug)fn RegularSourceMap::new(file? : String, source_root? : String, sources? : Array[String?], sources_content? : Array[String?], names? : Array[String], mappings? : String, ignore_list? : Array[Int]) -> RegularSourceMappub(all) struct SourceMapBuilder {
file : String?
sources : Array[SourceEntry]
names : Array[String]
mappings : Array[Mapping]
}fn SourceMapBuilder::add_generated(self : SourceMapBuilder, generated~ : Position) -> Unit raise SourceMapErrorfn SourceMapBuilder::add_mapping(self : SourceMapBuilder, generated~ : Position, source_index~ : Int, original_line~ : Int, original_column~ : Int, name? : String) -> Unit raise SourceMapErrorfn SourceMapBuilder::add_source(self : SourceMapBuilder, url? : String, content? : String, ignored? : Bool) -> Intpub(all) struct SourceMapConsumer {
map : DecodedSourceMap
line_starts : Array[Int]
reverse : Map[Int, Array[Int]]
}fn SourceMapConsumer::from_document(document : SourceMapDocument, map_url? : String) -> SourceMapConsumer raise SourceMapErrorfn SourceMapConsumer::generated_positions_for(self : SourceMapConsumer, source_index~ : Int, line? : Int, column? : Int) -> Array[Mapping]fn SourceMapConsumer::get_original_positions(self : SourceMapConsumer, generated~ : Position) -> Array[OriginalPosition?]fn SourceMapConsumer::original_position_for(self : SourceMapConsumer, generated~ : Position, bias? : LookupBias) -> Mapping?fn SourceMapConsumer::original_positions_for(self : SourceMapConsumer, positions : ArrayView[Position], bias? : LookupBias) -> Array[Mapping?]pub(all) enum SourceMapDocument {
Regular(RegularSourceMap)
Indexed(IndexSourceMap)
} derive(Eq, Debug)fn SourceMapRegistry::find(self : SourceMapRegistry, generated_file : String) -> RegisteredSourceMap?fn SourceMapRegistry::register(self : SourceMapRegistry, name~ : String, generated_file~ : String, map~ : DecodedSourceMap, aliases? : Array[String]) -> Unit raise SourceMapErrorfn SourceMapRegistry::symbolize(self : SourceMapRegistry, frames : String, bias? : LookupBias, context_radius? : Int) -> RegistrySymbolizationReport raise SourceMapErrorfn SourceMapRegistry::symbolize_frame(self : SourceMapRegistry, frame : StackFrame, bias? : LookupBias, context_radius? : Int) -> RegistrySymbolizedFramepub(all) struct SourceMapReport {
file : String?
sources : Int
mappings : Int
mapped_segments : Int
generated_only_segments : Int
named_segments : Int
ignored_sources : Int
embedded_sources : Int
referenced_sources : Int
unreferenced_sources : Int
generated_lines : Int
original_lines : Int
duplicate_positions : Int
open_spans : Int
source_usage : Array[SourceUsage]
generated_line_usage : Array[GeneratedLineUsage]
} derive(Eq, Debug)pub(all) struct SourceMapStats {
sources : Int
mappings : Int
mapped : Int
generated_only : Int
named : Int
ignored_sources : Int
generated_lines : Int
original_lines : Int
}pub(all) struct SourceResolverOptions {
map_url : String?
source_root : String?
normalize_windows : Bool
}fn SourceResolverOptions::new(map_url? : String, source_root? : String, normalize_windows? : Bool) -> SourceResolverOptionsfn SourceTextIndex::context(self : SourceTextIndex, source? : String, line~ : Int, column~ : Int, radius? : Int) -> SourceContext?pub(all) struct SymbolizedFrame {
generated : StackFrame
source : String?
line : Int?
column : Int?
name : String?
ignored : Bool
matched : Bool
} derive(Eq, Debug)pub(all) struct TransformReport {
mappings_before : Int
mappings_after : Int
sources_before : Int
sources_after : Int
duplicates_removed : Int
sources_removed : Int
}fn analyze_source_map_json(input : StringView, map_url? : String) -> SourceMapReport raise SourceMapErrorfn canonicalize(map : DecodedSourceMap, duplicate_policy? : DuplicateMappingPolicy, remove_unused_sources? : Bool) -> TransformResultfn compose(generated_to_intermediate : DecodedSourceMap, intermediate_to_original : DecodedSourceMap) -> DecodedSourceMapfn compose_json(outer_json : StringView, inner_json : StringView, indent? : Int) -> String raise SourceMapErrorfn compose_with_options(outer : DecodedSourceMap, inner : DecodedSourceMap, options? : ComposeOptions) -> ComposeResultfn concatenate(parts : ArrayView[(Position, DecodedSourceMap)], file? : String) -> DecodedSourceMap raise SourceMapErrorfn decode_document(document : SourceMapDocument, map_url? : String) -> DecodedSourceMap raise SourceMapErrorfn decode_mappings(input : String, sources_count~ : Int, names~ : ArrayView[String]) -> Array[Mapping] raise SourceMapErrorfn decode_regular_source_map(map : RegularSourceMap, map_url? : String) -> DecodedSourceMap raise SourceMapErrorfn encode_mappings(mappings : ArrayView[Mapping], names~ : ArrayView[String]) -> String raise SourceMapErrorfn encode_vlq(value : Int) -> Stringfn generated_positions_for(map : DecodedSourceMap, source_index~ : Int, line~ : Int, column? : Int) -> Array[Mapping]fn get_original_positions(map : DecodedSourceMap, generated~ : Position) -> Array[OriginalPosition?]fn mappings_line_count(input : String) -> Intfn normalize_generated_file(value : String) -> Stringfn original_position_for(map : DecodedSourceMap, generated~ : Position, bias? : LookupBias) -> Mapping?fn shift_generated(map : DecodedSourceMap, line_delta? : Int, first_line_column_delta? : Int) -> DecodedSourceMap raise SourceMapErrorfn slice_generated(map : DecodedSourceMap, start~ : Position, end~ : Position) -> DecodedSourceMap raise SourceMapErrorfn source_context_for_frame(map : DecodedSourceMap, frame : SymbolizedFrame, radius? : Int) -> SourceContext?fn source_context_for_mapping(map : DecodedSourceMap, mapping : Mapping, radius? : Int) -> SourceContext?fn symbolize(map : DecodedSourceMap, frames : String, bias? : LookupBias) -> Array[SymbolizedFrame] raise SourceMapErrorfn symbolize_frame(map : DecodedSourceMap, frame : StackFrame, bias? : LookupBias) -> SymbolizedFramefn symbolize_frame_with_context(map : DecodedSourceMap, frame : StackFrame, bias? : LookupBias, context_radius? : Int) -> ContextualSymbolizedFramefn symbolize_with_context(map : DecodedSourceMap, frames : String, bias? : LookupBias, context_radius? : Int) -> ContextualSymbolizationReport raise SourceMapErrorA pure MoonBit ECMA-426 Source Map library and command-line toolkit.
Dependencies