Skip to content

Commit

Permalink
fix(es/transforms/optimization): Fix dead_branch_remover (#1827)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_optimization:
 - Preserve side effects of a while statement even if it's a infinite loop. (#1825)
  • Loading branch information
kdy1 authored Jun 16, 2021
1 parent 4c8d68b commit b5a7a3f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ecmascript/transforms/optimization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_optimization"
repository = "https://github.com/swc-project/swc.git"
version = "0.22.0"
version = "0.22.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
18 changes: 11 additions & 7 deletions ecmascript/transforms/optimization/src/simplify/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,13 +843,17 @@ impl Fold for Remover {
Stmt::While(s) => {
if let (purity, Known(v)) = s.test.as_bool() {
if v {
Stmt::While(WhileStmt {
test: Box::new(Expr::Lit(Lit::Bool(Bool {
span: s.test.span(),
value: true,
}))),
..s
})
if purity.is_pure() {
Stmt::While(WhileStmt {
test: Box::new(Expr::Lit(Lit::Bool(Bool {
span: s.test.span(),
value: true,
}))),
..s
})
} else {
Stmt::While(s)
}
} else {
let body = s.body.extract_var_ids_as_var();
let body = body.map(Decl::Var).map(Stmt::Decl);
Expand Down
21 changes: 21 additions & 0 deletions ecmascript/transforms/optimization/src/simplify/branch/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1766,3 +1766,24 @@ fn return_function_hoisting() {
}",
);
}

#[test]
fn issue_1825() {
test(
"
function p(){
throw new Error('Something');
}
while ((p(), 1)) {
console.log('Hello world');
}
",
"
function p() {
throw new Error('Something');
}
while(p(), 1)console.log('Hello world');
",
);
}

0 comments on commit b5a7a3f

Please sign in to comment.