Skip to content

Commit 7b9b0c5

Browse files
committed
Begin adding tests.
1 parent 381cfdf commit 7b9b0c5

File tree

5 files changed

+62
-20
lines changed

5 files changed

+62
-20
lines changed

compiler/core/js_dump.ml

+2
Original file line numberDiff line numberDiff line change
@@ -1348,3 +1348,5 @@ let string_of_expression (e : J.expression) =
13481348
let (_ : cxt) = expression ~level:0 Ext_pp_scope.empty f e in
13491349
P.flush f ();
13501350
Buffer.contents buffer
1351+
1352+
let () = E.string_of_expression := string_of_expression

compiler/core/js_exp_make.ml

+20-20
Original file line numberDiff line numberDiff line change
@@ -663,20 +663,26 @@ let bin ?comment (op : J.binop) (e0 : t) (e1 : t) : t =
663663
*)
664664

665665
type filter_const = Ctrue | Cnull
666-
667-
let rec filter_const (e : t) ~j ~b:eq ~const =
666+
let string_of_filter_const = function
667+
| Ctrue -> "Ctrue"
668+
| Cnull -> "Cnull"
669+
670+
let string_of_expression = ref (fun _ -> "")
671+
let debug = false
672+
673+
let rec filter_const (e : t) ~j ~eq ~const =
674+
if debug then
675+
Printf.eprintf "filter_const e:%s eq:%b const:%s\n"
676+
(!string_of_expression e) eq
677+
(string_of_filter_const const);
668678
match e.expression_desc with
669679
| Bin (And, e1, e2) -> (
670-
match
671-
(filter_const e1 ~j ~b:eq ~const, filter_const e2 ~j ~b:eq ~const)
672-
with
680+
match (filter_const e1 ~j ~eq ~const, filter_const e2 ~j ~eq ~const) with
673681
| None, None -> None
674682
| Some e, None | None, Some e -> Some e
675683
| Some e1, Some e2 -> Some {e with expression_desc = Bin (And, e1, e2)})
676684
| Bin (Or, e1, e2) -> (
677-
match
678-
(filter_const e1 ~j ~b:eq ~const, filter_const e2 ~j ~b:eq ~const)
679-
with
685+
match (filter_const e1 ~j ~eq ~const, filter_const e2 ~j ~eq ~const) with
680686
| None, _ | _, None -> None
681687
| Some e1, Some e2 -> Some {e with expression_desc = Bin (Or, e1, e2)})
682688
| Bin (EqEqEq, {expression_desc = Var i}, {expression_desc = Bool b1})
@@ -690,14 +696,8 @@ let rec filter_const (e : t) ~j ~b:eq ~const =
690696
| Bin
691697
( NotEqEq,
692698
{expression_desc = Typeof {expression_desc = Var i}},
693-
{expression_desc = Str {txt = "bool"}} )
694-
when Js_op_util.same_vident i j && const = Ctrue ->
695-
None
696-
| Bin
697-
( NotEqEq,
698-
{expression_desc = Typeof {expression_desc = Var i}},
699-
{expression_desc = Str {txt = "string"}} )
700-
when Js_op_util.same_vident i j && (const = Cnull || const = Ctrue) ->
699+
{expression_desc = Str {txt = "boolean" | "string"}} )
700+
when Js_op_util.same_vident i j && (const = Ctrue || const = Cnull) ->
701701
None
702702
| Js_not
703703
{
@@ -737,7 +737,7 @@ let and_ ?comment (e1 : t) (e2 : t) : t =
737737
{expression_desc = Bool b} ),
738738
_ ) -> (
739739
match
740-
filter_const e1 ~j ~b:(if op = EqEqEq then b else not b) ~const:Ctrue
740+
filter_const e1 ~j ~eq:(if op = EqEqEq then b else not b) ~const:Ctrue
741741
with
742742
| None -> e2
743743
| Some e1 -> {expression_desc = Bin (And, e1, e2); comment})
@@ -751,7 +751,7 @@ let and_ ?comment (e1 : t) (e2 : t) : t =
751751
{expression_desc = Var j},
752752
{expression_desc = Null} ),
753753
_ ) -> (
754-
match filter_const e1 ~j ~b:(op = EqEqEq) ~const:Cnull with
754+
match filter_const e1 ~j ~eq:(op = EqEqEq) ~const:Cnull with
755755
| None -> e2
756756
| Some e1 -> {expression_desc = Bin (And, e1, e2); comment})
757757
| _, _ -> {expression_desc = Bin (And, e1, e2); comment}
@@ -770,15 +770,15 @@ let or_ ?comment (e1 : t) (e2 : t) =
770770
( ((EqEqEq | NotEqEq) as op),
771771
{expression_desc = Var j},
772772
{expression_desc = Null} ) ) -> (
773-
match filter_const e1 ~j ~b:(op <> EqEqEq) ~const:Cnull with
773+
match filter_const e1 ~j ~eq:(op <> EqEqEq) ~const:Cnull with
774774
| None -> e2
775775
| Some e1 -> {expression_desc = Bin (Or, e1, e2); comment})
776776
| ( Bin
777777
( ((EqEqEq | NotEqEq) as op),
778778
{expression_desc = Var j},
779779
{expression_desc = Null} ),
780780
_ ) -> (
781-
match filter_const e2 ~j ~b:(op <> EqEqEq) ~const:Cnull with
781+
match filter_const e2 ~j ~eq:(op <> EqEqEq) ~const:Cnull with
782782
| None -> e1
783783
| Some e2 -> {expression_desc = Bin (Or, e1, e2); comment})
784784
| _, _ -> {expression_desc = Bin (Or, e1, e2); comment}

compiler/core/js_exp_make.mli

+2
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,5 @@ val is_null_undefined : ?comment:string -> t -> t
372372
val make_exception : string -> t
373373

374374
val variadic_args : t list -> t list
375+
376+
val string_of_expression : (t -> string) ref

tests/tests/src/and_or_simplify.mjs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
4+
function check_null_typeof(x) {
5+
if (x !== null) {
6+
return 4;
7+
} else {
8+
return 3;
9+
}
10+
}
11+
12+
function check_undefined_typeof(x) {
13+
if (typeof x !== "boolean" || x !== undefined) {
14+
return 4;
15+
} else {
16+
return 3;
17+
}
18+
}
19+
20+
export {
21+
check_null_typeof,
22+
check_undefined_typeof,
23+
}
24+
/* No side effect */

tests/tests/src/and_or_simplify.res

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@unboxed
2+
type t = | @as(null) Null | @as(undefined) Undefined | B(bool)
3+
4+
let check_null_typeof = x =>
5+
switch x {
6+
| B(_) if x == Null => 3
7+
| _ => 4
8+
}
9+
10+
let check_undefined_typeof = x =>
11+
switch x {
12+
| B(_) if x == Undefined => 3
13+
| _ => 4
14+
}

0 commit comments

Comments
 (0)