Skip to content

Commit 4071859

Browse files
committed
remove unnecessary parens
1 parent 3a34381 commit 4071859

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

jscomp/core/js_dump.ml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ let raw_snippet_exp_simple_enough (s : string) =
163163
let exp_need_paren (e : J.expression) =
164164
match e.expression_desc with
165165
(* | Caml_uninitialized_obj _ *)
166-
| Call ({ expression_desc = Fun _ | Raw_js_code _ }, _, _) -> true
166+
| Call ({ expression_desc = Raw_js_code _ }, _, _) -> true
167167
| Raw_js_code { code_info = Exp _ }
168168
| Fun _
169169
| Caml_block
@@ -301,7 +301,7 @@ let rec try_optimize_curry cxt f len function_id =
301301
Curry_gen.pp_optimize_curry f len;
302302
P.paren_group f 1 (fun _ -> expression ~level:1 cxt f function_id)
303303

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
305305
(l : Ident.t list) (b : J.block) (env : Js_fun_env.t) : cxt =
306306
match b with
307307
| [
@@ -410,7 +410,7 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) ~fn_stat
410410
param_body ()
411411
| No_name { single_arg } ->
412412
(* 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 _ ->
414414
P.string f (L.function_ ~async ~arrow);
415415
param_body ())
416416
| Name_non_top x ->
@@ -510,8 +510,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
510510
expression ~level:0 cxt f e2)
511511
| Fun { is_method; params; body; env; return_unit; async; directive } ->
512512
(* 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
515517
(* TODO:
516518
when [e] is [Js_raw_code] with arity
517519
print it in a more precise way
@@ -544,9 +546,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
544546
};
545547
};
546548
] ->
547-
pp_function ?directive ~is_method ~return_unit ~async cxt f
549+
pp_function ?directive ~is_method ~return_unit ~async
550+
~need_paren:false
548551
~fn_state:(No_name { single_arg = true })
549-
params body env
552+
cxt f params body env
550553
| _ ->
551554
let el = match el with
552555
| [e] when e.expression_desc = Undefined {is_unit = true} ->
@@ -947,9 +950,10 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
947950
| _ -> (
948951
match e.expression_desc with
949952
| 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
951955
~fn_state:(if top then Name_top name else Name_non_top name)
952-
params body env
956+
cxt f params body env
953957
| _ ->
954958
let cxt = pp_var_assign cxt f name in
955959
let cxt = expression ~level:1 cxt f e in
@@ -1157,8 +1161,10 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11571161
match e.expression_desc with
11581162
| Fun { is_method; params; body; env; return_unit; async; directive } ->
11591163
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
11621168
in
11631169
semi f;
11641170
cxt

jscomp/test/async_inline.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/gpr_1692_test.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)