Skip to content

Commit 2386f6e

Browse files
committed
Handle undefined too.
1 parent 7b9b0c5 commit 2386f6e

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

compiler/core/js_exp_make.ml

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,11 @@ let bin ?comment (op : J.binop) (e0 : t) (e1 : t) : t =
662662
be careful for side effect
663663
*)
664664

665-
type filter_const = Ctrue | Cnull
665+
type filter_const = Ctrue | Cnull | Cundefined
666666
let string_of_filter_const = function
667667
| Ctrue -> "Ctrue"
668668
| Cnull -> "Cnull"
669+
| Cundefined -> "Cundefined"
669670

670671
let string_of_expression = ref (fun _ -> "")
671672
let debug = false
@@ -697,7 +698,8 @@ let rec filter_const (e : t) ~j ~eq ~const =
697698
( NotEqEq,
698699
{expression_desc = Typeof {expression_desc = Var i}},
699700
{expression_desc = Str {txt = "boolean" | "string"}} )
700-
when Js_op_util.same_vident i j && (const = Ctrue || const = Cnull) ->
701+
when Js_op_util.same_vident i j
702+
&& (const = Ctrue || const = Cnull || const = Cundefined) ->
701703
None
702704
| Js_not
703705
{
@@ -707,7 +709,8 @@ let rec filter_const (e : t) ~j ~eq ~const =
707709
[{expression_desc = Var i}],
708710
_ );
709711
}
710-
when Js_op_util.same_vident i j && (const = Ctrue || const = Cnull) ->
712+
when Js_op_util.same_vident i j
713+
&& (const = Ctrue || const = Cnull || const = Cundefined) ->
711714
None
712715
| _ -> Some e
713716

@@ -730,30 +733,40 @@ let and_ ?comment (e1 : t) (e2 : t) : t =
730733
Bin
731734
( ((EqEqEq | NotEqEq) as op),
732735
{expression_desc = Var j},
733-
{expression_desc = Bool b} ) )
736+
{expression_desc = Bool b} ) ) -> (
737+
match
738+
filter_const e1 ~j ~eq:(if op = EqEqEq then b else not b) ~const:Ctrue
739+
with
740+
| None -> e2
741+
| Some e1 -> {expression_desc = Bin (And, e1, e2); comment})
734742
| ( Bin
735743
( ((EqEqEq | NotEqEq) as op),
736744
{expression_desc = Var j},
737745
{expression_desc = Bool b} ),
738746
_ ) -> (
739747
match
740-
filter_const e1 ~j ~eq:(if op = EqEqEq then b else not b) ~const:Ctrue
748+
filter_const e2 ~j ~eq:(if op = EqEqEq then b else not b) ~const:Ctrue
741749
with
742-
| None -> e2
743-
| Some e1 -> {expression_desc = Bin (And, e1, e2); comment})
750+
| None -> e1
751+
| Some e2 -> {expression_desc = Bin (And, e1, e2); comment})
744752
| ( _,
745753
Bin
746754
( ((EqEqEq | NotEqEq) as op),
747755
{expression_desc = Var j},
748-
{expression_desc = Null} ) )
756+
{expression_desc = (Null | Undefined _) as c} ) ) -> (
757+
let const = if c == Null then Cnull else Cundefined in
758+
match filter_const e1 ~j ~eq:(op = EqEqEq) ~const with
759+
| None -> e2
760+
| Some e1 -> {expression_desc = Bin (And, e1, e2); comment})
749761
| ( Bin
750762
( ((EqEqEq | NotEqEq) as op),
751763
{expression_desc = Var j},
752-
{expression_desc = Null} ),
764+
{expression_desc = (Null | Undefined _) as c} ),
753765
_ ) -> (
754-
match filter_const e1 ~j ~eq:(op = EqEqEq) ~const:Cnull with
755-
| None -> e2
756-
| Some e1 -> {expression_desc = Bin (And, e1, e2); comment})
766+
let const = if c == Null then Cnull else Cundefined in
767+
match filter_const e2 ~j ~eq:(op = EqEqEq) ~const with
768+
| None -> e1
769+
| Some e2 -> {expression_desc = Bin (And, e1, e2); comment})
757770
| _, _ -> {expression_desc = Bin (And, e1, e2); comment}
758771

759772
let or_ ?comment (e1 : t) (e2 : t) =
@@ -769,16 +782,18 @@ let or_ ?comment (e1 : t) (e2 : t) =
769782
Bin
770783
( ((EqEqEq | NotEqEq) as op),
771784
{expression_desc = Var j},
772-
{expression_desc = Null} ) ) -> (
773-
match filter_const e1 ~j ~eq:(op <> EqEqEq) ~const:Cnull with
785+
{expression_desc = (Null | Undefined _) as c} ) ) -> (
786+
let const = if c == Null then Cnull else Cundefined in
787+
match filter_const e1 ~j ~eq:(op <> EqEqEq) ~const with
774788
| None -> e2
775789
| Some e1 -> {expression_desc = Bin (Or, e1, e2); comment})
776790
| ( Bin
777791
( ((EqEqEq | NotEqEq) as op),
778792
{expression_desc = Var j},
779-
{expression_desc = Null} ),
793+
{expression_desc = (Null | Undefined _) as c} ),
780794
_ ) -> (
781-
match filter_const e2 ~j ~eq:(op <> EqEqEq) ~const:Cnull with
795+
let const = if c == Null then Cnull else Cundefined in
796+
match filter_const e2 ~j ~eq:(op <> EqEqEq) ~const with
782797
| None -> e1
783798
| Some e2 -> {expression_desc = Bin (Or, e1, e2); comment})
784799
| _, _ -> {expression_desc = Bin (Or, e1, e2); comment}

tests/tests/src/and_or_simplify.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function check_null_typeof(x) {
1010
}
1111

1212
function check_undefined_typeof(x) {
13-
if (typeof x !== "boolean" || x !== undefined) {
13+
if (x !== undefined) {
1414
return 4;
1515
} else {
1616
return 3;

0 commit comments

Comments
 (0)