Skip to content

Commit 80da451

Browse files
committed
typeof bool && x
1 parent 25680ed commit 80da451

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

compiler/core/js_exp_make.ml

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,9 @@ let rec push_negation (e : t) : t option =
716716
- [(typeof x === "boolean" | "string" | "number") && (x === boolean/null/undefined/123/"hello")] -> [false]
717717
- [(Array.isArray(x)) && (x === boolean/null/undefined/123/"hello")] -> [false]
718718
719-
- [(typeof x === "boolean") && (x !== true/false)] -> unchanged
719+
- [(typeof x === "boolean") && (x)] -> [(x == true)]
720+
- [(typeof x === "boolean") && (!x)] -> [(x == false)]
721+
- [(typeof x === "boolean") && (x !== true/false)] -> [(x == false/true)]
720722
- [(typeof x === "string") && (x !== "abc")] -> unchanged
721723
- [(typeof x === "number") && (x !== 123)] -> unchanged
722724
- [(typeof x === "object") && (x !== null)] -> unchanged
@@ -861,14 +863,48 @@ let rec simplify_and ~n (e1 : t) (e2 : t) : t option =
861863
( EqEqEq,
862864
{expression_desc = Typeof {expression_desc = Var ia}},
863865
{expression_desc = Str {txt = "boolean"}} ),
864-
Bin (NotEqEq, {expression_desc = Var ib}, {expression_desc = Bool _}) )
865-
| ( Bin (NotEqEq, {expression_desc = Var ib}, {expression_desc = Bool _}),
866+
Var ib )
867+
| ( Var ib,
866868
Bin
867869
( EqEqEq,
868870
{expression_desc = Typeof {expression_desc = Var ia}},
869871
{expression_desc = Str {txt = "boolean"}} ) )
870872
when Js_op_util.same_vident ia ib ->
871-
None
873+
Some
874+
{
875+
expression_desc =
876+
Bin (EqEqEq, {expression_desc = Var ib; comment = None}, true_);
877+
comment = None;
878+
}
879+
| ( Bin
880+
( EqEqEq,
881+
{expression_desc = Typeof {expression_desc = Var ia}},
882+
{expression_desc = Str {txt = "boolean"}} ),
883+
Js_not {expression_desc = Var ib} )
884+
| ( Js_not {expression_desc = Var ib},
885+
Bin
886+
( EqEqEq,
887+
{expression_desc = Typeof {expression_desc = Var ia}},
888+
{expression_desc = Str {txt = "boolean"}} ) )
889+
when Js_op_util.same_vident ia ib ->
890+
Some
891+
{
892+
expression_desc =
893+
Bin (EqEqEq, {expression_desc = Var ib; comment = None}, false_);
894+
comment = None;
895+
}
896+
| ( Bin
897+
( EqEqEq,
898+
{expression_desc = Typeof {expression_desc = Var ia}},
899+
{expression_desc = Str {txt = "boolean"}} ),
900+
Bin (NotEqEq, {expression_desc = Var ib}, {expression_desc = Bool b}) )
901+
| ( Bin (NotEqEq, {expression_desc = Var ib}, {expression_desc = Bool b}),
902+
Bin
903+
( EqEqEq,
904+
{expression_desc = Typeof {expression_desc = Var ia}},
905+
{expression_desc = Str {txt = "boolean"}} ) )
906+
when Js_op_util.same_vident ia ib ->
907+
Some {expression_desc = Bool (not b); comment = None}
872908
| ( Bin
873909
( EqEqEq,
874910
{expression_desc = Typeof {expression_desc = Var ia}},

tests/tests/src/core/Core_JsonTests.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ function decodeJsonTest() {
1111
let match$1 = match["thirdProp"];
1212
if (Array.isArray(match$1) && match$1.length === 2) {
1313
let match$2 = match$1[0];
14-
if (typeof match$2 === "boolean" && match$2) {
14+
if (match$2 === true) {
1515
let match$3 = match$1[1];
16-
decodedCorrectly = typeof match$3 === "boolean" && !match$3;
16+
decodedCorrectly = match$3 === false;
1717
} else {
1818
decodedCorrectly = false;
1919
}

0 commit comments

Comments
 (0)