Rhino can remap stack-trace line numbers, parser error positions, and debugger source handoffs back to the original source when a transpiler or minifier has generated the script being compiled. The feature is opt-in: attach a SourceMapper to a compile spec and Rhino uses it automatically.
SourceMapV3 mapper = SourceMapV3.parse(mapJsonString); Script script = cx.compileScript( ScriptCompileSpec.fromSource(transpiledSource) .sourceName("bundle.js") .sourceMapper(mapper) .build());
From that point on, RhinoException.lineNumber() reports the original source line, parser error messages quote the original source text, and the debugger receives the primary original source content at compilation time.
The classes in org.mozilla.javascript.sourcemap:
| Class | Visibility | Role |
|---|---|---|
SourceMapper | public interface | Contract consumed by Rhino's parser and runtime |
SourceMapV3 | public final class | ECMA-426 plain source map; implements SourceMapper |
SourceMapJsonParser | package-private | Strict JSON parser; no external dependencies |
MappingsDecoder | package-private | Base64-VLQ mappings decoder |
Segment | package-private record | One decoded mappings segment |
SourceMapException | public class | Unchecked parse/decode error |
Position | public record | (sourcePath, line, column) in original source |
Three methods:
Position mapPosition(int targetLine, int targetColumn) — maps a 1-indexed transpiled position to the original source. Returns null when no mapping exists (gap segment, out-of-range line, column before the first segment on a line).String getSourceLineText(String sourcePath, int lineNumber) — returns a line of original source text by path and 1-indexed line number. Used for parser error messages.String getPrimarySourceContent() — returns the full text of the first original source. Used for the debugger compilation handoff.Parses an ECMA-426 v3 source map and implements SourceMapper. Additional accessors:
List<String> sources() — resolved source paths (after sourceRoot is prepended)List<String> ignoreList() — resolved paths of sources listed in ignoreListString file() — the file field, or nullString getMappedName(int targetLine, int targetColumn) — the original symbol name from the 5th VLQ field (names array), or null if the segment carries no nameThree factory methods: parse(String), parse(Reader), parseFile(Path).
The XSSI prefix )]}' is stripped automatically per ECMA-426 §3.2.
Not supported: indexed (sectioned) source maps (sections field) — parsing throws SourceMapException. Transitive (chained) source maps are also out of scope.
SourceMapJsonParser is a self-contained recursive-descent parser with no dependency on any JSON library. It produces:
LinkedHashMap<String, Object> (insertion order preserved)ArrayList<Object>String (full escape handling including \uXXXX)Long; floats/exponents → DoubleBoolean; null → nullAll errors throw SourceMapException with the byte offset.
MappingsDecoder decodes the Base64-VLQ mappings string into List<List<Segment>> grouped by 0-indexed generated line.
Key behaviours:
mapPosition correct regardless.long; decoded magnitude > Integer.MAX_VALUE throws. This enforces the ECMA-426 requirement to reject fields exceeding 32 bits. VLQs with many continuation-bit digits but a small final value (e.g. the validMappingLargeVLQ spec case) are accepted correctly.Segment field counts: 1 (gap, no source info), 4 (source mapped), or 5 (source + name). Other counts throw.
The implementation is validated against the tc39/source-map-tests suite (submoduled at tests/source-map-tests). 93 of 99 cases pass. The 6 excluded cases are:
| Case | Reason |
|---|---|
basicMappingWithIndexMap | Indexed map (sections) — not supported |
indexMapEmptySections | Indexed map |
indexMapWithMissingFile | Indexed map |
indexMapWithTwoConcatenatedSources | Indexed map |
transitiveMapping | checkMappingTransitive action — chained maps not supported |
transitiveMappingWithThreeSteps | checkMappingTransitive action |
The excludelist lives at tests/testsrc/source-map-tests-excludelist.txt. Cases on the list that start passing will fail the build (keeps the list honest). To regenerate after fixing bugs:
./gradlew :tests:test --tests SourceMapSpecSuiteTest -DupdateSourceMapTestsExcludelist=true
| Where | What |
|---|---|
Parser.mapLocation | Remaps error position and fetches original source line text |
Context debugger handoff | Passes getPrimarySourceContent() to the debugger at compile time |
CodeGenerator / BodyCodegen | Consume only position.line(); unaffected by the sourcePath addition |