Skip to content

Commit 6f38333

Browse files
committed
Compiler: do not convert the absense of sourceRoot to empty sourceRoot
1 parent 3158d8c commit 6f38333

File tree

5 files changed

+45
-30
lines changed

5 files changed

+45
-30
lines changed

compiler/lib/source_map.ml

+25-19
Original file line numberDiff line numberDiff line change
@@ -373,25 +373,31 @@ module Standard = struct
373373
let json t =
374374
let stringlit s = `Stringlit (Yojson.Safe.to_string (`String s)) in
375375
`Assoc
376-
[ "version", `Intlit (string_of_int t.version)
377-
; "file", stringlit (rewrite_path t.file)
378-
; ( "sourceRoot"
379-
, stringlit
380-
(match t.sourceroot with
381-
| None -> ""
382-
| Some s -> rewrite_path s) )
383-
; "names", `List (List.map t.names ~f:(fun s -> stringlit s))
384-
; "sources", `List (List.map t.sources ~f:(fun s -> stringlit (rewrite_path s)))
385-
; "mappings", stringlit (Mappings.to_string t.mappings)
386-
; ( "sourcesContent"
387-
, `List
388-
(match t.sources_content with
389-
| None -> []
390-
| Some l ->
391-
List.map l ~f:(function
392-
| None -> `Null
393-
| Some x -> Source_content.to_json x)) )
394-
]
376+
(List.filter_map
377+
~f:(fun (name, v) ->
378+
match v with
379+
| None -> None
380+
| Some v -> Some (name, v))
381+
[ "version", Some (`Intlit (string_of_int t.version))
382+
; "file", Some (stringlit (rewrite_path t.file))
383+
; ( "sourceRoot"
384+
, match t.sourceroot with
385+
| None -> None
386+
| Some s -> Some (stringlit (rewrite_path s)) )
387+
; "names", Some (`List (List.map t.names ~f:(fun s -> stringlit s)))
388+
; ( "sources"
389+
, Some (`List (List.map t.sources ~f:(fun s -> stringlit (rewrite_path s)))) )
390+
; "mappings", Some (stringlit (Mappings.to_string t.mappings))
391+
; ( "sourcesContent"
392+
, match t.sources_content with
393+
| None -> None
394+
| Some l ->
395+
Some
396+
(`List
397+
(List.map l ~f:(function
398+
| None -> `Null
399+
| Some x -> Source_content.to_json x))) )
400+
])
395401

396402
let of_json (json : Yojson.Raw.t) =
397403
match json with

compiler/tests-compiler/build_path_prefix_map.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ let%expect_test _ =
4141
| None -> failwith "no sourcemap generated!");
4242
[%expect
4343
{|
44-
file: test.js
45-
sourceRoot:
46-
sources:
47-
- /dune-root/test.ml
44+
file: test.js
45+
sourceRoot: <none>
46+
sources:
47+
- /dune-root/test.ml
4848
|}]
+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
sourcemap for test.bc.js
2-
b.ml:1:4 -> 12: function <>f(x){return x - 1 | 0;}
3-
b.ml:1:6 -> 14: function f(<>x){return x - 1 | 0;}
4-
b.ml:1:10 -> 17: function f(x){<>return x - 1 | 0;}
5-
b.ml:1:6 -> 24: function f(x){return <>x - 1 | 0;}
6-
b.ml:1:15 -> 34: function f(x){return x - 1 | 0;<>}
7-
b.ml:1:4 -> 23: var Testlib_B = [0, <>f];
2+
/my/sourceRoot#b.ml:1:4 -> 12: function <>f(x){return x - 1 | 0;}
3+
/my/sourceRoot#b.ml:1:6 -> 14: function f(<>x){return x - 1 | 0;}
4+
/my/sourceRoot#b.ml:1:10 -> 17: function f(x){<>return x - 1 | 0;}
5+
/my/sourceRoot#b.ml:1:6 -> 24: function f(x){return <>x - 1 | 0;}
6+
/my/sourceRoot#b.ml:1:15 -> 34: function f(x){return x - 1 | 0;<>}
7+
/my/sourceRoot#b.ml:1:4 -> 23: var Testlib_B = [0, <>f];

compiler/tests-sourcemap/dump_sourcemap.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ let print_mapping lines ?(line_offset = 0) (sm : Source_map.Standard.t) =
6262
-> (
6363
match file ori_source with
6464
| "a.ml" | "b.ml" | "c.ml" | "d.ml" ->
65+
let root =
66+
match sm.sourceroot with
67+
| None -> ""
68+
| Some root -> root ^ "#"
69+
in
6570
Printf.printf
66-
"%s:%d:%d -> %d:%s\n"
71+
"%s%s:%d:%d -> %d:%s\n"
72+
root
6773
(file ori_source)
6874
ori_line
6975
ori_col

compiler/tests-sourcemap/dune

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
(name test)
88
(modules test)
99
(modes js)
10+
(js_of_ocaml
11+
(link_flags
12+
(:standard --source-map-root /my/sourceRoot)))
1013
(libraries testlib))
1114

1215
(library

0 commit comments

Comments
 (0)