Skip to content

Commit a98bc28

Browse files
authored
use arrow syntax for anonymous functions (#6945)
* emit arrow syntax for non top level functions * remove unnecessary parens * fix cond * skip paren for arrow function with single param * skip return clause where possible * remove unnecessary parens on function literals * add changelog * remove unnecessary parens of iife
1 parent 5e6c891 commit a98bc28

File tree

363 files changed

+7461
-10793
lines changed

Some content is hidden

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

363 files changed

+7461
-10793
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Removed empty line at the end of `switch` statement
2424
- Removed empty `default` case from `switch` statement in the generated code
2525
- Optimised the Type Extension runtime code and removed trailing `/1` from `RE_EXN_ID` https://github.com/rescript-lang/rescript-compiler/pull/6958
26+
- Compact output for anonymous functions. https://github.com/rescript-lang/rescript-compiler/pull/6945
2627

2728
#### :bug: Bug Fix
2829
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949

jscomp/build_tests/react_ppx/src/gpr_3987_test.bs.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/core/js_dump.ml

Lines changed: 51 additions & 24 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
@@ -360,6 +360,12 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) ~fn_stat
360360
(* the context will be continued after this function *)
361361
let outer_cxt = Ext_pp_scope.merge cxt set_env in
362362

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+
363369
(* the context used to be printed inside this function
364370
365371
when printing a function,
@@ -385,35 +391,50 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) ~fn_stat
385391
function_body ?directive ~return_unit cxt f b))
386392
else
387393
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)
389399
in
390400
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)
392419
in
393420
let enclose () =
394421
let handle () =
395422
(
396423
match fn_state with
397424
| Is_return ->
398425
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);
401430
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 ())
408431
| Name_non_top x ->
409432
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);
412434
param_body ();
413435
semi f
414436
| Name_top x ->
415-
P.string f (L.function_async ~async);
416-
P.space f;
437+
P.string f (L.function_ ~async ~arrow);
417438
ignore (Ext_pp_scope.ident inner_cxt f x : cxt);
418439
param_body ())
419440
in
@@ -504,8 +525,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
504525
expression ~level:0 cxt f e2)
505526
| Fun { is_method; params; body; env; return_unit; async; directive } ->
506527
(* 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
509531
(* TODO:
510532
when [e] is [Js_raw_code] with arity
511533
print it in a more precise way
@@ -520,7 +542,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
520542
P.group f 0 (fun _ ->
521543
match (info, el) with
522544
| { 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
524550
P.paren_group f 0 (fun _ ->
525551
match el with
526552
| [
@@ -538,9 +564,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
538564
};
539565
};
540566
] ->
541-
pp_function ?directive ~is_method ~return_unit ~async cxt f
567+
pp_function ?directive ~is_method ~return_unit ~async
542568
~fn_state:(No_name { single_arg = true })
543-
params body env
569+
cxt f params body env
544570
| _ ->
545571
let el = match el with
546572
| [e] when e.expression_desc = Undefined {is_unit = true} ->
@@ -941,9 +967,9 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
941967
| _ -> (
942968
match e.expression_desc with
943969
| 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
945971
~fn_state:(if top then Name_top name else Name_non_top name)
946-
params body env
972+
cxt f params body env
947973
| _ ->
948974
let cxt = pp_var_assign cxt f name in
949975
let cxt = expression ~level:1 cxt f e in
@@ -1151,8 +1177,9 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11511177
match e.expression_desc with
11521178
| Fun { is_method; params; body; env; return_unit; async; directive } ->
11531179
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
11561183
in
11571184
semi f;
11581185
cxt

jscomp/core/js_dump_lit.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
2625
let await = "await"
2726

28-
let function_ = "function"
27+
let function_ ~async ~arrow =
28+
match (async, arrow) with
29+
| (true, true) -> "async "
30+
| (false, true) -> ""
31+
| (true, false) -> "async function "
32+
| (false, false) -> "function "
2933

30-
let function_async ~async = if async then "async function" else "function"
34+
let arrow = "=>"
3135

3236
let let_ = "let"
3337

jscomp/core/js_exp_make.ml

Lines changed: 1 addition & 1 deletion
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 ?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
{

jscomp/core/js_exp_make.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ val str : ?delim: J.delim -> ?comment: string -> string -> t
8888
val ocaml_fun :
8989
?comment:string ->
9090
?immutable_mask:bool array ->
91+
?directive:string ->
9192
return_unit:bool ->
9293
async:bool ->
9394
one_unit_arg:bool ->
94-
?directive:string ->
9595
J.ident list ->
9696
J.block ->
9797
t

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

Lines changed: 1 addition & 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/Hooks.res.js

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

0 commit comments

Comments
 (0)