Skip to content

Commit

Permalink
dump without /./
Browse files Browse the repository at this point in the history
  • Loading branch information
jchavarri committed Jun 2, 2023
1 parent 42b51ef commit 24d0f07
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions jscomp/core/js_dump_import_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,37 @@ let es6_export cxt f (idents : Ident.t list) =
P.group f 0 (fun _ ->
P.string f export;
P.space f;
if not @@ Ext_string.equal export s then (
if not @@ String.equal export s then (
P.string f L.as_;
P.space f;
P.string f s);
P.string f L.comma))
(fun _ -> P.newline f));
outer_cxt

let search_substring pat str start =
let rec search i j =
if j >= String.length pat then i
else if i + j >= String.length str then raise Not_found
else if str.[i + j] = pat.[j] then search i (j + 1)
else search (i + 1) 0
in
search start 0

let replace_substring ~before ~after str =
let rec search acc curr =
match search_substring before str curr with
| next ->
let prefix = String.sub str curr (next - curr) in
search (prefix :: acc) (next + String.length before)
| exception Not_found ->
let suffix = String.sub str curr (String.length str - curr) in
List.rev (suffix :: acc)
in
String.concat after (search [] 0)

let replace_dot_in_path path = replace_substring ~before:"/./" ~after:"/" path

(** Node or Google module style imports *)
let requires require_lit cxt f (modules : (Ident.t * string * bool) list) =
(* the context used to print the following program *)
Expand All @@ -113,7 +136,8 @@ let requires require_lit cxt f (modules : (Ident.t * string * bool) list) =
P.string f L.eq;
P.space f;
P.string f require_lit;
P.paren_group f 0 (fun _ -> Js_dump_string.pp_string f file);
P.paren_group f 0 (fun _ ->
Js_dump_string.pp_string f (replace_dot_in_path file));
if default then P.string f ".default";
P.string f L.semi;
P.newline f);
Expand All @@ -136,7 +160,7 @@ let imports cxt f (modules : (Ident.t * string * bool) list) =
P.space f;
P.string f L.from;
P.space f;
Js_dump_string.pp_string f file)
Js_dump_string.pp_string f (replace_dot_in_path file))
else (
P.string f L.star;
P.space f;
Expand All @@ -147,7 +171,7 @@ let imports cxt f (modules : (Ident.t * string * bool) list) =
P.space f;
P.string f L.from;
P.space f;
Js_dump_string.pp_string f file);
Js_dump_string.pp_string f (replace_dot_in_path file));
P.string f L.semi;
P.newline f);
outer_cxt

0 comments on commit 24d0f07

Please sign in to comment.