Skip to content

Commit

Permalink
9
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 20, 2025
1 parent 23e9daf commit dac84d5
Showing 1 changed file with 53 additions and 61 deletions.
114 changes: 53 additions & 61 deletions crates/oxc_minifier/src/ast_passes/statement_fusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,132 +196,124 @@ fn can_merge_block_stmt_member(node: &Statement) -> bool {
mod test {
use crate::tester::{test, test_same};

fn fuse(before: &str, after: &str) {
test(
&("function F(){if(CONDITION){".to_string() + before + "}}"),
&("function F(){if(CONDITION){".to_string() + after + "}}"),
);
}

fn fuse_same(code: &str) {
test_same(&("function F(){if(CONDITION){".to_string() + code + "}}"));
}
// fn test(before: &str, after: &str) {
// test(
// &("function F(){if(CONDITION){".to_string() + before + "}}"),
// &("function F(){if(CONDITION){".to_string() + after + "}}"),
// );
// }

#[test]
fn nothing_to_do() {
fuse_same("");
fuse_same("a");
fuse_same("a()");
fuse_same("if(a()){}");
}
// fn test_same(code: &str) {
// test_same(&("function F(){if(CONDITION){".to_string() + code + "}}"));
// }

#[test]
fn fold_block_with_statements() {
fuse("a;b;c", "a,b,c");
fuse("a();b();c();", "a(),b(),c()");
fuse("a(),b();c(),d()", "a(),b(),c(),d()");
fuse("a();b(),c(),d()", "a(),b(),c(),d()");
fuse("a(),b(),c();d()", "a(),b(),c(),d()");
test("a;b;c", "a,b,c");
test("a();b();c();", "a(),b(),c()");
test("a(),b();c(),d()", "a(),b(),c(),d()");
test("a();b(),c(),d()", "a(),b(),c(),d()");
test("a(),b(),c();d()", "a(),b(),c(),d()");
}

#[test]
fn fold_block_into_if() {
fuse("a;b;c;if(x){}", "if(a,b,c,x){}");
fuse("a;b;c;if(x,y){}else{}", "if(a,b,c,x,y){}else{}");
fuse("a;b;c;if(x,y){}", "if(a,b,c,x,y){}");
fuse("a;b;c;if(x,y,z){}", "if(a,b,c,x,y,z){}");
fuse("a();if(a()){}a()", "if(a(), a()){}a()");
test("a;b;c;if(x){}", "a,b,c,x");
test("a;b;c;if(x,y){}else{}", "a, b, c, !(x, y);");
test("a;b;c;if(x,y){}", "a, b, c, x, y");
test("a;b;c;if(x,y,z){}", "a, b, c, x, y, z");
test("a();if(a()){}a()", "a(), a(), a()");
}

#[test]
fn fold_block_return() {
fuse("a;b;c;return x", "return a,b,c,x");
fuse("a;b;c;return x+y", "return a,b,c,x+y");
fuse("a;b;c;return x;a;b;c", "return a,b,c,x;a,b,c");
test("a;b;c;return x", "return a,b,c,x");
test("a;b;c;return x+y", "return a,b,c,x+y");
test("a();b();c();return x();a();b();c()", "return a(),b(),c(),x()");
}

#[test]
fn fold_block_throw() {
fuse("a;b;c;throw x", "throw a,b,c,x");
fuse("a;b;c;throw x+y", "throw a,b,c,x+y");
fuse("a;b;c;throw x;a;b;c", "throw a,b,c,x;a,b,c");
test("a;b;c;throw x", "throw a,b,c,x");
test("a;b;c;throw x+y", "throw a,b,c,x+y");
test("a();b();c();throw x();a();b();c", "throw a(),b(),c(),x()");
}

#[test]
fn fold_switch() {
fuse("a;b;c;switch(x){}", "switch(a,b,c,x){}");
test("a;b;c;switch(x){}", "switch(a,b,c,x){}");
}

#[test]
fn fuse_into_for_in1() {
fuse("a;b;c;for(x in y){}", "for(x in a,b,c,y){}");
test("a;b;c;for(x in y){}", "for(x in a,b,c,y);");
}

#[test]
fn fuse_into_for_in2() {
fuse_same("a();for(var x = b() in y){}");
test_same("a();for(var x = b() in y);");
}

#[test]
fn fuse_into_vanilla_for1() {
fuse("a;b;c;for(;g;){}", "for(a,b,c;g;){}");
fuse("a;b;c;for(d;g;){}", "for(a,b,c,d;g;){}");
fuse("a;b;c;for(d,e;g;){}", "for(a,b,c,d,e;g;){}");
fuse_same("a();for(var x;g;){}");
test("a;b;c;for(;g;){}", "for(a,b,c;g;);");
test("a;b;c;for(d;g;){}", "for(a,b,c,d;g;);");
test("a;b;c;for(d,e;g;){}", "for(a,b,c,d,e;g;);");
test_same("a();for(var x;g;);");
}

#[test]
fn fuse_into_vanilla_for2() {
fuse("a;b;c;for(var d;g;){}", "a,b,c;for(var d;g;){}");
fuse("a;b;c;for(let d;g;){}", "a,b,c;for(let d;g;){}");
fuse("a;b;c;for(const d = 5;g;){}", "a,b,c;for(const d = 5;g;){}");
test("a;b;c;for(var d;g;){}", "a,b,c;for(var d;g;);");
test("a;b;c;for(let d;g;){}", "a,b,c;for(let d;g;);");
test("a;b;c;for(const d = 5;g;){}", "a,b,c;for(const d = 5;g;);");
}

#[test]
fn fuse_into_label() {
fuse("a;b;c;label:for(x in y){}", "label:for(x in a,b,c,y){}");
fuse("a;b;c;label:for(;g;){}", "label:for(a,b,c;g;){}");
fuse("a;b;c;l1:l2:l3:for(;g;){}", "l1:l2:l3:for(a,b,c;g;){}");
fuse("a;b;c;label:while(true){}", "label:for(a,b,c;true;){}");
test("a;b;c;label:for(x in y){}", "label:for(x in a,b,c,y);");
test("a;b;c;label:for(;g;){}", "label:for(a,b,c;g;);");
test("a;b;c;l1:l2:l3:for(;g;){}", "l1:l2:l3:for(a,b,c;g;);");
test("a;b;c;label:while(true){}", "label:for(a,b,c;;);");
}

#[test]
fn fuse_into_block() {
fuse("a;b;c;{d;e;f}", "{a,b,c,d,e,f}");
fuse(
test("a;b;c;{d;e;f}", "a,b,c,d,e,f");
test(
"a;b; label: { if(q) break label; bar(); }",
"label: { if(a,b,q) break label; bar(); }",
);
fuse("a;b;c;{var x;d;e;}", "a,b,c;{var x;d,e;}");
fuse("a;b;c;label:{break label;d;e;}", "a,b,c;label:{break label;d,e;}");
test("a;b;c;{var x;d;e;}", "a,b,c;{var x;d,e;}");
test("a;b;c;label:{break label;d;e;}", "a,b,c");
}

#[test]
fn fuse_into_switch_cases() {
fuse("switch (_) { case _: a; return b }", "switch (_) { case _: return a, b }");
test("switch (_) { case _: a; return b }", "switch (_) { case _: return a, b }");
}

#[test]
fn no_fuse_into_while() {
fuse("a;b;c;while(x){}", "for(a,b,c;x;){}");
test("a;b;c;while(x){}", "for(a,b,c;x;);");
}

#[test]
fn no_fuse_into_do() {
fuse("a;b;c;do{}while(x)", "a,b,c;do{}while(x)");
test("a;b;c;do;while(x)", "a,b,c;do;while(x)");
}

#[test]
fn no_fuse_into_block() {
// Never fuse a statement into a block that contains let/const/class declarations, or you risk
// colliding variable names. (unless the AST is normalized).
fuse("a; {b;}", "{a,b;}");
fuse("a; {b; var a = 1;}", "{a,b; var a = 1;}");
fuse_same("a; { b; let a = 1; }");
fuse_same("a; { b; const a = 1; }");
fuse_same("a; { b; class a {} }");
fuse_same("a; { b; function a() {} }");
fuse_same("a; { b; const otherVariable = 1; }");
test("a; {b;}", "a,b");
test("a; {b; var a = 1;}", "{a,b; var a = 1;}");
test_same("a; { b; let a = 1; }");
test_same("a; { b; const a = 1; }");
test_same("a; { b; class a {} }");
test_same("a; { b; function a() {} }");
test_same("a; { b; const otherVariable = 1; }");

// test(
// "function f(a) { if (COND) { a; { b; let a = 1; } } }",
Expand Down

0 comments on commit dac84d5

Please sign in to comment.