Skip to content

Commit c08f7c2

Browse files
committed
fix rescript-lang#3068 part one
1 parent 7c0e0c1 commit c08f7c2

File tree

2 files changed

+192
-248
lines changed

2 files changed

+192
-248
lines changed

jscomp/core/js_dump.ml

+96-124
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,31 @@ let pp_direction f (direction : J.for_direction) =
191191
let return_sp f =
192192
P.string f L.return ; P.space f
193193

194+
let bool f b =
195+
P.string f (if b then L.true_ else L.false_)
196+
197+
let comma_sp f =
198+
comma f ; P.space f
199+
let comma_nl f =
200+
comma f ; P.newline f
201+
202+
let debugger_nl f =
203+
P.newline f ;
204+
P.string f L.debugger;
205+
semi f ;
206+
P.newline f
207+
208+
let break_nl f =
209+
P.string f L.break;
210+
P.space f ;
211+
semi f;
212+
P.newline f
213+
214+
let continue f s =
215+
P.string f L.continue;
216+
P.space f ;
217+
P.string f s;
218+
semi f
194219
let rec formal_parameter_list cxt (f : P.t) (is_method : bool) (l : Ident.t list) (env : Js_fun_env.t) =
195220
let offset = if is_method then 1 else 0 in
196221
let rec aux i cxt l =
@@ -199,8 +224,7 @@ let rec formal_parameter_list cxt (f : P.t) (is_method : bool) (l : Ident.t list
199224
| [id] -> ipp_ident cxt f id (Js_fun_env.get_unused env i)
200225
| id :: r ->
201226
let cxt = ipp_ident cxt f id (Js_fun_env.get_unused env i) in
202-
comma f;
203-
P.space f;
227+
comma_sp f;
204228
aux (i + 1) cxt r in
205229
match l with
206230
| [] -> cxt
@@ -332,10 +356,8 @@ and pp_function is_method
332356
P.space f ;
333357
ignore @@ P.brace_vgroup f 1 (fun _ ->
334358
let cxt =
335-
if not (Js_fun_env.get_unused env 0) then
336-
pp_var_assign_this cxt f this
337-
else
338-
cxt in
359+
if Js_fun_env.get_unused env 0 then cxt
360+
else pp_var_assign_this cxt f this in
339361
function_body cxt f b
340362
);
341363
else
@@ -387,29 +409,24 @@ and pp_function is_method
387409
P.string f L.function_;
388410
pp_paren_params inner_cxt f lexical;
389411
P.brace_vgroup f 0 (fun _ ->
390-
begin
391-
return_sp f;
392-
P.string f L.function_;
393-
P.space f ;
394-
(match name with
395-
| No_name -> ()
396-
| Name_non_top x | Name_top x -> ignore (Ext_pp_scope.ident inner_cxt f x));
397-
param_body ()
398-
end);
412+
return_sp f;
413+
P.string f L.function_;
414+
P.space f ;
415+
(match name with
416+
| No_name -> ()
417+
| Name_non_top x | Name_top x -> ignore (Ext_pp_scope.ident inner_cxt f x));
418+
param_body ());
399419
pp_paren_params inner_cxt f lexical;
400420
P.string f L.rparen;
401-
begin match name with
402-
| No_name -> () (* expression *)
403-
| _ -> semi f (* has binding, a statement *)
404-
end
405-
in
406-
(match name with
407-
| Name_top name | Name_non_top name when Ident_set.mem name lexical ->
408-
(*TODO: when calculating lexical we should not include itself *)
409-
let lexical = (Ident_set.remove name lexical) in
410-
handle lexical
411-
| _ -> handle lexical)
412-
in
421+
match name with
422+
| No_name -> () (* expression *)
423+
| _ -> semi f (* has binding, a statement *) in
424+
handle
425+
(match name with
426+
| Name_top name | Name_non_top name when Ident_set.mem name lexical ->
427+
(*TODO: when calculating lexical we should not include itself *)
428+
Ident_set.remove name lexical
429+
| _ -> lexical) in
413430
enclose lexical return;
414431
outer_cxt
415432

