Skip to content

Commit

Permalink
fix(es/minifier): Abort seq inliner on ** (#8947)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8942
  • Loading branch information
kdy1 authored May 13, 2024
1 parent 8baa0e5 commit 3046d71
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/swc_ecma_minifier/src/compress/optimize/dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl Optimizer<'_> {
if let Expr::Assign(assign) = e {
// x += 1 => x + 1
if let Some(op) = assign.op.to_update() {
if op == op!("**") {
return false;
}

if let Some(lhs) = assign.left.as_ident() {
//
if self
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,7 @@ impl Optimizer<'_> {
| Expr::Tpl(..)
| Expr::TaggedTpl(..) => return Ok(false),

// See https://github.com/swc-project/swc/issues/8924 and https://github.com/swc-project/swc/issues/8942
Expr::Assign(AssignExpr {
op: op!("**="),
right,
Expand All @@ -1572,7 +1573,7 @@ impl Optimizer<'_> {
right,
..
}) => {
if is_pure_undefined(&self.expr_ctx, right) {
if !right.is_lit() {
return Ok(false);
}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11224,6 +11224,21 @@ console.log(eval(foo));",
);
}

#[test]
fn issue_8942() {
run_default_exec_test(
"
'use strict';
const k = (() => {
let x = 1;
x **= undefined / x;
return x;
})();
console.log(k);
",
);
}

#[test]
fn issue_8937() {
run_default_exec_test(
Expand Down
5 changes: 4 additions & 1 deletion crates/swc_ecma_minifier/tests/fixture/issues/8924/output.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
"use strict";
const k = NaN;
const k = (()=>{
let x = 1;
return x **= void 0;
})();

0 comments on commit 3046d71

Please sign in to comment.