@@ -2104,21 +2104,38 @@ let program ?(accept_unnamed_var = false) ?(source_map = false) f p =
21042104 | true ->
21052105 let sources = hashtbl_to_list files in
21062106 let names = hashtbl_to_list names in
2107+ let relocate pos m =
2108+ let gen_line = pos.PP. p_line + 1 in
2109+ let gen_col = pos.PP. p_col in
2110+ match m with
2111+ | Source_map. Gen { gen_col = _ ; gen_line = _ } ->
2112+ Source_map. Gen { gen_col; gen_line }
2113+ | Source_map. Gen_Ori m -> Source_map. Gen_Ori { m with gen_line; gen_col }
2114+ | Source_map. Gen_Ori_Name m ->
2115+ Source_map. Gen_Ori_Name { m with gen_line; gen_col }
2116+ in
2117+ let rec build_mappings pos mapping prev_mappings =
2118+ match mapping with
2119+ | [] -> prev_mappings
2120+ | (pos' , m ) :: rem ->
2121+ (* Firefox assumes that a mapping stops at the end of a
2122+ line, which is inconvenient. When this happens, we
2123+ repeat the mapping on the next line. *)
2124+ if pos'.PP. p_line = pos.PP. p_line
2125+ || (pos'.p_line = pos.p_line - 1 && pos.p_col = 0 )
2126+ then build_mappings pos' rem (relocate pos' m :: prev_mappings)
2127+ else if pos.p_col > 0
2128+ then
2129+ let pos = { pos with p_col = 0 } in
2130+ build_mappings pos mapping (relocate pos m :: prev_mappings)
2131+ else
2132+ let pos = { pos with p_line = pos.p_line - 1 } in
2133+ build_mappings pos mapping (relocate pos m :: prev_mappings)
2134+ in
21072135 let mappings =
2108- List. rev_map ! temp_mappings ~f: (fun (pos , m ) ->
2109- let gen_line = pos.PP. p_line + 1 in
2110- let gen_col = pos.PP. p_col in
2111- match m with
2112- | Source_map. Gen { gen_col = _ ; gen_line = _ } ->
2113- Source_map. Gen { gen_col; gen_line }
2114- | Source_map. Gen_Ori
2115- { gen_line = _; gen_col = _; ori_source; ori_line; ori_col } ->
2116- Source_map. Gen_Ori { gen_line; gen_col; ori_source; ori_line; ori_col }
2117- | Source_map. Gen_Ori_Name
2118- { gen_line = _; gen_col = _; ori_source; ori_line; ori_col; ori_name }
2119- ->
2120- Source_map. Gen_Ori_Name
2121- { gen_line; gen_col; ori_source; ori_line; ori_col; ori_name })
2136+ match ! temp_mappings with
2137+ | [] -> []
2138+ | (pos , m ) :: rem -> build_mappings pos rem [ relocate pos m ]
21222139 in
21232140 { Source_map. sources; names; mappings }
21242141 in
0 commit comments