Skip to content

use arrow syntax for anonymous functions #6945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Removed empty line at the end of `switch` statement
- Removed empty `default` case from `switch` statement in the generated code
- Optimised the Type Extension runtime code and removed trailing `/1` from `RE_EXN_ID` https://github.com/rescript-lang/rescript-compiler/pull/6958
- Compact output for anonymous functions. https://github.com/rescript-lang/rescript-compiler/pull/6945

#### :bug: Bug Fix
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949
Expand Down
6 changes: 3 additions & 3 deletions jscomp/build_tests/react_ppx/src/gpr_3987_test.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 51 additions & 24 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ let raw_snippet_exp_simple_enough (s : string) =
let exp_need_paren (e : J.expression) =
match e.expression_desc with
(* | Caml_uninitialized_obj _ *)
| Call ({ expression_desc = Fun _ | Raw_js_code _ }, _, _) -> true
| Call ({ expression_desc = Raw_js_code _ }, _, _) -> true
| Raw_js_code { code_info = Exp _ }
| Fun _
| Caml_block
Expand Down Expand Up @@ -360,6 +360,12 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) ~fn_stat
(* the context will be continued after this function *)
let outer_cxt = Ext_pp_scope.merge cxt set_env in

(* whether the function output can use arrow syntax *)
let arrow = match fn_state with
| Name_top _ -> false
| _ -> not is_method
in

(* the context used to be printed inside this function

when printing a function,
Expand All @@ -385,35 +391,50 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) ~fn_stat
function_body ?directive ~return_unit cxt f b))
else
let cxt =
P.paren_group f 1 (fun _ -> formal_parameter_list inner_cxt f l)
match l with
| [ single ] when arrow ->
Ext_pp_scope.ident inner_cxt f single
| l ->
P.paren_group f 1 (fun _ -> formal_parameter_list inner_cxt f l)
in
P.space f;
P.brace_vgroup f 1 (fun _ -> function_body ?directive ~return_unit cxt f b)
if arrow then (
P.string f (L.arrow);
P.space f;
);
match b with
| [ { statement_desc = Return { expression_desc = Undefined _ } } ]
when arrow
->
P.string f "{";
P.string f "}";

| [ { statement_desc = Return e } ] | [ { statement_desc = Exp e } ]
when arrow && directive == None
-> (if exp_need_paren e then P.paren_group f 0 else P.group f 0)
(fun _ -> ignore (expression ~level:0 cxt f e))

| _ ->
P.brace_vgroup f 1 (fun _ -> function_body ?directive ~return_unit cxt f b)
in
let enclose () =
let handle () =
(
match fn_state with
| Is_return ->
return_sp f;
P.string f (L.function_async ~async);
P.space f;
P.string f (L.function_ ~async ~arrow);
param_body ()
| No_name _ ->
P.string f (L.function_ ~async ~arrow);
param_body ()
| No_name { single_arg } ->
(* see # 1692, add a paren for annoymous function for safety *)
P.cond_paren_group f (not single_arg) (fun _ ->
P.string f (L.function_async ~async);
P.space f;
param_body ())
| Name_non_top x ->
ignore (pp_var_assign inner_cxt f x : cxt);
P.string f (L.function_async ~async);
P.space f;
P.string f (L.function_ ~async ~arrow);
param_body ();
semi f
| Name_top x ->
P.string f (L.function_async ~async);
P.space f;
P.string f (L.function_ ~async ~arrow);
ignore (Ext_pp_scope.ident inner_cxt f x : cxt);
param_body ())
in
Expand Down Expand Up @@ -504,8 +525,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
expression ~level:0 cxt f e2)
| Fun { is_method; params; body; env; return_unit; async; directive } ->
(* TODO: dump for comments *)
pp_function ?directive ~is_method cxt f ~fn_state:default_fn_exp_state params body
env ~return_unit ~async
pp_function ?directive ~is_method ~return_unit ~async
~fn_state:default_fn_exp_state
cxt f params body env
(* TODO:
when [e] is [Js_raw_code] with arity
print it in a more precise way
Expand All @@ -520,7 +542,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
P.group f 0 (fun _ ->
match (info, el) with
| { arity = Full }, _ | _, [] ->
let cxt = expression ~level:15 cxt f e in
let cxt =
P.cond_paren_group f
(match e.expression_desc with Fun _ -> true | _ -> false)
(fun () -> expression ~level:15 cxt f e )
in
P.paren_group f 0 (fun _ ->
match el with
| [
Expand All @@ -538,9 +564,9 @@ and expression_desc cxt ~(level : int) f x : cxt =
};
};
] ->
pp_function ?directive ~is_method ~return_unit ~async cxt f
pp_function ?directive ~is_method ~return_unit ~async
~fn_state:(No_name { single_arg = true })
params body env
cxt f params body env
| _ ->
let el = match el with
| [e] when e.expression_desc = Undefined {is_unit = true} ->
Expand Down Expand Up @@ -941,9 +967,9 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
| _ -> (
match e.expression_desc with
| Fun { is_method; params; body; env; return_unit; async; directive } ->
pp_function ?directive ~is_method cxt f ~return_unit ~async
pp_function ?directive ~is_method ~return_unit ~async
~fn_state:(if top then Name_top name else Name_non_top name)
params body env
cxt f params body env
| _ ->
let cxt = pp_var_assign cxt f name in
let cxt = expression ~level:1 cxt f e in
Expand Down Expand Up @@ -1151,8 +1177,9 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
match e.expression_desc with
| Fun { is_method; params; body; env; return_unit; async; directive } ->
let cxt =
pp_function ?directive ~return_unit ~is_method ~async cxt f ~fn_state:Is_return
params body env
pp_function ?directive ~return_unit ~is_method ~async
~fn_state:Is_return
cxt f params body env
in
semi f;
cxt
Expand Down
10 changes: 7 additions & 3 deletions jscomp/core/js_dump_lit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)


let await = "await"

let function_ = "function"
let function_ ~async ~arrow =
match (async, arrow) with
| (true, true) -> "async "
| (false, true) -> ""
| (true, false) -> "async function "
| (false, false) -> "function "

let function_async ~async = if async then "async function" else "function"
let arrow = "=>"

let let_ = "let"

Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ let unit : t = { expression_desc = Undefined {is_unit = true}; comment = None }
[Js_fun_env.empty] is a mutable state ..
*)

let ocaml_fun ?comment ?immutable_mask ~return_unit ~async ~one_unit_arg ?directive params body : t =
let ocaml_fun ?comment ?immutable_mask ?directive ~return_unit ~async ~one_unit_arg params body : t =
let params = if one_unit_arg then [] else params in
let len = List.length params in
{
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ val str : ?delim: J.delim -> ?comment: string -> string -> t
val ocaml_fun :
?comment:string ->
?immutable_mask:bool array ->
?directive:string ->
return_unit:bool ->
async:bool ->
one_unit_arg:bool ->
?directive:string ->
J.ident list ->
J.block ->
t
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 9 additions & 23 deletions jscomp/gentype_tests/typescript-react-example/src/Hooks.res.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading