Skip to content

Commit a0a03bd

Browse files
committed
Compiler: do not convert the absense of file to the empty file
1 parent 6f38333 commit a0a03bd

File tree

6 files changed

+51
-43
lines changed

6 files changed

+51
-43
lines changed

compiler/bin-js_of_ocaml/cmd_arg.ml

+6-6
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ let options =
302302
then
303303
let file, sm_output_file =
304304
match output_file with
305-
| `Name file, _ when sourcemap_inline_in_js -> file, None
306-
| `Name file, _ -> file, Some (chop_extension file ^ ".map")
307-
| `Stdout, _ -> "STDIN", None
305+
| `Name file, _ when sourcemap_inline_in_js -> Some file, None
306+
| `Name file, _ -> Some file, Some (chop_extension file ^ ".map")
307+
| `Stdout, _ -> None, None
308308
in
309309
Some
310310
( sm_output_file
@@ -531,9 +531,9 @@ let options_runtime_only =
531531
then
532532
let file, sm_output_file =
533533
match output_file with
534-
| `Name file, _ when sourcemap_inline_in_js -> file, None
535-
| `Name file, _ -> file, Some (chop_extension file ^ ".map")
536-
| `Stdout, _ -> "STDIN", None
534+
| `Name file, _ when sourcemap_inline_in_js -> Some file, None
535+
| `Name file, _ -> Some file, Some (chop_extension file ^ ".map")
536+
| `Stdout, _ -> None, None
537537
in
538538
Some
539539
( sm_output_file

compiler/bin-js_of_ocaml/link.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ let options =
9696
then
9797
let file, sm_output_file =
9898
match output_file with
99-
| Some file when sourcemap_inline_in_js -> file, None
100-
| Some file -> file, Some (chop_extension file ^ ".map")
101-
| None -> "STDIN", None
99+
| Some file when sourcemap_inline_in_js -> Some file, None
100+
| Some file -> Some file, Some (chop_extension file ^ ".map")
101+
| None -> None, None
102102
in
103103
Some
104104
( sm_output_file

compiler/lib/source_map.ml

+36-28
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,17 @@ let list_stringlit_opt name rest =
259259
module Standard = struct
260260
type t =
261261
{ version : int
262-
; file : string
262+
; file : string option
263263
; sourceroot : string option
264264
; sources : string list
265265
; sources_content : Source_content.t option list option
266266
; names : string list
267267
; mappings : Mappings.t
268268
}
269269

270-
let empty ~filename =
270+
let empty =
271271
{ version = 3
272-
; file = filename
272+
; file = None
273273
; sourceroot = None
274274
; sources = []
275275
; sources_content = None
@@ -356,7 +356,7 @@ module Standard = struct
356356
in
357357
let acc_rev, mappings_rev =
358358
loop
359-
{ (empty ~filename:"") with sources_content = Some [] }
359+
{ empty with sources_content = Some [] }
360360
[]
361361
~sources_offset:0
362362
~names_offset:0
@@ -379,7 +379,10 @@ module Standard = struct
379379
| None -> None
380380
| Some v -> Some (name, v))
381381
[ "version", Some (`Intlit (string_of_int t.version))
382-
; "file", Some (stringlit (rewrite_path t.file))
382+
; ( "file"
383+
, match t.file with
384+
| None -> None
385+
| Some file -> Some (stringlit (rewrite_path file)) )
383386
; ( "sourceRoot"
384387
, match t.sourceroot with
385388
| None -> None
@@ -403,11 +406,7 @@ module Standard = struct
403406
match json with
404407
| `Assoc (("version", `Intlit version) :: rest) when int_of_string version = 3 ->
405408
let string name json = Option.map ~f:string_of_stringlit (stringlit name json) in
406-
let file =
407-
match string "file" rest with
408-
| None -> ""
409-
| Some s -> s
410-
in
409+
let file = string "file" rest in
411410
let sourceroot = string "sourceRoot" rest in
412411
let names =
413412
match list_stringlit "names" rest with
@@ -457,29 +456,38 @@ module Index = struct
457456

458457
type t =
459458
{ version : int
460-
; file : string
459+
; file : string option
461460
; sections : (offset * [ `Map of Standard.t ]) list
462461
}
463462

464463
let json t =
465464
let stringlit s = `Stringlit (Yojson.Safe.to_string (`String s)) in
466465
`Assoc
467-
[ "version", `Intlit (string_of_int t.version)
468-
; "file", stringlit (rewrite_path t.file)
469-
; ( "sections"
470-
, `List
471-
(List.map
472-
~f:(fun ({ gen_line; gen_column }, `Map sm) ->
473-
`Assoc
474-
[ ( "offset"
475-
, `Assoc
476-
[ "line", `Intlit (string_of_int gen_line)
477-
; "column", `Intlit (string_of_int gen_column)
478-
] )
479-
; "map", Standard.json sm
480-
])
481-
t.sections) )
482-
]
466+
(List.filter_map
467+
~f:(fun (name, v) ->
468+
match v with
469+
| None -> None
470+
| Some v -> Some (name, v))
471+
[ "version", Some (`Intlit (string_of_int t.version))
472+
; ( "file"
473+
, match t.file with
474+
| None -> None
475+
| Some file -> Some (stringlit (rewrite_path file)) )
476+
; ( "sections"
477+
, Some
478+
(`List
479+
(List.map
480+
~f:(fun ({ gen_line; gen_column }, `Map sm) ->
481+
`Assoc
482+
[ ( "offset"
483+
, `Assoc
484+
[ "line", `Intlit (string_of_int gen_line)
485+
; "column", `Intlit (string_of_int gen_column)
486+
] )
487+
; "map", Standard.json sm
488+
])
489+
t.sections)) )
490+
])
483491

484492
let intlit ~errmsg name json =
485493
match List.assoc name json with
@@ -534,7 +542,7 @@ module Index = struct
534542
match List.assoc "sections" fields with
535543
| `List sections ->
536544
let sections = List.map ~f:section_of_json sections in
537-
{ version = 3; file = Option.value file ~default:""; sections }
545+
{ version = 3; file; sections }
538546
| _ -> invalid_arg "Source_map_io.Index.of_json: `sections` is not an array"
539547
| exception Not_found ->
540548
invalid_arg "Source_map_io.Index.of_json: no `sections` field")

compiler/lib/source_map.mli

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ end
7070
module Standard : sig
7171
type t =
7272
{ version : int
73-
; file : string
73+
; file : string option
7474
; sourceroot : string option
7575
; sources : string list
7676
; sources_content : Source_content.t option list option
@@ -90,7 +90,7 @@ module Standard : sig
9090
(** Merge two lists of debug mappings. The time cost of the merge is more than
9191
linear in function of the size of the input mappings. *)
9292

93-
val empty : filename:string -> t
93+
val empty : t
9494
end
9595

9696
module Index : sig
@@ -101,7 +101,7 @@ module Index : sig
101101

102102
type nonrec t =
103103
{ version : int
104-
; file : string
104+
; file : string option
105105
; sections : (offset * [ `Map of Standard.t ]) list
106106
}
107107
end

compiler/tests-compiler/build_path_prefix_map.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ open Util
2121

2222
let%expect_test _ =
2323
let print_section (sm : Js_of_ocaml_compiler.Source_map.Standard.t) =
24-
Printf.printf "file: %s\n" sm.file;
24+
Printf.printf "file: %s\n" (Option.value ~default:"<none>" sm.file);
2525
Printf.printf "sourceRoot: %s\n" (Option.value ~default:"<none>" sm.sourceroot);
2626
Printf.printf "sources:\n";
2727
List.iter sm.sources ~f:(fun source -> Printf.printf "- %s\n" (normalize_path source))

compiler/tests-compiler/sourcemap.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ let%expect_test _ =
134134
{ gen_line; gen_col; ori_source = source; ori_line = line; ori_col = col }
135135
in
136136
let s1 : Source_map.Standard.t =
137-
{ (Source_map.Standard.empty ~filename:"1.map") with
137+
{ Source_map.Standard.empty with
138138
names = [ "na"; "nb"; "nc" ]
139139
; sources = [ "sa"; "sb" ]
140140
; mappings =
141141
Source_map.Mappings.encode [ gen (1, 1) (10, 10) 0; gen (3, 3) (20, 20) 1 ]
142142
}
143143
in
144144
let s2 : Source_map.Standard.t =
145-
{ (Source_map.Standard.empty ~filename:"2.map") with
145+
{ Source_map.Standard.empty with
146146
names = [ "na2"; "nb2" ]
147147
; sources = [ "sa2" ]
148148
; mappings = Source_map.Mappings.encode [ gen (3, 3) (5, 5) 0 ]

0 commit comments

Comments
 (0)