Skip to content

Commit

Permalink
fix(es/minifier): Don't break ternary with assignment in test (#6906)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #6903.
  • Loading branch information
kdy1 committed Feb 8, 2023
1 parent 7f7e21b commit 951dafb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
Expand Up @@ -4,7 +4,7 @@ use swc_common::{util::take::Take, EqIgnoreSpan, Spanned, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::ext::ExprRefExt;
use swc_ecma_transforms_optimization::debug_assert_valid;
use swc_ecma_utils::{ExprExt, ExprFactory, StmtExt, StmtLike};
use swc_ecma_utils::{ExprExt, ExprFactory, IdentUsageFinder, StmtExt, StmtLike};

use super::Optimizer;
use crate::{
Expand Down Expand Up @@ -316,6 +316,10 @@ where

/// Compress a conditional expression if cons and alt is simillar
pub(super) fn compress_cond_expr_if_similar(&mut self, e: &mut Expr) {
if !self.options.conditionals {
return;
}

let cond = match e {
Expr::Cond(expr) => expr,
_ => return,
Expand Down Expand Up @@ -368,6 +372,9 @@ where
}

let cons_callee = cons.callee.as_expr().and_then(|e| e.as_ident())?;
if IdentUsageFinder::find(&cons_callee.to_id(), &**test) {
return None;
}
//

if !cons.callee.eq_ignore_span(&alt.callee) {
Expand Down
63 changes: 63 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Expand Up @@ -10758,3 +10758,66 @@ fn issue_6899_5() {
false,
);
}

#[test]
fn issue_6903_1() {
run_default_exec_test(
r###"
function test(a, b) {
let wrapper = (e) => e
wrapper = (e) => (["not", e])
if (a) {
return wrapper(b)
}
return wrapper(1)
}
console.log(test(true, "bad"))
"###,
);
}

#[test]
fn issue_6903_2() {
run_exec_test(
r###"
function test(a, b) {
let wrapper = (e) => e
wrapper = (e) => (["not", e])
if (a) {
return wrapper(b)
}
return wrapper(1)
}
console.log(test(true, "bad"))
"###,
r###"
{
"if_return": true,
"join_vars": true,
"side_effects": true,
"conditionals": true
}"###,
false,
);
}

#[test]
fn issue_6903_3() {
run_exec_test(
r###"
function test(a, b) {
let wrapper = (e)=>e;
return ((wrapper = (e)=>[
"not",
e
]), a) ? wrapper(b) : wrapper(1);
}
console.log(test(true, "bad"));
"###,
r###"
{
"conditionals": true
}"###,
false,
);
}

0 comments on commit 951dafb

Please sign in to comment.