From 5175daf5e93782d5ddfe5b4d013aca2088e9cfe8 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Tue, 15 Oct 2024 15:48:26 +0200 Subject: [PATCH] Compiler: do not convert the absense of file to the empty file --- compiler/bin-js_of_ocaml/cmd_arg.ml | 12 ++-- compiler/bin-js_of_ocaml/link.ml | 6 +- compiler/lib/source_map.ml | 64 +++++++++++-------- compiler/lib/source_map.mli | 6 +- .../tests-compiler/build_path_prefix_map.ml | 2 +- compiler/tests-compiler/sourcemap.ml | 4 +- 6 files changed, 51 insertions(+), 43 deletions(-) diff --git a/compiler/bin-js_of_ocaml/cmd_arg.ml b/compiler/bin-js_of_ocaml/cmd_arg.ml index 28fa323cfa..98d1cfee8a 100644 --- a/compiler/bin-js_of_ocaml/cmd_arg.ml +++ b/compiler/bin-js_of_ocaml/cmd_arg.ml @@ -302,9 +302,9 @@ let options = then let file, sm_output_file = match output_file with - | `Name file, _ when sourcemap_inline_in_js -> file, None - | `Name file, _ -> file, Some (chop_extension file ^ ".map") - | `Stdout, _ -> "STDIN", None + | `Name file, _ when sourcemap_inline_in_js -> Some file, None + | `Name file, _ -> Some file, Some (chop_extension file ^ ".map") + | `Stdout, _ -> None, None in Some ( sm_output_file @@ -531,9 +531,9 @@ let options_runtime_only = then let file, sm_output_file = match output_file with - | `Name file, _ when sourcemap_inline_in_js -> file, None - | `Name file, _ -> file, Some (chop_extension file ^ ".map") - | `Stdout, _ -> "STDIN", None + | `Name file, _ when sourcemap_inline_in_js -> Some file, None + | `Name file, _ -> Some file, Some (chop_extension file ^ ".map") + | `Stdout, _ -> None, None in Some ( sm_output_file diff --git a/compiler/bin-js_of_ocaml/link.ml b/compiler/bin-js_of_ocaml/link.ml index 8ac643dc19..6b0f8add7f 100644 --- a/compiler/bin-js_of_ocaml/link.ml +++ b/compiler/bin-js_of_ocaml/link.ml @@ -96,9 +96,9 @@ let options = then let file, sm_output_file = match output_file with - | Some file when sourcemap_inline_in_js -> file, None - | Some file -> file, Some (chop_extension file ^ ".map") - | None -> "STDIN", None + | Some file when sourcemap_inline_in_js -> Some file, None + | Some file -> Some file, Some (chop_extension file ^ ".map") + | None -> None, None in Some ( sm_output_file diff --git a/compiler/lib/source_map.ml b/compiler/lib/source_map.ml index 7af7b26bb5..1ae1b2f099 100644 --- a/compiler/lib/source_map.ml +++ b/compiler/lib/source_map.ml @@ -259,7 +259,7 @@ let list_stringlit_opt name rest = module Standard = struct type t = { version : int - ; file : string + ; file : string option ; sourceroot : string option ; sources : string list ; sources_content : Source_content.t option list option @@ -267,9 +267,9 @@ module Standard = struct ; mappings : Mappings.t } - let empty ~filename = + let empty = { version = 3 - ; file = filename + ; file = None ; sourceroot = None ; sources = [] ; sources_content = None @@ -356,7 +356,7 @@ module Standard = struct in let acc_rev, mappings_rev = loop - { (empty ~filename:"") with sources_content = Some [] } + { empty with sources_content = Some [] } [] ~sources_offset:0 ~names_offset:0 @@ -379,7 +379,10 @@ module Standard = struct | None -> None | Some v -> Some (name, v)) [ "version", Some (`Intlit (string_of_int t.version)) - ; "file", Some (stringlit (rewrite_path t.file)) + ; ( "file" + , match t.file with + | None -> None + | Some file -> Some (stringlit (rewrite_path file)) ) ; ( "sourceRoot" , match t.sourceroot with | None -> None @@ -403,11 +406,7 @@ module Standard = struct match json with | `Assoc (("version", `Intlit version) :: rest) when int_of_string version = 3 -> let string name json = Option.map ~f:string_of_stringlit (stringlit name json) in - let file = - match string "file" rest with - | None -> "" - | Some s -> s - in + let file = string "file" rest in let sourceroot = string "sourceRoot" rest in let names = match list_stringlit "names" rest with @@ -457,29 +456,38 @@ module Index = struct type t = { version : int - ; file : string + ; file : string option ; sections : (offset * [ `Map of Standard.t ]) list } 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) - ; ( "sections" - , `List - (List.map - ~f:(fun ({ gen_line; gen_column }, `Map sm) -> - `Assoc - [ ( "offset" - , `Assoc - [ "line", `Intlit (string_of_int gen_line) - ; "column", `Intlit (string_of_int gen_column) - ] ) - ; "map", Standard.json sm - ]) - t.sections) ) - ] + (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" + , match t.file with + | None -> None + | Some file -> Some (stringlit (rewrite_path file)) ) + ; ( "sections" + , Some + (`List + (List.map + ~f:(fun ({ gen_line; gen_column }, `Map sm) -> + `Assoc + [ ( "offset" + , `Assoc + [ "line", `Intlit (string_of_int gen_line) + ; "column", `Intlit (string_of_int gen_column) + ] ) + ; "map", Standard.json sm + ]) + t.sections)) ) + ]) let intlit ~errmsg name json = match List.assoc name json with @@ -534,7 +542,7 @@ module Index = struct match List.assoc "sections" fields with | `List sections -> let sections = List.map ~f:section_of_json sections in - { version = 3; file = Option.value file ~default:""; sections } + { version = 3; file; sections } | _ -> invalid_arg "Source_map_io.Index.of_json: `sections` is not an array" | exception Not_found -> invalid_arg "Source_map_io.Index.of_json: no `sections` field") diff --git a/compiler/lib/source_map.mli b/compiler/lib/source_map.mli index 2a38b92bd7..9b579f6779 100644 --- a/compiler/lib/source_map.mli +++ b/compiler/lib/source_map.mli @@ -70,7 +70,7 @@ end module Standard : sig type t = { version : int - ; file : string + ; file : string option ; sourceroot : string option ; sources : string list ; sources_content : Source_content.t option list option @@ -93,7 +93,7 @@ module Standard : sig linear in function of the size of the input mappings. When possible, prefer using {!val:concat}. *) - val empty : filename:string -> t + val empty : t end module Index : sig @@ -104,7 +104,7 @@ module Index : sig type nonrec t = { version : int - ; file : string + ; file : string option ; sections : (offset * [ `Map of Standard.t ]) list (** List of [(offset, map)] pairs. The sourcemap spec allows for [map] to be either a sourcemap object or a URL, but we don't need to generate diff --git a/compiler/tests-compiler/build_path_prefix_map.ml b/compiler/tests-compiler/build_path_prefix_map.ml index b6f9964e3d..29f9f7c8c3 100644 --- a/compiler/tests-compiler/build_path_prefix_map.ml +++ b/compiler/tests-compiler/build_path_prefix_map.ml @@ -21,7 +21,7 @@ open Util let%expect_test _ = let print_section (sm : Js_of_ocaml_compiler.Source_map.Standard.t) = - Printf.printf "file: %s\n" sm.file; + Printf.printf "file: %s\n" (Option.value ~default:"" sm.file); Printf.printf "sourceRoot: %s\n" (Option.value ~default:"" sm.sourceroot); Printf.printf "sources:\n"; List.iter sm.sources ~f:(fun source -> Printf.printf "- %s\n" (normalize_path source)) diff --git a/compiler/tests-compiler/sourcemap.ml b/compiler/tests-compiler/sourcemap.ml index 91b00e6b2f..2aa2cdb9e1 100644 --- a/compiler/tests-compiler/sourcemap.ml +++ b/compiler/tests-compiler/sourcemap.ml @@ -134,7 +134,7 @@ let%expect_test _ = { gen_line; gen_col; ori_source = source; ori_line = line; ori_col = col } in let s1 : Source_map.Standard.t = - { (Source_map.Standard.empty ~filename:"1.map") with + { Source_map.Standard.empty with names = [ "na"; "nb"; "nc" ] ; sources = [ "sa"; "sb" ] ; mappings = @@ -142,7 +142,7 @@ let%expect_test _ = } in let s2 : Source_map.Standard.t = - { (Source_map.Standard.empty ~filename:"2.map") with + { Source_map.Standard.empty with names = [ "na2"; "nb2" ] ; sources = [ "sa2" ] ; mappings = Source_map.Mappings.encode [ gen (3, 3) (5, 5) 0 ]