feat(es/transforms/optimization): simplify: Preserve do-while loops with conditional stoppers (#1618)

This commit is contained in:
Will Binns-Smith 2021-04-27 18:14:13 -07:00 committed by GitHub
parent a24266d986
commit f943021de0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -851,6 +851,10 @@ impl Fold for Remover {
}
Stmt::DoWhile(s) => {
if has_conditional_stopper(&[Stmt::DoWhile(s.clone())]) {
return Stmt::DoWhile(s);
}
if let Known(v) = s.test.as_pure_bool() {
if v {
// `for(;;);` is shorter than `do ; while(true);`

View File

@ -307,6 +307,12 @@ fn test_fold_useless_do_extra() {
);
}
#[test]
fn test_no_fold_do_with_conditional_stopper() {
test_same("do { if (Date.now() > 0) break; } while (0);");
test_same("do { if (Date.now() > 0) continue; } while (0);");
}
#[test]
fn test_fold_empty_do() {
test("do { } while(true);", "for (;;);");