Skip to content

Commit a417524

Browse files
committed
Handle combination of typeof and != const
1 parent eae1f41 commit a417524

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

compiler/core/js_exp_make.ml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,10 +711,11 @@ let rec push_negation (e : t) : t option =
711711
712712
Type check optimizations:
713713
- [(typeof x === "boolean") && (x === true/false)] -> [x === true/false]
714-
- [(typeof x === "string") && (x === boolean/null/undefined)] -> [false]
714+
- [(typeof x ==="boolean" | "string" | "number") && (x === boolean/null/undefined)] -> [false]
715715
- [(Array.isArray(x)) && (x === boolean/null/undefined)] -> [false]
716716
717-
- TODO: [(typeof x === "boolean") && (x !== null)] -> [typeof x === "boolean"]
717+
- [(typeof x === "boolean") && (x !== true/false)] -> unchanged
718+
- [(typeof x === "boolean" | "string" | "number") && (x !== boolean/null/undefined)] -> [typeof x === ...]
718719
719720
Note: The function preserves the semantics of the original expression while
720721
attempting to reduce it to a simpler form. If no simplification is possible,
@@ -828,6 +829,39 @@ let rec simplify_and (e1 : t) (e2 : t) : t option =
828829
_ ) )
829830
when Js_op_util.same_vident ia ib ->
830831
Some false_
832+
| ( Bin
833+
( EqEqEq,
834+
{expression_desc = Typeof {expression_desc = Var ia}},
835+
{expression_desc = Str {txt = "boolean"}} ),
836+
Bin (NotEqEq, {expression_desc = Var ib}, {expression_desc = Bool _}) )
837+
| ( Bin (NotEqEq, {expression_desc = Var ib}, {expression_desc = Bool _}),
838+
Bin
839+
( EqEqEq,
840+
{expression_desc = Typeof {expression_desc = Var ia}},
841+
{expression_desc = Str {txt = "boolean"}} ) )
842+
when Js_op_util.same_vident ia ib ->
843+
None
844+
| ( (Bin
845+
( EqEqEq,
846+
{expression_desc = Typeof {expression_desc = Var ia}},
847+
{expression_desc = Str {txt = "boolean" | "string" | "number"}} ) as
848+
typeof),
849+
Bin
850+
( NotEqEq,
851+
{expression_desc = Var ib},
852+
{expression_desc = Bool _ | Null | Undefined _} ) )
853+
| ( Bin
854+
( NotEqEq,
855+
{expression_desc = Var ib},
856+
{expression_desc = Bool _ | Null | Undefined _} ),
857+
(Bin
858+
( EqEqEq,
859+
{expression_desc = Typeof {expression_desc = Var ia}},
860+
{expression_desc = Str {txt = "boolean" | "string" | "number"}} ) as
861+
typeof) )
862+
when Js_op_util.same_vident ia ib ->
863+
(* Note: case boolean / Bool _ is handled above *)
864+
Some {expression_desc = typeof; comment = None}
831865
| _ -> None
832866

833867
(**

tests/tests/src/and_or_simplify.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function check_null_eq_typeof(x) {
66
}
77

88
function check_null_neq_typeof(x) {
9-
if (typeof x !== "boolean" || x === null) {
9+
if (typeof x !== "boolean") {
1010
return 4;
1111
} else {
1212
return 3;
@@ -18,7 +18,7 @@ function check_undefined_eq_typeof(x) {
1818
}
1919

2020
function check_undefined_neq_typeof(x) {
21-
if (typeof x !== "boolean" || x === undefined) {
21+
if (typeof x !== "boolean") {
2222
return 4;
2323
} else {
2424
return 3;

0 commit comments

Comments
 (0)