Skip to content

Commit aad058e

Browse files
authored
Add reprint ReScript source command (#6682)
* add simple command for transforming a ReScript file using the provided PPXes, and then print the transformed source back to stdout * remove forced no builtin ppx flag * inline fn
1 parent fe8090f commit aad058e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

jscomp/bsc/rescript_compiler_main.ml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,48 @@ let process_file sourcefile ?(kind ) ppf =
106106
in
107107
Config.uncurried := uncurried;
108108
res
109+
110+
let reprint_source_file sourcefile =
111+
let uncurried = !Config.uncurried in
112+
let kind = Ext_file_extensions.classify_input (Ext_filename.get_extension_maybe sourcefile) in
113+
let sourcefile = set_abs_input_name sourcefile in
114+
let res = match kind with
115+
| Res ->
116+
let parseResult =
117+
Res_driver.parsingEngine.parseImplementation ~forPrinter:true ~filename:sourcefile
118+
in
119+
if parseResult.invalid then (
120+
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
121+
exit 1
122+
);
123+
Res_compmisc.init_path ();
124+
parseResult.parsetree
125+
|> Cmd_ppx_apply.apply_rewriters ~restore:false ~tool_name:Js_config.tool_name Ml
126+
|> Ppx_entry.rewrite_implementation
127+
|> Res_printer.printImplementation ~width:100 ~comments:parseResult.comments
128+
|> print_endline
129+
| Resi ->
130+
let parseResult =
131+
Res_driver.parsingEngine.parseInterface ~forPrinter:true ~filename:sourcefile
132+
in
133+
if parseResult.invalid then (
134+
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
135+
exit 1
136+
);
137+
Res_compmisc.init_path ();
138+
parseResult.parsetree
139+
|> Cmd_ppx_apply.apply_rewriters ~restore:false ~tool_name:Js_config.tool_name Mli
140+
|> Ppx_entry.rewrite_signature
141+
|> Res_printer.printInterface ~width:100 ~comments:parseResult.comments
142+
|> print_endline
143+
| _
144+
->
145+
print_endline ("Invalid input for reprinting ReScript source. Must be a ReScript file: " ^ sourcefile);
146+
exit 2
147+
in
148+
Config.uncurried := uncurried;
149+
res
150+
109151
let usage = "Usage: bsc <options> <files>\nOptions are:"
110152

111153
let ppf = Format.err_formatter
@@ -390,6 +432,9 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =
390432
"-dsource", set Clflags.dump_source,
391433
"*internal* print source";
392434

435+
"-reprint-source", string_call reprint_source_file,
436+
"*internal* transform the target ReScript file using PPXes provided, and print the transformed ReScript code to stdout";
437+
393438
"-format", string_call format_file,
394439
"*internal* Format as Res syntax";
395440

0 commit comments

Comments
 (0)