Skip to content

Commit acdfaf9

Browse files
committed
formatting
1 parent aaf840b commit acdfaf9

File tree

2 files changed

+78
-36
lines changed

2 files changed

+78
-36
lines changed

jscomp/core/js_stmt_make.ml

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ let int_switch ?comment ?declaration ?default (e : J.expression) clauses : t
111111
end in
112112
begin match declaration, continuation with
113113
| Some (kind, did),
114-
[ {statement_desc = Exp {expression_desc = Bin(Eq, {expression_desc = Var (Id id) ; _}, e0); _}; _}]
114+
[ {statement_desc =
115+
Exp {
116+
expression_desc =
117+
Bin(Eq, {expression_desc = Var (Id id) ; _}, e0); _}; _}]
115118
when Ident.same did id
116119
->
117120
define_variable ?comment ~kind id e0
@@ -120,13 +123,13 @@ let int_switch ?comment ?declaration ?default (e : J.expression) clauses : t
120123
block (declare_variable ?comment ~kind did :: continuation)
121124
| None, _ -> block continuation
122125
end
123-
124126
| _ ->
125-
match declaration with
127+
begin match declaration with
126128
| Some (kind, did) ->
127129
block [declare_variable ?comment ~kind did ;
128130
{ statement_desc = J.Int_switch (e,clauses, default); comment}]
129131
| None -> { statement_desc = J.Int_switch (e,clauses, default); comment}
132+
end
130133

131134
let string_switch ?comment ?declaration ?default (e : J.expression) clauses : t=
132135
match e.expression_desc with
@@ -186,15 +189,21 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
186189
let declared = ref false in
187190
let rec aux ?comment (e : J.expression) (then_ : J.block) (else_ : J.block ) acc =
188191
match e.expression_desc, then_, (else_ : J.block ) with
189-
| _, [ {statement_desc = Return {return_value = b; _}; _}],
192+
| _,
193+
[ {statement_desc = Return {return_value = b; _}; _}],
190194
[ {statement_desc = Return {return_value = a; _}; _}]
191195
->
192196
return_stmt (E.econd e b a ) :: acc
193-
| _, [ {statement_desc =
194-
Exp {expression_desc = Bin(Eq, ({expression_desc = Var (Id id0); _} as l0), a0); _}; _}],
197+
| _,
195198
[ {statement_desc =
196-
Exp ({ expression_desc = Bin(Eq,
197-
{expression_desc = Var (Id id1); _}, b0); _}); _}]
199+
Exp
200+
{expression_desc = Bin(Eq, ({expression_desc = Var (Id id0); _} as l0), a0); _};
201+
_}],
202+
[ {statement_desc =
203+
Exp (
204+
{ expression_desc =
205+
Bin(Eq,
206+
{expression_desc = Var (Id id1); _}, b0); _}); _}]
198207
when Ident.same id0 id1 ->
199208
begin match declaration with
200209
| Some (kind,did) when Ident.same did id0 ->
@@ -207,13 +216,13 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
207216
exp (E.assign l0 (E.econd e a0 b0)) :: acc
208217
end
209218

210-
| _, _,
211-
[ {statement_desc = Exp {expression_desc = Number _}; _}]
219+
| _, _,
220+
({statement_desc = Exp {expression_desc = Number _}; _}::more_else)
212221
->
213-
aux ?comment e then_ [] acc
214-
| _, [ {statement_desc = Exp {expression_desc = Number _}; _}], _
222+
aux ?comment e then_ more_else acc
223+
| _, ({statement_desc = Exp {expression_desc = Number _}; _} :: more_then), _
215224
->
216-
aux ?comment e [] else_ acc
225+
aux ?comment e more_then else_ acc
217226

218227
| _, [ {statement_desc = Exp b; _}], [ {statement_desc = Exp a; _}]
219228
->
@@ -234,6 +243,7 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
234243
move code outside of branch is generally helpful later
235244
*)
236245
aux ?comment e ys xs (y::acc)
246+
237247

238248
| Number ( Int { i = 0l; _}) , _, _
239249
->
@@ -269,13 +279,24 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
269279
(** Add comment when simplified *)
270280
aux ?comment e then_ else_ acc
271281

