Skip to content

Commit

Permalink
Compiler: js-parser: preserve consise body
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Dec 17, 2023
1 parent 128cf49 commit a2ae471
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion compiler/lib/javascript.ml
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ and expression =
| EVar of ident
| EFun of ident option * function_declaration
| EClass of ident option * class_declaration
| EArrow of function_declaration * arrow_info
| EArrow of function_declaration * bool * arrow_info
| EStr of Utf8_string.t
| ETemplate of template
| EArr of array_litteral
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib/javascript.mli
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ and expression =
| EVar of ident
| EFun of ident option * function_declaration
| EClass of ident option * class_declaration
| EArrow of function_declaration * arrow_info
| EArrow of function_declaration * bool * arrow_info
| EStr of Utf8_string.t
(* A UTF-8 encoded string that may contain escape sequences. *)
| ETemplate of template
Expand Down
8 changes: 4 additions & 4 deletions compiler/lib/js_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ struct
PP.end_group f)
| EFun (i, decl) -> function_declaration' f i decl
| EClass (i, cl_decl) -> class_declaration f i cl_decl
| EArrow ((k, p, b, pc), _) ->
| EArrow ((k, p, b, pc), consise, _) ->
if Prec.(l > AssignementExpression)
then (
PP.start_group f 1;
Expand All @@ -461,15 +461,15 @@ struct
PP.string f ")=>";
PP.end_group f);
PP.end_group f;
(match b with
| [ (Return_statement (Some e), loc) ] ->
(match b, consise with
| [ (Return_statement (Some e), loc) ], true ->
(* Should not starts with '{' *)
PP.start_group f 1;
PP.break1 f;
output_debug_info f loc;
parenthesized_expression ~obj:true AssignementExpression f e;
PP.end_group f
| l ->
| l, _ ->
let b =
match l with
| [ (Block l, _) ] -> l
Expand Down
17 changes: 11 additions & 6 deletions compiler/lib/js_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -1082,20 +1082,25 @@ encaps:
(* TODO conflict with as then in indent_keyword_bis *)
arrow_function:
| i=ident T_ARROW b=arrow_body
{ EArrow (({async = false; generator = false}, list [param' i],b, p $symbolstartpos), AUnknown) }
{ let b,consise = b in
EArrow (({async = false; generator = false}, list [param' i],b, p $symbolstartpos), consise, AUnknown) }
| T_LPAREN_ARROW a=formal_parameter_list_opt ")" T_ARROW b=arrow_body
{ EArrow (({async = false; generator = false}, a,b, p $symbolstartpos), AUnknown) }
{ let b,consise = b in
EArrow (({async = false; generator = false}, a,b, p $symbolstartpos), consise, AUnknown) }

async_arrow_function:
| T_ASYNC i=ident T_ARROW b=arrow_body { EArrow(({async = true; generator = false}, list [param' i],b, p $symbolstartpos), AUnknown) }
| T_ASYNC i=ident T_ARROW b=arrow_body {
let b,consise = b in
EArrow(({async = true; generator = false}, list [param' i],b, p $symbolstartpos), consise, AUnknown) }
| T_ASYNC T_LPAREN_ARROW a=formal_parameter_list_opt ")" T_ARROW b=arrow_body
{ EArrow (({async = true; generator = false}, a,b, p $symbolstartpos), AUnknown) }
{ let b,consise = b in
EArrow (({async = true; generator = false}, a,b, p $symbolstartpos), consise, AUnknown) }


(* was called consise body in spec *)
arrow_body:
| "{" b=function_body "}" { b }
| e=assignment_expr_for_consise_body { [(Return_statement (Some e), p $symbolstartpos)] }
| "{" b=function_body "}" { b, false }
| e=assignment_expr_for_consise_body { [(Return_statement (Some e), p $symbolstartpos)], true }

(*----------------------------*)
(* no in *)
Expand Down
18 changes: 9 additions & 9 deletions compiler/lib/js_traverse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class map : mapper =
let idopt = Option.map ~f:m#ident idopt in
EFun (idopt, m#fun_decl fun_decl)
| EClass (id, cl_decl) -> EClass (Option.map ~f:m#ident id, m#class_decl cl_decl)
| EArrow (fun_decl, x) -> EArrow (m#fun_decl fun_decl, x)
| EArrow (fun_decl, consise, x) -> EArrow (m#fun_decl fun_decl, consise, x)
| EArr l ->
EArr
(List.map l ~f:(function
Expand Down Expand Up @@ -671,7 +671,7 @@ class iter : iterator =
| EClass (i, cl_decl) ->
Option.iter ~f:m#ident i;
m#class_decl cl_decl
| EArrow (fun_decl, _) -> m#fun_decl fun_decl
| EArrow (fun_decl, _, _) -> m#fun_decl fun_decl
| EArr l ->
List.iter l ~f:(function
| ElementHole -> ()
Expand Down Expand Up @@ -1710,9 +1710,9 @@ let use_fun_context l =

method expression x =
match x with
| EArrow (_, ANo_fun_context) -> ()
| EArrow (_, AUse_parent_fun_context) -> raise True
| EArrow (fun_decl, AUnknown) -> super#fun_decl fun_decl
| EArrow (_, _, ANo_fun_context) -> ()
| EArrow (_, _, AUse_parent_fun_context) -> raise True
| EArrow (fun_decl, _, AUnknown) -> super#fun_decl fun_decl
| _ -> super#expression x
end)
#statements
Expand Down Expand Up @@ -1752,11 +1752,11 @@ class simpl =
| EFun
(None, (({ generator = false; async = true | false }, _, body, _) as fun_decl))
when Config.Flag.es6 () && not (use_fun_context body) ->
EArrow (fun_decl, ANo_fun_context)
| EArrow (((_, _, body, _) as fun_decl), AUnknown) ->
EArrow (fun_decl, false, ANo_fun_context)
| EArrow (((_, _, body, _) as fun_decl), consise, AUnknown) ->
if use_fun_context body
then EArrow (fun_decl, AUse_parent_fun_context)
else EArrow (fun_decl, ANo_fun_context)
then EArrow (fun_decl, consise, AUse_parent_fun_context)
else EArrow (fun_decl, consise, ANo_fun_context)
| e -> e

method statement s =
Expand Down
2 changes: 1 addition & 1 deletion compiler/tests-compiler/es6.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let f x =
"use strict";
var
runtime = globalThis.jsoo_runtime,
f = x=>{var g = y=>(x + y | 0) + 7 | 0; return g;},
f = x=>{var g = y=>{return (x + y | 0) + 7 | 0;}; return g;},
Test = [0, f];
runtime.caml_register_global(0, Test, "Test");
return;})
Expand Down
2 changes: 1 addition & 1 deletion compiler/tests-compiler/util/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class find_function_declaration r n =
List.iter l ~f:(function
| DeclIdent
( (S { name = Utf8 name; _ } as id)
, Some ((EFun (_, fun_decl) | EArrow (fun_decl, _)), _) ) -> (
, Some ((EFun (_, fun_decl) | EArrow (fun_decl, _, _)), _) ) -> (
let fd = id, fun_decl in
match n with
| None -> r := fd :: !r
Expand Down

0 comments on commit a2ae471

Please sign in to comment.