From bb90af3c1daa9ad25ae1d383fccafe6ea4c92189 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Sat, 26 Oct 2024 21:42:23 -0700 Subject: [PATCH] refactor: use inline record for `J.{While,ForRange}` (#1197) --- jscomp/core/j.ml | 15 ++++++++------- jscomp/core/js_dump.ml | 11 +++++++++-- jscomp/core/js_pass_scope.ml | 2 +- jscomp/core/js_stmt_make.ml | 11 +++++++++-- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/jscomp/core/j.ml b/jscomp/core/j.ml index 4a83ca581..b4f44590a 100644 --- a/jscomp/core/j.ml +++ b/jscomp/core/j.ml @@ -275,14 +275,15 @@ and statement_desc = (* Function declaration and Variable declaration *) | Exp of expression | If of { cond : expression; then_ : block; else_ : block } - | While of label option * expression * block + | While of { label : label option; cond : expression; body : block } (* check if it contains loop mutable values, happens in nested loop *) - | ForRange of - for_ident_expression option - * finish_ident_expression - * for_ident - * for_direction - * block + | ForRange of { + for_ident_expr : for_ident_expression option; + finish_expr : finish_ident_expression; + for_ident : for_ident; + direction : for_direction; + body : block; + } | Continue of label | Break (* only used when inline a fucntion *) | Return of expression diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index ed2d201b4..76317d33c 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -1060,7 +1060,7 @@ and statement_desc top cxt (s : J.statement_desc) : cxt = string cxt L.else_; space cxt; brace_block cxt s2) - | While (label, e, s) -> + | While { label; cond = e; body = s } -> (* FIXME: print scope as well *) (match label with | Some i -> @@ -1086,7 +1086,14 @@ and statement_desc top cxt (s : J.statement_desc) : cxt = let cxt = brace_block cxt s in semi cxt; cxt - | ForRange (for_ident_expression, finish, id, direction, s) -> + | ForRange + { + for_ident_expr = for_ident_expression; + finish_expr = finish; + for_ident = id; + direction; + body = s; + } -> let action cxt = vgroup cxt 0 (fun _ -> let cxt = diff --git a/jscomp/core/js_pass_scope.ml b/jscomp/core/js_pass_scope.ml index 9411c2798..2f3e1ed5a 100644 --- a/jscomp/core/js_pass_scope.ml +++ b/jscomp/core/js_pass_scope.ml @@ -159,7 +159,7 @@ let record_scope_pass = statement = (fun self state x -> match x.statement_desc with - | ForRange (_, _, loop_id, _, stmts) -> + | ForRange { for_ident = loop_id; body = stmts; _ } -> (* TODO: simplify definition of For *) let { defined_idents = defined_idents'; diff --git a/jscomp/core/js_stmt_make.ml b/jscomp/core/js_stmt_make.ml index 94330cd52..01ce6a7d6 100644 --- a/jscomp/core/js_stmt_make.ml +++ b/jscomp/core/js_stmt_make.ml @@ -372,13 +372,20 @@ let assign ?comment id e : t = { statement_desc = J.Exp (E.assign (E.var id) e); comment } let while_ ?comment ?label (e : E.t) (st : J.block) : t = - { statement_desc = While (label, e, st); comment } + { statement_desc = While { label; cond = e; body = st }; comment } let for_ ?comment for_ident_expression finish_ident_expression id direction (b : J.block) : t = { statement_desc = - ForRange (for_ident_expression, finish_ident_expression, id, direction, b); + ForRange + { + for_ident_expr = for_ident_expression; + finish_expr = finish_ident_expression; + for_ident = id; + direction; + body = b; + }; comment; }