Skip to content

Commit

Permalink
Compiler: do not convert the absense of sourceRoot to empty sourceRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Oct 15, 2024
1 parent ab81425 commit a406b34
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
44 changes: 25 additions & 19 deletions compiler/lib/source_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions compiler/tests-compiler/build_path_prefix_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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: <none>
sources:
- /dune-root/test.ml
|}]
12 changes: 6 additions & 6 deletions compiler/tests-sourcemap/dump.reference
Original file line number Diff line number Diff line change
@@ -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];
8 changes: 7 additions & 1 deletion compiler/tests-sourcemap/dump_sourcemap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions compiler/tests-sourcemap/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(name test)
(modules test)
(modes js)
(js_of_ocaml (link_flags (:standard --source-map-root /my/sourceRoot)))
(libraries testlib))

(library
Expand Down

0 comments on commit a406b34

Please sign in to comment.