@@ -662,10 +662,11 @@ let bin ?comment (op : J.binop) (e0 : t) (e1 : t) : t =
662
662
be careful for side effect
663
663
*)
664
664
665
- type filter_const = Ctrue | Cnull
665
+ type filter_const = Ctrue | Cnull | Cundefined
666
666
let string_of_filter_const = function
667
667
| Ctrue -> " Ctrue"
668
668
| Cnull -> " Cnull"
669
+ | Cundefined -> " Cundefined"
669
670
670
671
let string_of_expression = ref (fun _ -> " " )
671
672
let debug = false
@@ -697,7 +698,8 @@ let rec filter_const (e : t) ~j ~eq ~const =
697
698
( NotEqEq ,
698
699
{expression_desc = Typeof {expression_desc = Var i}},
699
700
{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 ) ->
701
703
None
702
704
| Js_not
703
705
{
@@ -707,7 +709,8 @@ let rec filter_const (e : t) ~j ~eq ~const =
707
709
[{expression_desc = Var i}],
708
710
_ );
709
711
}
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 ) ->
711
714
None
712
715
| _ -> Some e
713
716
@@ -730,30 +733,40 @@ let and_ ?comment (e1 : t) (e2 : t) : t =
730
733
Bin
731
734
( ((EqEqEq | NotEqEq ) as op),
732
735
{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})
734
742
| ( Bin
735
743
( ((EqEqEq | NotEqEq ) as op),
736
744
{expression_desc = Var j},
737
745
{expression_desc = Bool b} ),
738
746
_ ) -> (
739
747
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
741
749
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})
744
752
| ( _,
745
753
Bin
746
754
( ((EqEqEq | NotEqEq ) as op),
747
755
{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})
749
761
| ( Bin
750
762
( ((EqEqEq | NotEqEq ) as op),
751
763
{expression_desc = Var j},
752
- {expression_desc = Null } ),
764
+ {expression_desc = ( Null | Undefined _) as c } ),
753
765
_ ) -> (
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})
757
770
| _ , _ -> {expression_desc = Bin (And , e1, e2); comment}
758
771
759
772
let or_ ?comment (e1 : t ) (e2 : t ) =
@@ -769,16 +782,18 @@ let or_ ?comment (e1 : t) (e2 : t) =
769
782
Bin
770
783
( ((EqEqEq | NotEqEq ) as op),
771
784
{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
774
788
| None -> e2
775
789
| Some e1 -> {expression_desc = Bin (Or , e1, e2); comment})
776
790
| ( Bin
777
791
( ((EqEqEq | NotEqEq ) as op),
778
792
{expression_desc = Var j},
779
- {expression_desc = Null } ),
793
+ {expression_desc = ( Null | Undefined _) as c } ),
780
794
_ ) -> (
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
782
797
| None -> e1
783
798
| Some e2 -> {expression_desc = Bin (Or , e1, e2); comment})
784
799
| _ , _ -> {expression_desc = Bin (Or , e1, e2); comment}
0 commit comments