Skip to content

Commit 62b90bc

Browse files
vouillonhhugo
authored andcommitted
Source map: repeat mappings over new lines
Firefox assumes that a mapping stops at the end of a line, which is inconvenient. When this happens, we repeat the mapping on the next line.
1 parent 6f53be3 commit 62b90bc

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

compiler/lib/js_output.ml

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

compiler/tests-compiler/sourcemap.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ let%expect_test _ =
8585
13: //# sourceMappingURL=test.map
8686
/builtin/blackbox.ml:1:0 -> 5:7
8787
/builtin/blackbox.ml:1:0 -> 5:17
88+
/builtin/blackbox.ml:1:0 -> 6:0
8889
/dune-root/test.ml:1:4 -> 6:12
8990
/dune-root/test.ml:1:7 -> 6:15
9091
/dune-root/test.ml:1:11 -> 6:18
9192
/dune-root/test.ml:1:12 -> 6:28
93+
/dune-root/test.ml:1:12 -> 7:0
9294
/dune-root/test.ml:1:12 -> 7:7
9395
/builtin/blackbox.ml:1:0 -> 7:14
9496
|}]

compiler/tests-sourcemap/dump.reference

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ sourcemap for test.bc.js
33
/my/sourceRoot#b.ml:1:6 -> 14: function f(<>x){return x - 1 | 0; }
44
/my/sourceRoot#b.ml:1:10 -> 17: function f(x){<>return x - 1 | 0; }
55
/my/sourceRoot#b.ml:1:15 -> 35: function f(x){return x - 1 | 0; <>}
6+
/my/sourceRoot#b.ml:1:15 -> 0:<> var Testlib_B = [0, f];
67
/my/sourceRoot#b.ml:1:15 -> 7: var <>Testlib_B = [0, f];

0 commit comments

Comments
 (0)