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

Code before:
                return;
            }

            let ctx = self
                .ctx
                .clone()
                .with(BitCtx::InFnLike, true)
                .with(BitCtx::TopLevel, false);
            let mut optimizer = self.with_ctx(ctx);

            trace_op!("inline: Inlining arguments");
            optimizer.inline_vars_in_node(callee, vars);

            e.args.retain(|a| !a.expr.is_invalid());

            fn is_invalid_pat(p: &Pat) -> bool {
                match p {
                    Pat::Invalid(_) => true,
                    Pat::Ident(i) => i.id.is_dummy(),
                    Pat::Rest(r) => is_invalid_pat(&r.arg),
                    _ => false,
                }
            }

            match callee {
                Expr::Arrow(callee) => {
                    // Drop invalid nodes
                    callee.params.retain(|p| !is_invalid_pat(p))
                }
                Expr::Fn(callee) => {
                    // Drop invalid nodes
                    callee.function.params.retain(|p| !is_invalid_pat(&p.pat))
                }
                _ => {}
            };
        }
    }

    /// If a parameter is not used, we can ignore return value of the
    /// corresponding argument.
    pub(super) fn ignore_unused_args_of_iife(&mut self, e: &mut CallExpr) {
        if !self.options.unused && !self.options.reduce_vars {
            return;
        }

        let callee = match &mut e.callee {
            Callee::Super(_) | Callee::Import(_) => return,
            Callee::Expr(e) => &mut **e,
            #[cfg(swc_ast_unknown)]
            _ => panic!("unable to access unknown nodes"),
        };

Review comment (suggestion):
Returning `true` when `earlier_param` is `None` (line 317) means the code conservatively assumes a dependency exists when it cannot determine the parameter identity. While this is safe, consider adding a comment explaining this conservative approach to make the logic clearer for future maintainers.

Code after:
                                param.take();
                            } else {
                                trace_op!(
                                    "iife: Trying to inline default param argument ({}{:?}) (not \
                                     inlinable)",
                                    param_id.id.sym,
                                    param_id.id.ctxt
                                );
                            }
                            continue;
                        }

                        if assign_pat.right.may_have_side_effects(self.ctx.expr_ctx) {
                            continue;
                        }

                        // `b = a` depends on parameter evaluation order. We only inline
                        // defaults that are independent of earlier parameters.
                        let references_earlier_param =
                            param_idents.iter().take(idx).any(|earlier_param| {
                                let Some(earlier_param) = earlier_param else {
                                    return true;
                                };

                                contains_ident_ref(&*assign_pat.right, earlier_param)
                            });
                        if references_earlier_param {
                            continue;
                        }

                        trace_op!(
                            "iife: Trying to inline default param value ({}{:?})",
                            param_id.id.sym,
                            param_id.id.ctxt
                        );
                        vars.insert(param_id.to_id(), Box::new((*assign_pat.right).clone()));
                        param.take();
                    }

                    Pat::Rest(rest_pat) => {
                        if let Pat::Ident(param_id) = &mut *rest_pat.arg {
                            if let Some(usage) = self.data.vars.get(&param_id.to_id()) {
                                if usage.flags.contains(VarUsageInfoFlags::REASSIGNED)
                                    || usage.ref_count != 1
                                    || !usage.flags.contains(VarUsageInfoFlags::HAS_PROPERTY_ACCESS)
                                {
                                    continue;
                                }

                                if e.args.iter().skip(idx).any(|arg| {
                                    if arg.spread.is_some() {