diff --git a/build/serializer/sourceMap.js b/build/serializer/sourceMap.js index 547e53377a7c560db8dfd983601606de15896e07..29a0c855088bceacb7ba00a627615539cbf37738 100644 --- a/build/serializer/sourceMap.js +++ b/build/serializer/sourceMap.js @@ -27,6 +27,13 @@ function loadSourcemapCodec() { } return _sourcemapCodec; } +let _traceMapping; +function loadTraceMapping() { + if (!_traceMapping) { + _traceMapping = require('@jridgewell/trace-mapping'); + } + return _traceMapping; +} let _Generator; function loadGenerator() { if (!_Generator) { @@ -224,6 +231,58 @@ function repairInvalidNegativeIndices(map) { } return changed ? { ...map, mappings: encode(decoded) } : map; } +// Hermes can emit mappings for Metro trailer/debug lines that have no +// corresponding Metro source-map line. @jridgewell/remapping assumes every +// referenced generated line exists, so strip those invalid bridge segments. +function getGeneratedLineCount(map) { + const { TraceMap, decodedMappings } = loadTraceMapping(); + return decodedMappings(new TraceMap(map)).length; +} +function dropInvalidOriginalSegments(map, maxOriginalLine) { + const { TraceMap, decodedMappings } = loadTraceMapping(); + const decoded = decodedMappings(new TraceMap(map)); + let didChange = false; + const filtered = decoded.map((line) => { + const filteredLine = line.filter((segment) => { + if (segment.length < 4) { + return true; + } + const sourceIndex = segment[1]; + const sourceLine = segment[2]; + const sourceColumn = segment[3]; + return (sourceIndex >= 0 && + sourceIndex < map.sources.length && + sourceLine >= 0 && + sourceLine < maxOriginalLine && + sourceColumn >= 0); + }); + if (filteredLine.length !== line.length) { + didChange = true; + } + return filteredLine; + }); + if (!didChange) { + return map; + } + return { + ...map, + mappings: loadSourcemapCodec().encode(filtered), + }; +} +function sanitizeSourceMapsForComposition(maps) { + let sanitized = maps; + for (let index = 1; index < maps.length; index++) { + const maxOriginalLine = getGeneratedLineCount(sanitized[index - 1]); + const next = dropInvalidOriginalSegments(sanitized[index], maxOriginalLine); + if (next !== sanitized[index]) { + if (sanitized === maps) { + sanitized = maps.slice(); + } + sanitized[index] = next; + } + } + return sanitized; +} // `maps[0]` is the original-most transform; `maps[maps.length - 1]` is // the most recent. Built on `@jridgewell/remapping` instead of mozilla's // `SourceMapConsumer`-based composer. @@ -251,7 +310,7 @@ function composeSourceMaps(maps) { return { ...map, ignoreList: map.x_google_ignoreList }; }); // Metro convention is original-first; remapping is most-recent first. - let input = normalized.slice().reverse(); + let input = sanitizeSourceMapsForComposition(normalized).slice().reverse(); const remap = loadRemapping(); let composed; try {