Repository: swc-project/swc
File: crates/swc_ecma_minifier/src/compress/optimize/inline.rs (Rust)

Code before:
fn is_arrow_simple_enough_for_copy(e: &ArrowExpr) -> Option<u8> {
    if e.is_async {
        return None;
    }

    match &*e.body {
        BlockStmtOrExpr::BlockStmt(s) => is_block_stmt_of_fn_simple_enough_for_copy(s),
        BlockStmtOrExpr::Expr(e) => is_arrow_body_simple_enough_for_copy(e),
    }
}

fn is_arrow_body_simple_enough_for_copy(e: &Expr) -> Option<u8> {
    match e {
        Expr::Ident(..) | Expr::Lit(..) => return Some(1),
        Expr::Member(MemberExpr { obj, prop, .. }) if !prop.is_computed() => {
            return Some(is_arrow_body_simple_enough_for_copy(obj)? + 2)
        }
        Expr::Call(c) => {
            let mut arg_cost = 0;
            for arg in &c.args {
                let cost = is_arrow_body_simple_enough_for_copy(&arg.expr)?;
                arg_cost += cost;
                arg_cost += if arg.spread.is_some() { 3 } else { 0 };
            }

            return Some(arg_cost + 2);
        }
        Expr::Unary(u) => return Some(is_arrow_body_simple_enough_for_copy(&u.arg)? + 1),

        Expr::Bin(b) => {
            return Some(
                is_arrow_body_simple_enough_for_copy(&b.left)?
                    + is_arrow_body_simple_enough_for_copy(&b.right)?
                    + 2,
            )
        }
        _ => {}
    }

    None
}

fn is_block_stmt_of_fn_simple_enough_for_copy(b: &BlockStmt) -> Option<u8> {
    if b.stmts.len() == 1 {
        if let Stmt::Return(ret) = &b.stmts[0] {
            return ret
                .arg
                .as_deref()
                .map_or(Some(0), is_arrow_body_simple_enough_for_copy);
        }
    }

Review comment (performance):
The function doesn't include the cost of the callee expression itself in the calculation. This could lead to inaccurate cost estimation for complex call expressions like `obj.method.call()` where the callee has significant complexity.
```suggestion
            let callee_cost = is_arrow_body_simple_enough_for_copy(&c.callee)?;
            return Some(arg_cost + callee_cost + 2);
```

Code after:
}

fn is_arrow_simple_enough_for_copy(e: &ArrowExpr) -> Option<u8> {
    if e.is_async {
        return None;
    }

    match &*e.body {
        BlockStmtOrExpr::BlockStmt(s) => is_block_stmt_of_fn_simple_enough_for_copy(s),
        BlockStmtOrExpr::Expr(e) => is_arrow_body_simple_enough_for_copy(e),
    }
}

fn is_arrow_body_simple_enough_for_copy(e: &Expr) -> Option<u8> {
    match e {
        Expr::Ident(..) | Expr::Lit(..) => return Some(1),
        Expr::Member(MemberExpr { obj, prop, .. }) if !prop.is_computed() => {
            return Some(is_arrow_body_simple_enough_for_copy(obj)? + 2)
        }
        Expr::Call(c) => {
            let mut cost = is_arrow_body_simple_enough_for_copy(c.callee.as_expr()?.deref())?;
            for arg in &c.args {
                let arg_cost = is_arrow_body_simple_enough_for_copy(&arg.expr)?;
                cost += arg_cost;
                cost += if arg.spread.is_some() { 3 } else { 0 };
            }

            return Some(cost + 2);
        }
        Expr::Unary(u) => return Some(is_arrow_body_simple_enough_for_copy(&u.arg)? + 1),

        Expr::Bin(b) => {
            return Some(
                is_arrow_body_simple_enough_for_copy(&b.left)?
                    + is_arrow_body_simple_enough_for_copy(&b.right)?
                    + 2,
            )
        }
        _ => {}
    }

    None
}

fn is_block_stmt_of_fn_simple_enough_for_copy(b: &BlockStmt) -> Option<u8> {
    if b.stmts.len() == 1 {
        if let Stmt::Return(ret) = &b.stmts[0] {
            return ret
                .arg
                .as_deref()
                .map_or(Some(0), is_arrow_body_simple_enough_for_copy);