@@ -482,12 +499,11 @@ and expression_desc cxt (level:int) f x : cxt =
482499
| Var v ->
483500
vident cxt f v
484501
| Bool b ->
485-
(if b then P.string f L.true_ else P.string f L.false_ ) ; cxt
502+
bool f b ; cxt
486503
| Seq (e1, e2) ->
487504
P.cond_paren_group f (level > 0) 1 (fun () ->
488505
let cxt = expression 0 cxt f e1 in
489-
comma f;
490-
P.space f ;
506+
comma_sp f;
491507
expression 0 cxt f e2 )
492508
| Fun (method_, l, b, env) -> (* TODO: dump for comments *)
493509
pp_function method_ cxt f false l b env
@@ -533,8 +549,7 @@ and expression_desc cxt (level:int) f x : cxt =
533549
P.string f L.apply;
534550
P.paren_group f 1 (fun _ ->
535551
P.string f L.null;
536-
comma f ;
537-
P.space f ;
552+
comma_sp f ;
538553
expression 1 cxt f el
539554
)
540555
)
@@ -585,15 +600,14 @@ and expression_desc cxt (level:int) f x : cxt =
585600
P.paren_group f 1 (fun _ ->
586601
comma_strings f params
587602
);
588-
P.brace f (fun _ ->
589-
P.string f s);
603+
P.brace f (fun _ -> P.string f s);
590604
cxt
591605
| Raw_js_code (s,info) ->
592606
(match info with
593607
| Exp ->
594-
P.string f "(";
608+
P.string f L.lparen;
595609
P.string f s ;
596-
P.string f ")";
610+
P.string f L.rparen;
597611
cxt
598612
| Stmt ->
599613
P.newline f ;
@@ -611,15 +625,13 @@ and expression_desc cxt (level:int) f x : cxt =
611625
-> Int32.to_string v (* check , js convention with ocaml lexical convention *)
612626
| Uint i
613627
-> Format.asprintf "%lu" i
614-
| Nint i -> Nativeint.to_string i
615-
in
628+
| Nint i -> Nativeint.to_string i in
616629
let need_paren =
617630
if s.[0] = '-'
618631
then level > 13 (* Negative numbers may need to be parenthesized. *)
619632
else level = 15 (* Parenthesize as well when followed by a dot. *)
620633
&& s.[0] <> 'I' (* Infinity *)
621-
&& s.[0] <> 'N' (* NaN *)
622-
in
634+
&& s.[0] <> 'N' (* NaN *) in
623635
let action = fun _ -> P.string f s in
624636
(
625637
if need_paren
@@ -944,7 +956,7 @@ and expression_desc cxt (level:int) f x : cxt =
944956
[level 1] is correct, however
945957
to make nice indentation , force nested conditional to be parenthesized
946958
*)
947-
let cxt = (P.group f 1 @@ fun _ -> expression 3 cxt f e1) in
959+
let cxt = P.group f 1 (fun _ -> expression 3 cxt f e1) in
948960
(* let cxt = (P.group f 1 @@ fun _ -> expression 1 cxt f e1) in *)
949961
P.space f;
950962
P.string f L.colon;
@@ -974,43 +986,25 @@ and expression_desc cxt (level:int) f x : cxt =
974986
P.paren_group f 1 action
975987
else action ()
976988

977-
and property_name cxt f (s : J.property_name) : unit =
978-
Js_dump_property.property_key f s
979-
980-
981-
and property_name_and_value_list cxt f l : cxt =
982-
match l with
983-
| [] -> cxt
984-
| [(pn, e)] ->
985-
property_name cxt f pn ;
989+
and property_name_and_value_list cxt f l =
990+
iter_lst cxt f l (fun cxt f (pn,e) ->
991+
Js_dump_property.property_key f pn ;
986992
P.string f L.colon;
987993
P.space f;
988994
expression 1 cxt f e
989-
| (pn, e) :: r ->
990-
property_name cxt f pn ;
991-
P.string f L.colon;
992-
P.space f;
993-
let cxt = expression 1 cxt f e in
994-
comma f;
995-
P.newline f;
996-
property_name_and_value_list cxt f r
997-
995+
) comma_nl
998996
and array_element_list cxt f el : cxt =
999-
iter_lst cxt f el (fun cxt f e -> expression 1 cxt f e )
1000-
(fun f -> comma f; P.newline f)
997+
iter_lst cxt f el (fun cxt f e -> expression 1 cxt f e ) comma_nl
1001998

1002999
and arguments cxt f l : cxt =
1003-
iter_lst cxt f l (fun cxt f e -> expression 1 cxt f e)
1004-
(fun f -> comma f; P.space f)
1000+
iter_lst cxt f l (fun cxt f e -> expression 1 cxt f e) comma_sp
10051001

1006-
10071002
and variable_declaration top cxt f
10081003
(variable : J.variable_declaration) : cxt =
10091004
(* TODO: print [const/var] for different backends *)
10101005
match variable with
10111006
| {ident = i; value = None; ident_info ; _} ->
1012-
if ident_info.used_stats = Dead_pure
1013-
then cxt
1007+
if ident_info.used_stats = Dead_pure then cxt
10141008
else pp_var_declare cxt f i
10151009
| { ident = name; value = Some e; ident_info = {used_stats; _}} ->
10161010
match used_stats with
@@ -1072,8 +1066,8 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
10721066
let cxt =
10731067
(
10741068
if exp_need_paren e
1075-
then (P.paren_group f 1)
1076-
else (P.group f 0)
1069+
then P.paren_group f 1
1070+
else P.group f 0
10771071
) (fun _ -> expression 0 cxt f e ) in
10781072
semi f;
10791073
cxt
@@ -1088,7 +1082,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
10881082
| If (e, s1, s2) -> (* TODO: always brace those statements *)
10891083
P.string f L.if_;
10901084
P.space f;
1091-
let cxt = P.paren_group f 1 @@ fun _ -> expression 0 cxt f e in
1085+
let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in
10921086
P.space f;
10931087
let cxt = block cxt f s1 in
10941088
(match s2 with
@@ -1119,14 +1113,14 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11191113
match e.expression_desc with
11201114
| Number (Int {i = 1l}) ->
11211115
P.string f L.while_;
1122-
P.string f "(";
1116+
P.string f L.lparen;
11231117
P.string f L.true_;
1124-
P.string f ")";
1118+
P.string f L.rparen;
11251119
P.space f ;
11261120
cxt
11271121
| _ ->
11281122
P.string f L.while_;
1129-
let cxt = P.paren_group f 1 @@ fun _ -> expression 0 cxt f e in
1123+
let cxt = P.paren_group f 1 (fun _ -> expression 0 cxt f e) in
11301124
P.space f ;
11311125
cxt
11321126
in
@@ -1201,37 +1195,19 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
12011195
let lexical = Ident_set.elements lexical in
12021196
P.vgroup f 0
12031197
(fun _ ->
1204-
1205-
P.string f "(function" ;
1198+
P.string f L.lparen;
1199+
P.string f L.function_;
12061200
pp_paren_params inner_cxt f lexical;
12071201
let cxt = P.brace_vgroup f 0 (fun _ -> action inner_cxt) in
12081202
pp_paren_params inner_cxt f lexical;
1209-
P.string f ")";
1203+
P.string f L.rparen;
12101204
semi f;
12111205
cxt
12121206
)
1213-
| Continue s ->
1214-
P.string f L.continue;
1215-
P.space f ;
1216-
P.string f s;
1217-
semi f;
1218-
(* P.newline f; *)
1219-
(* #2642 *)
1220-
cxt
1221-
| Debugger
1222-
->
1223-
P.newline f ;
1224-
P.string f L.debugger;
1225-
semi f ;
1226-
P.newline f;
1227-
cxt
1228-
| Break
1229-
->
1230-
P.string f L.break;
1231-
P.space f ;
1232-
semi f;
1233-
P.newline f;
1234-
cxt
1207+
| Continue s -> continue f s ; cxt
1208+
(* P.newline f; #2642 *)
1209+
| Debugger -> debugger_nl f ; cxt
1210+
| Break -> break_nl f; cxt
12351211

12361212
| Return {return_value = e} ->
12371213
begin match e with
@@ -1269,27 +1245,24 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
12691245
| String_switch (e, cc, def) ->
12701246
P.string f L.switch;
12711247
P.space f;
1272-
let cxt = P.paren_group f 1 @@ fun _ -> expression 0 cxt f e
1273-
in
1248+
let cxt = P.paren_group f 1 @@ fun _ -> expression 0 cxt f e in
12741249
P.space f;
1275-
P.brace_vgroup f 1 @@ fun _ ->
1276-
let cxt = loop_case_clauses cxt f (fun f i -> Js_dump_string.pp_string f i ) cc in
1277-
(match def with
1278-
| None -> cxt
1279-
| Some def ->
1280-
P.group f 1 @@ fun _ ->
1281-
P.string f L.default;
1282-
P.string f L.colon;
1283-
P.newline f;
1284-
statement_list false cxt f def )
1285-
1250+
P.brace_vgroup f 1 (fun _ ->
1251+
let cxt = loop_case_clauses cxt f (fun f i -> Js_dump_string.pp_string f i ) cc in
1252+
match def with
1253+
| None -> cxt
1254+
| Some def ->
1255+
P.group f 1 (fun _ ->
1256+
P.string f L.default;
1257+
P.string f L.colon;
1258+
P.newline f;
1259+
statement_list false cxt f def ))
12861260
| Throw e ->
12871261
P.string f L.throw;
12881262
P.space f ;
1289-
P.group f throw_indent @@ fun _ ->
1290-
1291-
let cxt = expression 0 cxt f e in
1292-
semi f ; cxt
1263+
P.group f throw_indent (fun _ ->
1264+
let cxt = expression 0 cxt f e in
1265+
semi f ; cxt)
12931266

12941267
(* There must be a space between the return and its
12951268
argument. A line return would not work *)
@@ -1307,16 +1280,15 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
13071280
P.string f "catch (";
13081281
let cxt = Ext_pp_scope.ident cxt f i in
13091282
P.string f ")";
1310-
block cxt f b
1311-
in
1312-
begin match fin with
1313-
| None -> cxt
1314-
| Some b ->
1315-
P.group f 1 @@ fun _ ->
1316-
P.string f L.finally;
1317-
P.space f;
1318-
block cxt f b
1319-
end
1283+
block cxt f b in
1284+
match fin with
1285+
| None -> cxt
1286+
| Some b ->
1287+
P.group f 1 (fun _ ->
1288+
P.string f L.finally;
1289+
P.space f;
1290+
block cxt f b)
1291+
13201292
and function_body cxt f b =
13211293
match b with
13221294
| [] -> cxt

0 commit comments

Comments
 (0)