@@ -163,7 +163,7 @@ let raw_snippet_exp_simple_enough (s : string) =
163
163
let exp_need_paren (e : J.expression ) =
164
164
match e.expression_desc with
165
165
(* | Caml_uninitialized_obj _ * )
166
- | Call ({ expression_desc = Fun _ | Raw_js_code _ } , _ , _ ) -> true
166
+ | Call ({ expression_desc = Raw_js_code _ } , _ , _ ) -> true
167
167
| Raw_js_code { code_info = Exp _ }
168
168
| Fun _
169
169
| Caml_block
@@ -360,6 +360,12 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) ~fn_stat
360
360
(* the context will be continued after this function *)
361
361
let outer_cxt = Ext_pp_scope. merge cxt set_env in
362
362
363
+ (* whether the function output can use arrow syntax *)
364
+ let arrow = match fn_state with
365
+ | Name_top _ -> false
366
+ | _ -> not is_method
367
+ in
368
+
363
369
(* the context used to be printed inside this function
364
370
365
371
when printing a function,
@@ -385,35 +391,50 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) ~fn_stat
385
391
function_body ?directive ~return_unit cxt f b))
386
392
else
387
393
let cxt =
388
- P. paren_group f 1 (fun _ -> formal_parameter_list inner_cxt f l)
394
+ match l with
395
+ | [ single ] when arrow ->
396
+ Ext_pp_scope. ident inner_cxt f single
397
+ | l ->
398
+ P. paren_group f 1 (fun _ -> formal_parameter_list inner_cxt f l)
389
399
in
390
400
P. space f;
391
- P. brace_vgroup f 1 (fun _ -> function_body ?directive ~return_unit cxt f b)
401
+ if arrow then (
402
+ P. string f (L. arrow);
403
+ P. space f;
404
+ );
405
+ match b with
406
+ | [ { statement_desc = Return { expression_desc = Undefined _ } } ]
407
+ when arrow
408
+ ->
409
+ P. string f " {" ;
410
+ P. string f " }" ;
411
+
412
+ | [ { statement_desc = Return e } ] | [ { statement_desc = Exp e } ]
413
+ when arrow && directive == None
414
+ -> (if exp_need_paren e then P. paren_group f 0 else P. group f 0 )
415
+ (fun _ -> ignore (expression ~level: 0 cxt f e))
416
+
417
+ | _ ->
418
+ P. brace_vgroup f 1 (fun _ -> function_body ?directive ~return_unit cxt f b)
392
419
in
393
420
let enclose () =
394
421
let handle () =
395
422
(
396
423
match fn_state with
397
424
| Is_return ->
398
425
return_sp f;
399
- P. string f (L. function_async ~async );
400
- P. space f;
426
+ P. string f (L. function_ ~async ~arrow );
427
+ param_body ()
428
+ | No_name _ ->
429
+ P. string f (L. function_ ~async ~arrow );
401
430
param_body ()
402
- | No_name { single_arg } ->
403
- (* see # 1692, add a paren for annoymous function for safety *)
404
- P. cond_paren_group f (not single_arg) (fun _ ->
405
- P. string f (L. function_async ~async );
406
- P. space f;
407
- param_body () )
408
431
| Name_non_top x ->
409
432
ignore (pp_var_assign inner_cxt f x : cxt );
410
- P. string f (L. function_async ~async );
411
- P. space f;
433
+ P. string f (L. function_ ~async ~arrow );
412
434
param_body () ;
413
435
semi f
414
436
| Name_top x ->
415
- P. string f (L. function_async ~async );
416
- P. space f;
437
+ P. string f (L. function_ ~async ~arrow );
417
438
ignore (Ext_pp_scope. ident inner_cxt f x : cxt );
418
439
param_body () )
419
440
in
@@ -504,8 +525,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
504
525
expression ~level: 0 cxt f e2)
505
526
| Fun { is_method; params; body; env; return_unit; async; directive } ->
506
527
(* TODO: dump for comments *)
507
- pp_function ?directive ~is_method cxt f ~fn_state: default_fn_exp_state params body
508
- env ~return_unit ~async
528
+ pp_function ?directive ~is_method ~return_unit ~async
529
+ ~fn_state: default_fn_exp_state
530
+ cxt f params body env
509
531
(* TODO:
510
532
when [e] is [Js_raw_code] with arity
511
533
print it in a more precise way
@@ -520,7 +542,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
520
542
P. group f 0 (fun _ ->
521
543
match (info, el) with
522
544
| { arity = Full } , _ | _ , [] ->
523
- let cxt = expression ~level: 15 cxt f e in
545
+ let cxt =
546
+ P. cond_paren_group f
547
+ (match e.expression_desc with Fun _ -> true | _ -> false )
548
+ (fun () -> expression ~level: 15 cxt f e )
549
+ in
524
550
P. paren_group f 0 (fun _ ->
525
551
match el with
526
552
| [
@@ -538,9 +564,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
538
564
};
539
565
};
540
566
] ->
541
- pp_function ?directive ~is_method ~return_unit ~async cxt f
567
+ pp_function ?directive ~is_method ~return_unit ~async
542
568
~fn_state: (No_name { single_arg = true })
543
- params body env
569
+ cxt f params body env
544
570
| _ ->
545
571
let el = match el with
546
572
| [e] when e.expression_desc = Undefined {is_unit = true } ->
@@ -941,9 +967,9 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
941
967
| _ -> (
942
968
match e.expression_desc with
943
969
| Fun { is_method; params; body; env; return_unit; async; directive } ->
944
- pp_function ?directive ~is_method cxt f ~return_unit ~async
970
+ pp_function ?directive ~is_method ~return_unit ~async
945
971
~fn_state: (if top then Name_top name else Name_non_top name)
946
- params body env
972
+ cxt f params body env
947
973
| _ ->
948
974
let cxt = pp_var_assign cxt f name in
949
975
let cxt = expression ~level: 1 cxt f e in
@@ -1151,8 +1177,9 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
1151
1177
match e.expression_desc with
1152
1178
| Fun { is_method; params; body; env; return_unit; async; directive } ->
1153
1179
let cxt =
1154
- pp_function ?directive ~return_unit ~is_method ~async cxt f ~fn_state: Is_return
1155
- params body env
1180
+ pp_function ?directive ~return_unit ~is_method ~async
1181
+ ~fn_state: Is_return
1182
+ cxt f params body env
1156
1183
in
1157
1184
semi f;
1158
1185
cxt
0 commit comments