Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for better pretty printing JS output using flow pretty printer #55

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/lib/RehpDriver.re
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,9 @@ let f =
standalone
? augmentWithLinkInfoStandalone(~linkall, ~shouldExportRuntime)
: augmentWithLinkInfoSeparate;

let post_process_output () =
Pretty_print.post_process(formatter, x => x);

configure(formatter)
>> specialize_js_once(~file?, ~projectRoot?, d)
Expand All @@ -644,7 +647,8 @@ let f =
>> coloring
>> check
/* Print the transformed target langauge and include any linked stubs */
>> outputter(formatter, ~custom_header, ~source_map?);
>> outputter(formatter, ~custom_header, ~source_map?)
>> post_process_output;
};

let profiles = [(1, o1), (2, o2), (3, o3)];
Expand Down
24 changes: 21 additions & 3 deletions compiler/lib/pretty_print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ type t =
mutable line : int;
mutable col : int;
mutable total : int;
output : string -> int -> int -> unit }
output : string -> int -> int -> unit;
finally : string -> unit;
get_accumulated_output: unit -> string }

let spaces = String.make 80 ' '
let output st (s : string) l =
Expand Down Expand Up @@ -221,19 +223,35 @@ let newline st =
st.cur <- 0; st.l <- []; st.n <- 0; st.w <- 0

let to_out_channel ch =
let buffer = Buffer.create 100 in
{ indent = 0; box_indent = 0; prev_indents = [];
limit = 78; cur = 0; l = []; n = 0; w = 0;
col = 0; line = 0; total = 0;
compact = false; pending_space = None; last_char = None; needed_space = None;
output = output_substring ch
output = (fun s i l -> Buffer.add_substring buffer s i l);
finally = (fun s ->
Buffer.clear buffer;
output_string ch s);
get_accumulated_output = fun () -> Buffer.contents buffer;
}

let to_buffer b =
let buffer = Buffer.create 100 in
{ indent = 0; box_indent = 0; prev_indents = [];
limit = 78; cur = 0; l = []; n = 0; w = 0;
col = 0; line = 0; total = 0;
compact = false; pending_space = None; last_char = None; needed_space = None;
output = fun s i l -> Buffer.add_substring b s i l }
output = (fun s i l -> Buffer.add_substring buffer s i l);
finally = (fun s ->
Buffer.clear buffer;
Buffer.add_string b s);
get_accumulated_output = fun () -> Buffer.contents buffer;
melwyn95 marked this conversation as resolved.
Show resolved Hide resolved
}

let post_process t fn =
let str = t.get_accumulated_output () in
let processed_str = fn str in
t.finally processed_str

let set_compact st v = st.compact <- v

Expand Down
2 changes: 2 additions & 0 deletions compiler/lib/pretty_print.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ val total : t -> int

val set_compact : t -> bool -> unit
val set_needed_space_function : t -> (char -> char -> bool) -> unit

val post_process : t -> (string -> string) -> unit
Loading