282+
(*
283+
{[ if a then { if b then d else e} else e ]}
284+
285+
=> if a && b then d else e
286+
*)
287+
(* | _,
288+
[ {statement_desc = If (pred, then_, Some ([else_] as cont)) }],
289+
[ another_else] when Js_analyzer.eq_statement else_ another_else
290+
->
291+
aux ?comment (E.and_ e pred) then_ cont acc *)
272292
| _ ->
273293
let e = E.ocaml_boolean_under_condition e in
274-
{ statement_desc = If (e,
275-
then_,
276-
(match else_ with
277-
| [] -> None
278-
| v -> Some v));
294+
{ statement_desc =
295+
If (e,
296+
then_,
297+
(match else_ with
298+
| [] -> None
299+
| v -> Some v));
279300
comment } :: acc in
280301
let if_block =
281302
aux ?comment e then_ (match else_ with None -> [] | Some v -> v) [] in

lib/whole_compiler.ml

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73506,7 +73506,10 @@ let int_switch ?comment ?declaration ?default (e : J.expression) clauses : t
7350673506
end in
7350773507
begin match declaration, continuation with
7350873508
| Some (kind, did),
73509-
[ {statement_desc = Exp {expression_desc = Bin(Eq, {expression_desc = Var (Id id) ; _}, e0); _}; _}]
73509+
[ {statement_desc =
73510+
Exp {
73511+
expression_desc =
73512+
Bin(Eq, {expression_desc = Var (Id id) ; _}, e0); _}; _}]
7351073513
when Ident.same did id
7351173514
->
7351273515
define_variable ?comment ~kind id e0
@@ -73515,13 +73518,13 @@ let int_switch ?comment ?declaration ?default (e : J.expression) clauses : t
7351573518
block (declare_variable ?comment ~kind did :: continuation)
7351673519
| None, _ -> block continuation
7351773520
end
73518-
7351973521
| _ ->
73520-
match declaration with
73522+
begin match declaration with
7352173523
| Some (kind, did) ->
7352273524
block [declare_variable ?comment ~kind did ;
7352373525
{ statement_desc = J.Int_switch (e,clauses, default); comment}]
7352473526
| None -> { statement_desc = J.Int_switch (e,clauses, default); comment}
73527+
end
7352573528

7352673529
let string_switch ?comment ?declaration ?default (e : J.expression) clauses : t=
7352773530
match e.expression_desc with
@@ -73581,15 +73584,21 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
7358173584
let declared = ref false in
7358273585
let rec aux ?comment (e : J.expression) (then_ : J.block) (else_ : J.block ) acc =
7358373586
match e.expression_desc, then_, (else_ : J.block ) with
73584-
| _, [ {statement_desc = Return {return_value = b; _}; _}],
73587+
| _,
73588+
[ {statement_desc = Return {return_value = b; _}; _}],
7358573589
[ {statement_desc = Return {return_value = a; _}; _}]
7358673590
->
7358773591
return_stmt (E.econd e b a ) :: acc
73588-
| _, [ {statement_desc =
73589-
Exp {expression_desc = Bin(Eq, ({expression_desc = Var (Id id0); _} as l0), a0); _}; _}],
73592+
| _,
7359073593
[ {statement_desc =
73591-
Exp ({ expression_desc = Bin(Eq,
73592-
{expression_desc = Var (Id id1); _}, b0); _}); _}]
73594+
Exp
73595+
{expression_desc = Bin(Eq, ({expression_desc = Var (Id id0); _} as l0), a0); _};
73596+
_}],
73597+
[ {statement_desc =
73598+
Exp (
73599+
{ expression_desc =
73600+
Bin(Eq,
73601+
{expression_desc = Var (Id id1); _}, b0); _}); _}]
7359373602
when Ident.same id0 id1 ->
7359473603
begin match declaration with
7359573604
| Some (kind,did) when Ident.same did id0 ->
@@ -73602,13 +73611,13 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
7360273611
exp (E.assign l0 (E.econd e a0 b0)) :: acc
7360373612
end
7360473613

73605-
| _, _,
73606-
[ {statement_desc = Exp {expression_desc = Number _}; _}]
73614+
| _, _,
73615+
({statement_desc = Exp {expression_desc = Number _}; _}::more_else)
7360773616
->
73608-
aux ?comment e then_ [] acc
73609-
| _, [ {statement_desc = Exp {expression_desc = Number _}; _}], _
73617+
aux ?comment e then_ more_else acc
73618+
| _, ({statement_desc = Exp {expression_desc = Number _}; _} :: more_then), _
7361073619
->
73611-
aux ?comment e [] else_ acc
73620+
aux ?comment e more_then else_ acc
7361273621

7361373622
| _, [ {statement_desc = Exp b; _}], [ {statement_desc = Exp a; _}]
7361473623
->
@@ -73629,6 +73638,7 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
7362973638
move code outside of branch is generally helpful later
7363073639
*)
7363173640
aux ?comment e ys xs (y::acc)
73641+
7363273642

7363373643
| Number ( Int { i = 0l; _}) , _, _
7363473644
->
@@ -73664,13 +73674,24 @@ let rec if_ ?comment ?declaration ?else_ (e : J.expression) (then_ : J.block)
7366473674
(** Add comment when simplified *)
7366573675
aux ?comment e then_ else_ acc
7366673676

73677+
(*
73678+
{[ if a then { if b then d else e} else e ]}
73679+
73680+
=> if a && b then d else e
73681+
*)
73682+
(* | _,
73683+
[ {statement_desc = If (pred, then_, Some ([else_] as cont)) }],
73684+
[ another_else] when Js_analyzer.eq_statement else_ another_else
73685+
->
73686+
aux ?comment (E.and_ e pred) then_ cont acc *)
7366773687
| _ ->
7366873688
let e = E.ocaml_boolean_under_condition e in
73669-
{ statement_desc = If (e,
73670-
then_,
73671-
(match else_ with
73672-
| [] -> None
73673-
| v -> Some v));
73689+
{ statement_desc =
73690+
If (e,
73691+
then_,
73692+
(match else_ with
73693+
| [] -> None
73694+
| v -> Some v));
7367473695
comment } :: acc in
7367573696
let if_block =
7367673697
aux ?comment e then_ (match else_ with None -> [] | Some v -> v) [] in

0 commit comments

Comments
 (0)