diff --git a/compiler/lib/source_map.ml b/compiler/lib/source_map.ml index 8e4308c760..7af7b26bb5 100644 --- a/compiler/lib/source_map.ml +++ b/compiler/lib/source_map.ml @@ -373,25 +373,31 @@ module Standard = struct let json t = let stringlit s = `Stringlit (Yojson.Safe.to_string (`String s)) in `Assoc - [ "version", `Intlit (string_of_int t.version) - ; "file", stringlit (rewrite_path t.file) - ; ( "sourceRoot" - , stringlit - (match t.sourceroot with - | None -> "" - | Some s -> rewrite_path s) ) - ; "names", `List (List.map t.names ~f:(fun s -> stringlit s)) - ; "sources", `List (List.map t.sources ~f:(fun s -> stringlit (rewrite_path s))) - ; "mappings", stringlit (Mappings.to_string t.mappings) - ; ( "sourcesContent" - , `List - (match t.sources_content with - | None -> [] - | Some l -> - List.map l ~f:(function - | None -> `Null - | Some x -> Source_content.to_json x)) ) - ] + (List.filter_map + ~f:(fun (name, v) -> + match v with + | None -> None + | Some v -> Some (name, v)) + [ "version", Some (`Intlit (string_of_int t.version)) + ; "file", Some (stringlit (rewrite_path t.file)) + ; ( "sourceRoot" + , match t.sourceroot with + | None -> None + | Some s -> Some (stringlit (rewrite_path s)) ) + ; "names", Some (`List (List.map t.names ~f:(fun s -> stringlit s))) + ; ( "sources" + , Some (`List (List.map t.sources ~f:(fun s -> stringlit (rewrite_path s)))) ) + ; "mappings", Some (stringlit (Mappings.to_string t.mappings)) + ; ( "sourcesContent" + , match t.sources_content with + | None -> None + | Some l -> + Some + (`List + (List.map l ~f:(function + | None -> `Null + | Some x -> Source_content.to_json x))) ) + ]) let of_json (json : Yojson.Raw.t) = match json with diff --git a/compiler/tests-compiler/build_path_prefix_map.ml b/compiler/tests-compiler/build_path_prefix_map.ml index d77c3d62f4..b6f9964e3d 100644 --- a/compiler/tests-compiler/build_path_prefix_map.ml +++ b/compiler/tests-compiler/build_path_prefix_map.ml @@ -41,8 +41,8 @@ let%expect_test _ = | None -> failwith "no sourcemap generated!"); [%expect {| - file: test.js - sourceRoot: - sources: - - /dune-root/test.ml + file: test.js + sourceRoot: + sources: + - /dune-root/test.ml |}] diff --git a/compiler/tests-sourcemap/dump.reference b/compiler/tests-sourcemap/dump.reference index d9a8cf150c..be88f6df65 100644 --- a/compiler/tests-sourcemap/dump.reference +++ b/compiler/tests-sourcemap/dump.reference @@ -1,7 +1,7 @@ sourcemap for test.bc.js -b.ml:1:4 -> 12: function <>f(x){return x - 1 | 0;} -b.ml:1:6 -> 14: function f(<>x){return x - 1 | 0;} -b.ml:1:10 -> 17: function f(x){<>return x - 1 | 0;} -b.ml:1:6 -> 24: function f(x){return <>x - 1 | 0;} -b.ml:1:15 -> 34: function f(x){return x - 1 | 0;<>} -b.ml:1:4 -> 23: var Testlib_B = [0, <>f]; +/my/sourceRoot#b.ml:1:4 -> 12: function <>f(x){return x - 1 | 0;} +/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:6 -> 24: function f(x){return <>x - 1 | 0;} +/my/sourceRoot#b.ml:1:15 -> 34: function f(x){return x - 1 | 0;<>} +/my/sourceRoot#b.ml:1:4 -> 23: var Testlib_B = [0, <>f]; diff --git a/compiler/tests-sourcemap/dump_sourcemap.ml b/compiler/tests-sourcemap/dump_sourcemap.ml index 82fcbbdcaf..59bb828bd9 100644 --- a/compiler/tests-sourcemap/dump_sourcemap.ml +++ b/compiler/tests-sourcemap/dump_sourcemap.ml @@ -62,8 +62,14 @@ let print_mapping lines ?(line_offset = 0) (sm : Source_map.Standard.t) = -> ( match file ori_source with | "a.ml" | "b.ml" | "c.ml" | "d.ml" -> + let root = + match sm.sourceroot with + | None -> "" + | Some root -> root ^ "#" + in Printf.printf - "%s:%d:%d -> %d:%s\n" + "%s%s:%d:%d -> %d:%s\n" + root (file ori_source) ori_line ori_col diff --git a/compiler/tests-sourcemap/dune b/compiler/tests-sourcemap/dune index e690f5a3ee..6c96b3aef1 100644 --- a/compiler/tests-sourcemap/dune +++ b/compiler/tests-sourcemap/dune @@ -7,6 +7,9 @@ (name test) (modules test) (modes js) + (js_of_ocaml + (link_flags + (:standard --source-map-root /my/sourceRoot))) (libraries testlib)) (library