@@ -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
@@ -301,7 +301,7 @@ let rec try_optimize_curry cxt f len function_id =
301
301
Curry_gen. pp_optimize_curry f len;
302
302
P. paren_group f 1 (fun _ -> expression ~level: 1 cxt f function_id)
303
303
304
- and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t ) ~fn_state
304
+ and pp_function ~return_unit ~async ~is_method ~ need_paren ?directive cxt (f : P.t ) ~fn_state
305
305
(l : Ident.t list ) (b : J.block ) (env : Js_fun_env.t ) : cxt =
306
306
match b with
307
307
| [
@@ -410,7 +410,7 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) ~fn_stat
410
410
param_body ()
411
411
| No_name { single_arg } ->
412
412
(* see # 1692, add a paren for annoymous function for safety *)
413
- P. cond_paren_group f (not single_arg) (fun _ ->
413
+ P. cond_paren_group f (need_paren && not single_arg) (fun _ ->
414
414
P. string f (L. function_ ~async ~arrow );
415
415
param_body () )
416
416
| Name_non_top x ->
@@ -510,8 +510,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
510
510
expression ~level: 0 cxt f e2)
511
511
| Fun { is_method; params; body; env; return_unit; async; directive } ->
512
512
(* TODO: dump for comments *)
513
- pp_function ?directive ~is_method cxt f ~fn_state: default_fn_exp_state params body
514
- env ~return_unit ~async
513
+ pp_function ?directive ~is_method ~return_unit ~async
514
+ ~need_paren: true
515
+ ~fn_state: default_fn_exp_state
516
+ cxt f params body env
515
517
(* TODO:
516
518
when [e] is [Js_raw_code] with arity
517
519
print it in a more precise way
@@ -544,9 +546,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
544
546
};
545
547
};
546
548
] ->
547
- pp_function ?directive ~is_method ~return_unit ~async cxt f
549
+ pp_function ?directive ~is_method ~return_unit ~async
550
+ ~need_paren: false
548
551
~fn_state: (No_name { single_arg = true })
549
- params body env
552
+ cxt f params body env
550
553
| _ ->
551
554
let el = match el with
552
555
| [e] when e.expression_desc = Undefined {is_unit = true } ->
@@ -947,9 +950,10 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
947
950
| _ -> (
948
951
match e.expression_desc with
949
952
| Fun { is_method; params; body; env; return_unit; async; directive } ->
950
- pp_function ?directive ~is_method cxt f ~return_unit ~async
953
+ pp_function ?directive ~is_method ~return_unit ~async
954
+ ~need_paren: false
951
955
~fn_state: (if top then Name_top name else Name_non_top name)
952
- params body env
956
+ cxt f params body env
953
957
| _ ->
954
958
let cxt = pp_var_assign cxt f name in
955
959
let cxt = expression ~level: 1 cxt f e in
@@ -1157,8 +1161,10 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
1157
1161
match e.expression_desc with
1158
1162
| Fun { is_method; params; body; env; return_unit; async; directive } ->
1159
1163
let cxt =
1160
- pp_function ?directive ~return_unit ~is_method ~async cxt f ~fn_state: Is_return
1161
- params body env
1164
+ pp_function ?directive ~return_unit ~is_method ~async
1165
+ ~need_paren: false
1166
+ ~fn_state: Is_return
1167
+ cxt f params body env
1162
1168
in
1163
1169
semi f;
1164
1170
cxt
0 commit comments