Skip to content

Commit 291653f

Browse files
committed
remove unnecessary parens on function literals
1 parent 4a3a6fc commit 291653f

File tree

279 files changed

+2865
-2857
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

279 files changed

+2865
-2857
lines changed

jscomp/core/j.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ and expression_desc =
135135
env : Js_fun_env.t;
136136
return_unit : bool;
137137
async : bool;
138+
iife : bool;
138139
directive : string option;
139140
}
140141
| Str of { delim : delim; txt : string }

jscomp/core/js_dump.ml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ~need_paren ?directive cxt (f : P.t) ~fn_state
304+
and pp_function ~return_unit ~async ~is_method ~iife ?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
| [
@@ -426,8 +426,7 @@ and pp_function ~return_unit ~async ~is_method ~need_paren ?directive cxt (f : P
426426
P.string f (L.function_ ~async ~arrow);
427427
param_body ()
428428
| No_name _ ->
429-
(* see # 1692, add a paren for annoymous function for safety *)
430-
P.cond_paren_group f (need_paren) (fun _ ->
429+
P.cond_paren_group f (iife) (fun _ ->
431430
P.string f (L.function_ ~async ~arrow);
432431
param_body ())
433432
| Name_non_top x ->
@@ -525,10 +524,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
525524
let cxt = expression ~level:0 cxt f e1 in
526525
comma_sp f;
527526
expression ~level:0 cxt f e2)
528-
| Fun { is_method; params; body; env; return_unit; async; directive } ->
527+
| Fun { is_method; params; body; env; return_unit; async; iife; directive } ->
529528
(* TODO: dump for comments *)
530-
pp_function ?directive ~is_method ~return_unit ~async
531-
~need_paren:true
529+
pp_function ?directive ~is_method ~return_unit ~async ~iife
532530
~fn_state:default_fn_exp_state
533531
cxt f params body env
534532
(* TODO:
@@ -545,7 +543,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
545543
P.group f 0 (fun _ ->
546544
match (info, el) with
547545
| { arity = Full }, _ | _, [] ->
548-
let cxt = expression ~level:15 cxt f e in
546+
let cxt =
547+
P.cond_paren_group f
548+
(match e.expression_desc with Fun _ -> true | _ -> false)
549+
(fun () -> expression ~level:15 cxt f e )
550+
in
549551
P.paren_group f 0 (fun _ ->
550552
match el with
551553
| [
@@ -559,12 +561,12 @@ and expression_desc cxt ~(level : int) f x : cxt =
559561
env;
560562
return_unit;
561563
async;
564+
iife;
562565
directive;
563566
};
564567
};
565568
] ->
566-
pp_function ?directive ~is_method ~return_unit ~async
567-
~need_paren:false
569+
pp_function ?directive ~is_method ~return_unit ~async ~iife
568570
~fn_state:(No_name { single_arg = true })
569571
cxt f params body env
570572
| _ ->
@@ -968,7 +970,7 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
968970
match e.expression_desc with
969971
| Fun { is_method; params; body; env; return_unit; async; directive } ->
970972
pp_function ?directive ~is_method ~return_unit ~async
971-
~need_paren:false
973+
~iife:false
972974
~fn_state:(if top then Name_top name else Name_non_top name)
973975
cxt f params body env
974976
| _ ->
@@ -1176,10 +1178,10 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11761178
cxt
11771179
| Return e -> (
11781180
match e.expression_desc with
1179-
| Fun { is_method; params; body; env; return_unit; async; directive } ->
1181+
| Fun { is_method; params; body; env; return_unit; async; iife; directive } ->
11801182
let cxt =
11811183
pp_function ?directive ~return_unit ~is_method ~async
1182-
~need_paren:false
1184+
~iife
11831185
~fn_state:Is_return
11841186
cxt f params body env
11851187
in

jscomp/core/js_exp_make.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ let unit : t = { expression_desc = Undefined {is_unit = true}; comment = None }
207207
[Js_fun_env.empty] is a mutable state ..
208208
*)
209209

210-
let ocaml_fun ?comment ?immutable_mask ~return_unit ~async ~one_unit_arg ?directive params body : t =
210+
let ocaml_fun ?comment ?immutable_mask ?(iife = false) ?directive ~return_unit ~async ~one_unit_arg params body : t =
211211
let params = if one_unit_arg then [] else params in
212212
let len = List.length params in
213213
{
@@ -220,12 +220,13 @@ let ocaml_fun ?comment ?immutable_mask ~return_unit ~async ~one_unit_arg ?direct
220220
env = Js_fun_env.make ?immutable_mask len;
221221
return_unit;
222222
async;
223+
iife;
223224
directive;
224225
};
225226
comment;
226227
}
227228

228-
let method_ ?comment ?immutable_mask ~return_unit params body : t =
229+
let method_ ?comment ?immutable_mask ?(iife = false) ~return_unit params body : t =
229230
let len = List.length params in
230231
{
231232
expression_desc =
@@ -237,6 +238,7 @@ let method_ ?comment ?immutable_mask ~return_unit params body : t =
237238
env = Js_fun_env.make ?immutable_mask len;
238239
return_unit;
239240
async = false;
241+
iife;
240242
directive = None;
241243
};
242244
comment;
@@ -1303,6 +1305,7 @@ let of_block ?comment ?e block : t =
13031305
env = Js_fun_env.make 0;
13041306
return_unit;
13051307
async = false;
1308+
iife = true;
13061309
directive = None;
13071310
};
13081311
}

jscomp/core/js_exp_make.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,19 @@ val str : ?delim: J.delim -> ?comment: string -> string -> t
8888
val ocaml_fun :
8989
?comment:string ->
9090
?immutable_mask:bool array ->
91+
?iife:bool ->
92+
?directive:string ->
9193
return_unit:bool ->
9294
async:bool ->
9395
one_unit_arg:bool ->
94-
?directive:string ->
9596
J.ident list ->
9697
J.block ->
9798
t
9899

99100
val method_ :
100101
?comment:string ->
101102
?immutable_mask:bool array ->
103+
?iife:bool ->
102104
return_unit:bool ->
103105
J.ident list ->
104106
J.block ->

jscomp/gentype_tests/typescript-react-example/src/Hooks.res.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.

jscomp/gentype_tests/typescript-react-example/src/ImmutableArray.res.js

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

jscomp/gentype_tests/typescript-react-example/src/Records.res.js

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

jscomp/gentype_tests/typescript-react-example/src/TestPromise.res.js

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

0 commit comments

Comments
 (0)