Skip to content

Commit

Permalink
Source map: repeat mappings over new lines
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vouillon committed Oct 18, 2024
1 parent 880ceb1 commit 4494d81
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
53 changes: 38 additions & 15 deletions compiler/lib/js_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2154,22 +2154,45 @@ let program ?(accept_unnamed_var = false) f ?source_map p =
String.length filename >= 9 && String.sub filename 0 9 = "/builtin/")
in
let sm_mappings = Source_map.Mappings.decode sm.mappings in
let mappings =
List.rev_append_map !temp_mappings sm_mappings ~f:(fun (pos, m) ->
let gen_line = pos.PP.p_line + 1 in
let gen_col = pos.PP.p_col in
match m with
| Source_map.Gen { gen_col = _; gen_line = _ } ->
Source_map.Gen { gen_col; gen_line }
| Source_map.Gen_Ori
{ gen_line = _; gen_col = _; ori_source; ori_line; ori_col } ->
Source_map.Gen_Ori { gen_line; gen_col; ori_source; ori_line; ori_col }
| Source_map.Gen_Ori_Name
{ gen_line = _; gen_col = _; ori_source; ori_line; ori_col; ori_name }
->
Source_map.Gen_Ori_Name
{ gen_line; gen_col; ori_source; ori_line; ori_col; ori_name })
let relocate pos m =
let gen_line = pos.PP.p_line + 1 in
let gen_col = pos.PP.p_col in
match m with
| Source_map.Gen { gen_col = _; gen_line = _ } ->
Source_map.Gen { gen_col; gen_line }
| Source_map.Gen_Ori m -> Source_map.Gen_Ori { m with gen_line; gen_col }
| Source_map.Gen_Ori_Name m ->
Source_map.Gen_Ori_Name { m with gen_line; gen_col }
in
let prepend mapping prev_mappings =
let rec prepend_rec pos mapping prev_mappings =
match mapping with
| [] -> prev_mappings
| (pos', m) :: rem ->
(*
Format.eprintf
"%d,%d %d,%d@."
pos'.PP.p_line
pos'.PP.p_col
pos.PP.p_line
pos.PP.p_col;
*)
if pos'.PP.p_line = pos.PP.p_line
|| (pos'.p_line = pos.p_line - 1 && pos.p_col = 0)
then prepend_rec pos' rem (relocate pos' m :: prev_mappings)
else if pos.p_col > 0
then
let pos = { pos with p_col = 0 } in
prepend_rec pos mapping (relocate pos m :: prev_mappings)
else
let pos = { pos with p_line = pos.p_line - 1 } in
prepend_rec pos mapping (relocate pos m :: prev_mappings)
in
match mapping with
| [] -> prev_mappings
| (pos, m) :: rem -> prepend_rec pos rem (relocate pos m :: prev_mappings)
in
let mappings = prepend !temp_mappings sm_mappings in
let mappings = Source_map.Mappings.encode mappings in
Some
{ sm with
Expand Down
2 changes: 2 additions & 0 deletions compiler/tests-compiler/sourcemap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ let%expect_test _ =
13: //# sourceMappingURL=test.map
/builtin/blackbox.ml:1:0 -> 5:7
/builtin/blackbox.ml:1:0 -> 5:17
/builtin/blackbox.ml:1:0 -> 6:0
/dune-root/test.ml:1:4 -> 6:12
/dune-root/test.ml:1:7 -> 6:15
/dune-root/test.ml:1:11 -> 6:18
/dune-root/test.ml:1:12 -> 6:28
/dune-root/test.ml:1:12 -> 7:0
/dune-root/test.ml:1:12 -> 7:7
/builtin/blackbox.ml:1:0 -> 7:14
|}]
Expand Down
1 change: 1 addition & 0 deletions compiler/tests-sourcemap/dump.reference
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ sourcemap for test.bc.js
/my/sourceRoot#b.ml:1:6 -> 14: function f(<>x){return x - 1 | 0; }
/my/sourceRoot#b.ml:1:10 -> 17: function f(x){<>return x - 1 | 0; }
/my/sourceRoot#b.ml:1:15 -> 35: function f(x){return x - 1 | 0; <>}
/my/sourceRoot#b.ml:1:15 -> 0:<> var Testlib_B = [0, f];
/my/sourceRoot#b.ml:1:15 -> 7: var <>Testlib_B = [0, f];

0 comments on commit 4494d81

Please sign in to comment.