Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Don't break ternary with assignment in test #6906

Merged
merged 7 commits into from Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
);
}