Skip to content

Commit

Permalink
Compiler: do not convert the absense of file to the empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Oct 15, 2024
1 parent 5be1a65 commit e072cf6
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 43 deletions.
12 changes: 6 additions & 6 deletions compiler/bin-js_of_ocaml/cmd_arg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions compiler/bin-js_of_ocaml/link.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 36 additions & 28 deletions compiler/lib/source_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,17 @@ 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
; names : string list
; mappings : Mappings.t
}

let empty ~filename =
let empty =
{ version = 3
; file = filename
; file = None
; sourceroot = None
; sources = []
; sources_content = None
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions compiler/lib/source_map.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -90,7 +90,7 @@ module Standard : sig
(** Merge two lists of debug mappings. The time cost of the merge is more than
linear in function of the size of the input mappings. *)

val empty : filename:string -> t
val empty : t
end

module Index : sig
Expand All @@ -101,7 +101,7 @@ module Index : sig

type nonrec t =
{ version : int
; file : string
; file : string option
; sections : (offset * [ `Map of Standard.t ]) list
}
end
Expand Down
2 changes: 1 addition & 1 deletion compiler/tests-compiler/build_path_prefix_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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:"<none>" sm.file);
Printf.printf "sourceRoot: %s\n" (Option.value ~default:"<none>" sm.sourceroot);
Printf.printf "sources:\n";
List.iter sm.sources ~f:(fun source -> Printf.printf "- %s\n" (normalize_path source))
Expand Down
4 changes: 2 additions & 2 deletions compiler/tests-compiler/sourcemap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ 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 =
Source_map.Mappings.encode [ gen (1, 1) (10, 10) 0; gen (3, 3) (20, 20) 1 ]
}
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 ]
Expand Down

0 comments on commit e072cf6

Please sign in to comment.