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

Code before:
        }
    }

    fn visit_mut_var_declarators(&mut self, n: &mut Vec<VarDeclarator>) {
        n.visit_mut_children_with(self);

        n.retain(|v| !v.name.is_invalid());
    }

    fn visit_mut_prop(&mut self, n: &mut Prop) {
        n.visit_mut_children_with(self);

        if let Prop::Shorthand(i) = n {
            if let Some(expr) = self.lits.get(&i.to_id()) {
                let key = if i.sym == "__proto__" {
                    PropName::Computed(ComputedPropName {
                        span: i.span,
                        expr: Box::new(Expr::Lit(Lit::Str(Str {
                            span: i.span,
                            value: i.sym.clone(),
                            raw: None,
                        }))),
                    })
                } else {
                    i.take().into()
                };
                *n = Prop::KeyValue(KeyValueProp {
                    key,
                    value: expr.clone(),
                });
                self.changed = true;
            }
        }
    }
}

pub(crate) struct NormalMultiReplacer<'a> {
    pub vars: &'a mut FxHashMap<Id, Box<Expr>>,
    pub changed: bool,
    should_consume: bool,
}

impl<'a> NormalMultiReplacer<'a> {
    /// `worked` will be changed to `true` if any replacement is done
    pub fn new(vars: &'a mut FxHashMap<Id, Box<Expr>>, should_consume: bool) -> Self {
        NormalMultiReplacer {
            vars,
            should_consume,
            changed: false,
        }
    }

Review comment (refactor):
The `__proto__` handling logic is duplicated across three different visitor implementations. Consider extracting this into a shared helper function to reduce code duplication and improve maintainability.
```suggestion
                let key = prop_name_from_ident(&i);
```

Code after:
        }
    }

    fn visit_mut_var_declarators(&mut self, n: &mut Vec<VarDeclarator>) {
        n.visit_mut_children_with(self);

        n.retain(|v| !v.name.is_invalid());
    }

    fn visit_mut_prop(&mut self, n: &mut Prop) {
        n.visit_mut_children_with(self);

        if let Prop::Shorthand(i) = n {
            if let Some(expr) = self.lits.get(&i.to_id()) {
                let key = prop_name_from_ident(i.take());
                *n = Prop::KeyValue(KeyValueProp {
                    key,
                    value: expr.clone(),
                });
                self.changed = true;
            }
        }
    }
}

pub(crate) struct NormalMultiReplacer<'a> {
    pub vars: &'a mut FxHashMap<Id, Box<Expr>>,
    pub changed: bool,
    should_consume: bool,
}

impl<'a> NormalMultiReplacer<'a> {
    /// `worked` will be changed to `true` if any replacement is done
    pub fn new(vars: &'a mut FxHashMap<Id, Box<Expr>>, should_consume: bool) -> Self {
        NormalMultiReplacer {
            vars,
            should_consume,
            changed: false,
        }
    }

    fn var(&mut self, i: &Id) -> Option<Box<Expr>> {
        let mut e = self.vars.remove(i)?;

        e.visit_mut_children_with(self);

        let e = if self.should_consume {
            e
        } else {
            let new_e = e.clone();
            self.vars.insert(i.clone(), e);