@@ -718,6 +718,10 @@ let rec push_negation (e : t) : t option =
718718 - [(typeof x === "boolean" | "string" | "number") && (x !== boolean/null/undefined)] -> [typeof x === ...]
719719 - [(Array.isArray(x)) && (x !== boolean/null/undefined)] -> [Array.isArray(x)]
720720
721+ Equality optimizations:
722+ - [e && e] -> [e]
723+ - [(x === boolean/null/undefined) && (x === boolean/null/undefined)] -> [false] (when not equal)
724+
721725 Note: The function preserves the semantics of the original expression while
722726 attempting to reduce it to a simpler form. If no simplification is possible,
723727 returns [None].
@@ -726,7 +730,6 @@ let rec simplify_and (e1 : t) (e2 : t) : t option =
726730 if debug then
727731 Printf. eprintf " simplify_and %s %s\n " (! string_of_expression e1)
728732 (! string_of_expression e2);
729-
730733 match (e1.expression_desc, e2.expression_desc) with
731734 | Bool false , _ -> Some false_
732735 | _ , Bool false -> Some false_
@@ -735,39 +738,21 @@ let rec simplify_and (e1 : t) (e2 : t) : t option =
735738 | Bin (And, a , b ), _ -> (
736739 let ao = simplify_and a e2 in
737740 let bo = simplify_and b e2 in
738- if ao = None && bo = None then None
739- else
740- let a_ =
741- match ao with
742- | None -> a
743- | Some a_ -> a_
744- in
745- let b_ =
746- match bo with
747- | None -> b
748- | Some b_ -> b_
749- in
741+ match (ao, bo) with
742+ | None , _ | _ , None -> None
743+ | Some a_ , Some b_ -> (
750744 match simplify_and a_ b_ with
751745 | None -> Some {expression_desc = Bin (And , a_, b_); comment = None }
752- | Some e -> Some e)
746+ | Some e -> Some e))
753747 | Bin (Or, a , b ), _ -> (
754748 let ao = simplify_and a e2 in
755749 let bo = simplify_and b e2 in
756- if ao = None && bo = None then None
757- else
758- let a_ =
759- match ao with
760- | None -> a
761- | Some a_ -> a_
762- in
763- let b_ =
764- match bo with
765- | None -> b
766- | Some b_ -> b_
767- in
750+ match (ao, bo) with
751+ | None , _ | _ , None -> None
752+ | Some a_ , Some b_ -> (
768753 match simplify_or a_ b_ with
769754 | None -> Some {expression_desc = Bin (Or , a_, b_); comment = None }
770- | Some e -> Some e)
755+ | Some e -> Some e))
771756 | ( Bin
772757 ( ((EqEqEq | NotEqEq ) as op1),
773758 {expression_desc = Var i1},
@@ -882,6 +867,17 @@ let rec simplify_and (e1 : t) (e2 : t) : t option =
882867 when Js_op_util. same_vident ia ib ->
883868 Some {expression_desc = is_array; comment = None }
884869 | x , y when x = y -> Some e1
870+ | ( Bin
871+ ( EqEqEq ,
872+ {expression_desc = Var ia},
873+ {expression_desc = Bool _ | Null | Undefined _} ),
874+ Bin
875+ ( EqEqEq ,
876+ {expression_desc = Var ib},
877+ {expression_desc = Bool _ | Null | Undefined _} ) )
878+ when Js_op_util. same_vident ia ib ->
879+ (* Note: case x = y is handled above *)
880+ Some false_
885881 | _ -> None
886882
887883(* *
0 commit comments