Skip to content

Commit

Permalink
refactor: J.{Int,String}_switch (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored Oct 27, 2024
1 parent fa6edd2 commit 37577ac
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
12 changes: 10 additions & 2 deletions jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,16 @@ and statement_desc =
A more robust signature would be
{[ goto : label option ; ]}
*)
| Int_switch of expression * int_clause list * block option
| String_switch of expression * string_clause list * block option
| Int_switch of {
expr : expression;
clauses : int_clause list;
default : block option;
}
| String_switch of {
expr : expression;
clauses : string_clause list;
default : block option;
}
| Throw of expression
| Try of block * (exception_ident * block) option * block option
| Debugger
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
let cxt = expression ~level:0 cxt e in
semi cxt;
cxt))
| Int_switch (e, cc, def) ->
| Int_switch { expr = e; clauses = cc; default = def } ->
string cxt L.switch;
space cxt;
let cxt = paren_group cxt 1 (fun _ -> expression ~level:0 cxt e) in
Expand All @@ -1199,7 +1199,7 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
string cxt L.colon;
newline cxt;
statements ~top:false cxt def))
| String_switch (e, cc, def) ->
| String_switch { expr = e; clauses = cc; default = def } ->
string cxt L.switch;
space cxt;
let cxt = paren_group cxt 1 (fun _ -> expression ~level:0 cxt e) in
Expand Down
22 changes: 17 additions & 5 deletions jscomp/core/js_stmt_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@ let int_switch ?(comment : string option)
block
[
declare_variable ?comment ~kind did;
{ statement_desc = J.Int_switch (e, clauses, default); comment };
{
statement_desc = J.Int_switch { expr = e; clauses; default };
comment;
};
]
| None -> { statement_desc = J.Int_switch (e, clauses, default); comment }
)
| None ->
{
statement_desc = J.Int_switch { expr = e; clauses; default };
comment;
})

let string_switch ?(comment : string option)
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
Expand Down Expand Up @@ -171,10 +177,16 @@ let string_switch ?(comment : string option)
block
[
declare_variable ?comment ~kind did;
{ statement_desc = String_switch (e, clauses, default); comment };
{
statement_desc = String_switch { expr = e; clauses; default };
comment;
};
]
| None ->
{ statement_desc = String_switch (e, clauses, default); comment })
{
statement_desc = String_switch { expr = e; clauses; default };
comment;
})

let rec block_last_is_return_throw_or_continue (x : J.block) =
match x with
Expand Down

0 comments on commit 37577ac

Please sign in to comment.