test(minifier): fix minimize condition tests (#7222)

This commit is contained in:
7086cmd 2024-11-11 08:36:19 +00:00
parent 080a67be16
commit 0d6a66aa9e

View file

@ -103,7 +103,7 @@ mod test {
fold("function f(){if(x){foo()}else{bar()}}", "function f(){x?foo():bar()}"); fold("function f(){if(x){foo()}else{bar()}}", "function f(){x?foo():bar()}");
// Try it out with properties and methods // Try it out with properties and methods
fold("function f(){if(x){a.b=1}}", "function f(){if(x)a.b=1}"); fold("function f(){if(x){a.b=1}}", "function f(){x&&(a.b=1)}");
fold("function f(){if(x){a.b*=1}}", "function f(){x&&(a.b*=1)}"); fold("function f(){if(x){a.b*=1}}", "function f(){x&&(a.b*=1)}");
fold("function f(){if(x){a.b+=1}}", "function f(){x&&(a.b+=1)}"); fold("function f(){if(x){a.b+=1}}", "function f(){x&&(a.b+=1)}");
fold("function f(){if(x){++a.b}}", "function f(){x&&++a.b}"); fold("function f(){if(x){++a.b}}", "function f(){x&&++a.b}");
@ -152,12 +152,9 @@ mod test {
"if(a)if(b){f1();f2()}else c&&f3();else d&&f4()", "if(a)if(b){f1();f2()}else c&&f3();else d&&f4()",
); );
fold("function f(){foo()}", "function f(){foo()}"); fold_same("function f(){foo()}");
fold("switch(x){case y: foo()}", "switch(x){case y:foo()}"); fold_same("switch(x){case y: foo()}");
fold( fold_same("try{foo()}catch(ex){bar()}finally{baz()}");
"try{foo()}catch(ex){bar()}finally{baz()}",
"try{foo()}catch(ex){bar()}finally{baz()}",
);
} }
/** Try to minimize returns */ /** Try to minimize returns */
@ -246,31 +243,40 @@ mod test {
// TODO(bradfordcsmith): Stop normalizing the expected output or document why it is necessary. // TODO(bradfordcsmith): Stop normalizing the expected output or document why it is necessary.
// enableNormalizeExpectedOutput(); // enableNormalizeExpectedOutput();
fold("if (a) { x = 1; x++ } else { x = 2; x++ }", "x=(a) ? 1 : 2; x++"); fold("if (a) { x = 1; x++ } else { x = 2; x++ }", "x=(a) ? 1 : 2; x++");
// fold( fold(
// "if (a) { x = 1; x++; y += 1; z = pi; }" + " else { x = 2; x++; y += 1; z = pi; }", concat!(
// "x=(a) ? 1 : 2; x++; y += 1; z = pi;", "if (a) { x = 1; x++; y += 1; z = pi; }",
// ); " else { x = 2; x++; y += 1; z = pi; }"
// fold( ),
// "function z() {" + "if (a) { foo(); return !0 } else { goo(); return !0 }" + "}", "x=(a) ? 1 : 2; x++; y += 1; z = pi;",
// "function z() {(a) ? foo() : goo(); return !0}", );
// ); fold(
// fold( concat!("function z() {", "if (a) { foo(); return !0 } else { goo(); return !0 }", "}"),
// "function z() {if (a) { foo(); x = true; return true " "function z() {(a) ? foo() : goo(); return !0}",
// + "} else { goo(); x = true; return true }}", );
// "function z() {(a) ? foo() : goo(); x = true; return true}", fold(
// ); concat!(
"function z() {if (a) { foo(); x = true; return true ",
"} else { goo(); x = true; return true }}"
),
"function z() {(a) ? foo() : goo(); x = true; return true}",
);
// fold( fold(
// "function z() {" concat!(
// + " if (a) { bar(); foo(); return true }" "function z() {",
// + " else { bar(); goo(); return true }" " if (a) { bar(); foo(); return true }",
// + "}", " else { bar(); goo(); return true }",
// "function z() {" "}"
// + " if (a) { bar(); foo(); }" ),
// + " else { bar(); goo(); }" concat!(
// + " return true;" "function z() {",
// + "}", " if (a) { bar(); foo(); }",
// ); " else { bar(); goo(); }",
" return true;",
"}"
),
);
} }
#[test] #[test]
@ -647,28 +653,43 @@ mod test {
fold_same("function f() { while(1) { return 7} return 5}"); fold_same("function f() { while(1) { return 7} return 5}");
// fold_same("function f() {" + " try { while(x) {return f()}} catch (e) { } return f()}"); fold_same(concat!(
"function f() {",
" try { while(x) {return f()}} catch (e) { } return f()}"
));
// fold_same("function f() {" + " try { while(x) {return f()}} finally {alert(1)} return f()}"); fold_same(concat!(
"function f() {",
" try { while(x) {return f()}} finally {alert(1)} return f()}"
));
// Both returns has the same handler // Both returns has the same handler
// fold( fold(
// "function f() {" + " try { while(x) { return f() } return f() } catch (e) { } }", concat!(
// "function f() {" + " try { while(x) { break } return f() } catch (e) { } }", "function f() {",
// ); " try { while(x) { return f() } return f() } catch (e) { } }"
),
concat!("function f() {", " try { while(x) { break } return f() } catch (e) { } }"),
);
// We can't fold this because it'll change the order of when foo is called. // We can't fold this because it'll change the order of when foo is called.
// fold_same( fold_same(concat!(
// "function f() {" "function f() {",
// + " try { while(x) { return foo() } } finally { alert(1) } " " try { while(x) { return foo() } } finally { alert(1) } ",
// + " return foo()}", " return foo()}"
// ); ));
// This is fine, we have no side effect in the return value. // This is fine, we have no side effect in the return value.
// fold( fold(
// "function f() {" + " try { while(x) { return 1 } } finally { alert(1) } return 1}", concat!(
// "function f() {" + " try { while(x) { break } } finally { alert(1) } return 1}", "function f() {",
// ); " try { while(x) { return 1 } } finally { alert(1) } return 1}"
),
concat!(
"function f() {",
" try { while(x) { break } } finally { alert(1) } return 1}"
),
);
fold_same("function f() { try{ return a } finally { a = 2 } return a; }"); fold_same("function f() { try{ return a } finally { a = 2 } return a; }");
@ -738,28 +759,40 @@ mod test {
fold_same("function f() { while(1) { throw 7} throw 5}"); fold_same("function f() { while(1) { throw 7} throw 5}");
// fold_same("function f() {" + " try { while(x) {throw f()}} catch (e) { } throw f()}"); fold_same(concat!(
"function f() {",
" try { while(x) {throw f()}} catch (e) { } throw f()}"
));
// fold_same("function f() {" + " try { while(x) {throw f()}} finally {alert(1)} throw f()}"); fold_same(concat!(
"function f() {",
" try { while(x) {throw f()}} finally {alert(1)} throw f()}"
));
// Both throws has the same handler // Both throws has the same handler
// fold( fold(
// "function f() {" + " try { while(x) { throw f() } throw f() } catch (e) { } }", concat!("function f() {", " try { while(x) { throw f() } throw f() } catch (e) { } }"),
// "function f() {" + " try { while(x) { break } throw f() } catch (e) { } }", concat!("function f() {", " try { while(x) { break } throw f() } catch (e) { } }"),
// ); );
// We can't fold this because it'll change the order of when foo is called. // We can't fold this because it'll change the order of when foo is called.
// fold_same( fold_same(concat!(
// "function f() {" "function f() {",
// + " try { while(x) { throw foo() } } finally { alert(1) } " " try { while(x) { throw foo() } } finally { alert(1) } ",
// + " throw foo()}", " throw foo()}"
// ); ));
// This is fine, we have no side effect in the throw value. // This is fine, we have no side effect in the throw value.
// fold( fold(
// "function f() {" + " try { while(x) { throw 1 } } finally { alert(1) } throw 1}", concat!(
// "function f() {" + " try { while(x) { break } } finally { alert(1) } throw 1}", "function f() {",
// ); " try { while(x) { throw 1 } } finally { alert(1) } throw 1}"
),
concat!(
"function f() {",
" try { while(x) { break } } finally { alert(1) } throw 1}"
),
);
fold_same("function f() { try{ throw a } finally { a = 2 } throw a; }"); fold_same("function f() { try{ throw a } finally { a = 2 } throw a; }");
@ -798,10 +831,13 @@ mod test {
"function f() { switch(a){ case 1: } return a; }", "function f() { switch(a){ case 1: } return a; }",
); );
// fold( fold(
// "function f() { switch(a){ " + " case 1: return a; case 2: return a; } return a; }", concat!(
// "function f() { switch(a){ " + " case 1: break; case 2: } return a; }", "function f() { switch(a){ ",
// ); " case 1: return a; case 2: return a; } return a; }"
),
concat!("function f() { switch(a){ ", " case 1: break; case 2: } return a; }"),
);
} }
#[test] #[test]
@ -835,10 +871,10 @@ mod test {
"function f() { switch(a){ case 1: } throw a; }", "function f() { switch(a){ case 1: } throw a; }",
); );
// fold( fold(
// "function f() { switch(a){ " + "case 1: throw a; case 2: throw a; } throw a; }", concat!("function f() { switch(a){ ", "case 1: throw a; case 2: throw a; } throw a; }"),
// "function f() { switch(a){ case 1: break; case 2: } throw a; }", concat!("function f() { switch(a){ case 1: break; case 2: } throw a; }"),
// ); );
} }
#[test] #[test]
@ -872,10 +908,20 @@ mod test {
#[test] #[test]
#[ignore] #[ignore]
fn test_remove_else_cause() { fn test_remove_else_cause() {
// test( test(
// "function f() {" + " if(x) return 1;" + " else if(x) return 2;" + " else if(x) return 3 }", concat!(
// "function f() {" + " if(x) return 1;" + "{ if(x) return 2;" + "{ if(x) return 3 } } }", "function f() {",
// ); " if(x) return 1;",
" else if(x) return 2;",
" else if(x) return 3 }"
),
concat!(
"function f() {",
" if(x) return 1;",
"{ if(x) return 2;",
"{ if(x) return 3 } } }"
),
);
} }
#[test] #[test]
@ -916,26 +962,32 @@ mod test {
#[test] #[test]
#[ignore] #[ignore]
fn test_issue925() { fn test_issue925() {
// test( test(
// "if (x[--y] === 1) {\n" + " x[y] = 0;\n" + "} else {\n" + " x[y] = 1;\n" + "}", concat!(
// "(x[--y] === 1) ? x[y] = 0 : x[y] = 1;", "if (x[--y] === 1) {\n",
// ); " x[y] = 0;\n",
"} else {\n",
" x[y] = 1;\n",
"}"
),
"(x[--y] === 1) ? x[y] = 0 : x[y] = 1;",
);
// test( test(
// "if (x[--y]) {\n" + " a = 0;\n" + "} else {\n" + " a = 1;\n" + "}", concat!("if (x[--y]) {\n", " a = 0;\n", "} else {\n", " a = 1;\n", "}"),
// "a = (x[--y]) ? 0 : 1;", "a = (x[--y]) ? 0 : 1;",
// ); );
// test( test(
// lines( concat!(
// "if (x?.[--y]) {", // "if (x?.[--y]) {", //
// " a = 0;", " a = 0;",
// "} else {", "} else {",
// " a = 1;", " a = 1;",
// "}", "}",
// ), ),
// "a = (x?.[--y]) ? 0 : 1;", "a = (x?.[--y]) ? 0 : 1;",
// ); );
test("if (x++) { x += 2 } else { x += 3 }", "x++ ? x += 2 : x += 3"); test("if (x++) { x += 2 } else { x += 3 }", "x++ ? x += 2 : x += 3");
@ -1014,8 +1066,8 @@ mod test {
#[ignore] #[ignore]
fn test_coercion_substitution_hook() { fn test_coercion_substitution_hook() {
// enableTypeCheck(); // enableTypeCheck();
// test_same(lines("var x = {};", "var y = x != null ? 1 : 2;")); test_same(concat!("var x = {};", "var y = x != null ? 1 : 2;"));
// test_same(lines("var x = 1;", "var y = x != 0 ? 1 : 2;")); test_same(concat!("var x = 1;", "var y = x != 0 ? 1 : 2;"));
} }
#[test] #[test]
@ -1091,17 +1143,17 @@ mod test {
#[ignore] #[ignore]
fn test_minimize_if_with_new_target_condition() { fn test_minimize_if_with_new_target_condition() {
// Related to https://github.com/google/closure-compiler/issues/3097 // Related to https://github.com/google/closure-compiler/issues/3097
// test( test(
// lines( concat!(
// "function x() {", "function x() {",
// " if (new.target) {", " if (new.target) {",
// " return 1;", " return 1;",
// " } else {", " } else {",
// " return 2;", " return 2;",
// " }", " }",
// "}", "}",
// ), ),
// lines("function x() {", " return new.target ? 1 : 2;", "}"), concat!("function x() {", " return new.target ? 1 : 2;", "}"),
// ); );
} }
} }