Skip to content

Commit 79d5481

Browse files
committed
test(minifier): add and enable some tests in fold_constants (#8769)
Ported some tests that was missing from closure compiler. Also enabled some tests.
1 parent 8cf9d34 commit 79d5481

File tree

1 file changed

+58
-16
lines changed

1 file changed

+58
-16
lines changed

crates/oxc_minifier/src/peephole/fold_constants.rs

+58-16
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,19 @@ mod test {
10181018
fold_same("'' + x === !y");
10191019
}
10201020

1021+
#[test]
1022+
fn test_number_number_comparison() {
1023+
fold("1 > 1", "!1");
1024+
fold("2 == 3", "!1");
1025+
fold("3.6 === 3.6", "!0");
1026+
fold_same("+x > +y");
1027+
fold_same("+x == +y");
1028+
fold("+x === +y", "+x == +y");
1029+
fold_same("+x > +x"); // foldable to false
1030+
fold_same("+x == +x");
1031+
fold("+x === +x", "+x == +x");
1032+
}
1033+
10211034
#[test]
10221035
fn test_string_string_comparison() {
10231036
fold("'a' < 'b'", "!0");
@@ -1117,6 +1130,22 @@ mod test {
11171130
fold("NaN==foo()", "foo()==NaN");
11181131
}
11191132

1133+
#[test]
1134+
#[ignore]
1135+
fn test_object_comparison1() {
1136+
fold("!new Date()", "false");
1137+
fold("!!new Date()", "true");
1138+
1139+
fold("new Date() == null", "false");
1140+
fold("new Date() == undefined", "false");
1141+
fold("new Date() != null", "true");
1142+
fold("new Date() != undefined", "true");
1143+
fold("null == new Date()", "false");
1144+
fold("undefined == new Date()", "false");
1145+
fold("null != new Date()", "true");
1146+
fold("undefined != new Date()", "true");
1147+
}
1148+
11201149
#[test]
11211150
fn js_typeof() {
11221151
fold("x = typeof 1", "x = \"number\"");
@@ -1162,7 +1191,7 @@ mod test {
11621191
fold("a=+false", "a=0");
11631192
fold_same("a=+foo()");
11641193
fold_same("a=+f");
1165-
// test("a=+(f?true:false)", "a=+(f?1:0)");
1194+
fold("a=+(f?true:false)", "a=+!!f");
11661195
fold("a=+0", "a=0");
11671196
fold("a=+Infinity", "a=Infinity");
11681197
fold("a=+NaN", "a=NaN");
@@ -1175,7 +1204,7 @@ mod test {
11751204
fold_same("a=~~foo()");
11761205
fold("a=~0xffffffff", "a=0");
11771206
fold("a=~~0xffffffff", "a=-1");
1178-
// test_same("a=~.5");
1207+
fold("a=~.5", "a=-1");
11791208
}
11801209

11811210
#[test]
@@ -1381,6 +1410,8 @@ mod test {
13811410
// these cases look strange because bitwise OR converts unsigned numbers to be signed
13821411
fold("x = 1 | 3000000001", "x = -1294967295");
13831412
fold("x = 4294967295 | 0", "x = -1");
1413+
1414+
fold("x = -1 | 0", "x = -1");
13841415
}
13851416

13861417
#[test]
@@ -1482,39 +1513,45 @@ mod test {
14821513
fold("8589934591 >>> 0", "4294967295");
14831514
fold("8589934592 >>> 0", "0");
14841515
fold("8589934593 >>> 0", "1");
1516+
1517+
fold("x = -1 << 1", "x = -2");
1518+
fold("x = -1 << 8", "x = -256");
1519+
fold("x = -1 >> 1", "x = -1");
1520+
fold("x = -2 >> 1", "x = -1");
1521+
fold("x = -1 >> 0", "x = -1");
14851522
}
14861523

14871524
#[test]
14881525
fn test_string_add() {
14891526
fold("x = 'a' + 'bc'", "x = 'abc'");
14901527
fold("x = 'a' + 5", "x = 'a5'");
14911528
fold("x = 5 + 'a'", "x = '5a'");
1492-
// test("x = 'a' + 5n", "x = 'a5n'");
1493-
// test("x = 5n + 'a'", "x = '5na'");
1529+
fold("x = 'a' + 5n", "x = 'a5'");
1530+
fold("x = 5n + 'a'", "x = '5a'");
14941531
fold("x = 'a' + ''", "x = 'a'");
14951532
fold("x = 'a' + foo()", "x = 'a'+foo()");
14961533
fold("x = foo() + 'a' + 'b'", "x = foo()+'ab'");
14971534
fold("x = (foo() + 'a') + 'b'", "x = foo()+'ab'"); // believe it!
14981535
fold("x = foo() + 'a' + 'b' + 'cd' + bar()", "x = foo()+'abcd'+bar()");
14991536
fold("x = foo() + 2 + 'b'", "x = foo()+2+\"b\""); // don't fold!
15001537

1501-
// test("x = foo() + 'a' + 2", "x = foo()+\"a2\"");
1538+
// fold("x = foo() + 'a' + 2", "x = foo()+\"a2\"");
15021539
fold("x = '' + null", "x = 'null'");
15031540
fold("x = true + '' + false", "x = 'truefalse'");
1504-
// test("x = '' + []", "x = ''");
1505-
// test("x = foo() + 'a' + 1 + 1", "x = foo() + 'a11'");
1541+
// fold("x = '' + []", "x = ''");
1542+
// fold("x = foo() + 'a' + 1 + 1", "x = foo() + 'a11'");
15061543
fold("x = 1 + 1 + 'a'", "x = '2a'");
15071544
fold("x = 1 + 1 + 'a'", "x = '2a'");
15081545
fold("x = 'a' + (1 + 1)", "x = 'a2'");
1509-
// test("x = '_' + p1 + '_' + ('' + p2)", "x = '_' + p1 + '_' + p2");
1510-
// test("x = 'a' + ('_' + 1 + 1)", "x = 'a_11'");
1511-
// test("x = 'a' + ('_' + 1) + 1", "x = 'a_11'");
1512-
// test("x = 1 + (p1 + '_') + ('' + p2)", "x = 1 + (p1 + '_') + p2");
1513-
// test("x = 1 + p1 + '_' + ('' + p2)", "x = 1 + p1 + '_' + p2");
1514-
// test("x = 1 + 'a' + p1", "x = '1a' + p1");
1515-
// test("x = (p1 + (p2 + 'a')) + 'b'", "x = (p1 + (p2 + 'ab'))");
1516-
// test("'a' + ('b' + p1) + 1", "'ab' + p1 + 1");
1517-
// test("x = 'a' + ('b' + p1 + 'c')", "x = 'ab' + (p1 + 'c')");
1546+
// fold("x = '_' + p1 + '_' + ('' + p2)", "x = '_' + p1 + '_' + p2");
1547+
// fold("x = 'a' + ('_' + 1 + 1)", "x = 'a_11'");
1548+
// fold("x = 'a' + ('_' + 1) + 1", "x = 'a_11'");
1549+
// fold("x = 1 + (p1 + '_') + ('' + p2)", "x = 1 + (p1 + '_') + p2");
1550+
// fold("x = 1 + p1 + '_' + ('' + p2)", "x = 1 + p1 + '_' + p2");
1551+
// fold("x = 1 + 'a' + p1", "x = '1a' + p1");
1552+
// fold("x = (p1 + (p2 + 'a')) + 'b'", "x = (p1 + (p2 + 'ab'))");
1553+
// fold("'a' + ('b' + p1) + 1", "'ab' + p1 + 1");
1554+
// fold("x = 'a' + ('b' + p1 + 'c')", "x = 'ab' + (p1 + 'c')");
15181555
fold("void 0 + ''", "'undefined'");
15191556

15201557
fold_same("x = 'a' + (4 + p1 + 'a')");
@@ -1556,6 +1593,11 @@ mod test {
15561593
fold("x = null + 1", "x = 1");
15571594
}
15581595

1596+
#[test]
1597+
fn test_fold_sub() {
1598+
fold("x = 10 - 20", "x = -10");
1599+
}
1600+
15591601
#[test]
15601602
fn test_fold_multiply() {
15611603
fold_same("x = 2.25 * 3");

0 commit comments

Comments
 (0)