Repository: vercel/next.js
File: crates/next-error-code-swc-plugin/src/lib.rs (Rust)

Code before:
            .get(&ident.to_id())
            .cloned()
            .unwrap_or_else(|| "%s".to_string()),

        _ => "%s".to_string(),
    }
}

impl VisitMut for TransformVisitor {
    fn visit_mut_module(&mut self, module: &mut Module) {
        let mut collector = BindingCollector {
            bindings: FxHashMap::default(),
        };
        module.visit_with(&mut collector);
        self.resolved_bindings = collector.bindings;
        module.visit_mut_children_with(self);
    }

    fn visit_mut_script(&mut self, script: &mut Script) {
        let mut collector = BindingCollector {
            bindings: FxHashMap::default(),
        };
        script.visit_with(&mut collector);
        self.resolved_bindings = collector.bindings;
        script.visit_mut_children_with(self);
    }

    fn visit_mut_expr(&mut self, expr: &mut Expr) {
        let mut error_message: Option<String> = None;

        // The first arg to `Object.defineProperty(new Error(...), "__NEXT_ERROR_CODE", { value:
        // "$code", enumerable: false })`
        let mut new_error_expr: Option<NewExpr> = None;

        // Find expressions like `new Error(...)` or `Error(...)`
        // And populate `error_message` and `new_error_expr` when found
        match expr {
            Expr::New(new_expr) => match &*new_expr.callee {
                Expr::Ident(ident) if is_error_class_name(ident.sym.as_str()) => {
                    if let Some(args) = &new_expr.args {
                        if let Some(first_arg) = args.first() {
                            new_error_expr = Some(new_expr.clone());
                            error_message = Some(stringify_new_error_arg(
                                &first_arg.expr,
                                &self.resolved_bindings,
                            ));
                        }
                    }
                }
                _ => {}
            },

Review comment (suggestion):
this can be simplified
```
pub enum Program {
    Module(Module),
    Script(Script),
}
```


```suggestion
    fn visit_mut_program(&mut self, program: &mut Program) {
        let mut collector = BindingCollector {
            bindings: FxHashMap::default(),
        };
        program.visit_with(&mut collector);
        self.resolved_bindings = collector.bindings;
        program.visit_mut_children_with(self);
    }
```

Code after:
            .get(&ident.to_id())
            .cloned()
            .unwrap_or_else(|| "%s".to_string()),

        _ => "%s".to_string(),
    }
}

impl VisitMut for TransformVisitor {
    fn visit_mut_program(&mut self, program: &mut Program) {
        let mut collector = BindingCollector {
            bindings: FxHashMap::default(),
        };
        program.visit_with(&mut collector);
        self.resolved_bindings = collector.bindings;
        program.visit_mut_children_with(self);
    }

    fn visit_mut_expr(&mut self, expr: &mut Expr) {
        let mut error_message: Option<String> = None;

        // The first arg to `Object.defineProperty(new Error(...), "__NEXT_ERROR_CODE", { value:
        // "$code", enumerable: false })`
        let mut new_error_expr: Option<NewExpr> = None;

        // Find expressions like `new Error(...)` or `Error(...)`
        // And populate `error_message` and `new_error_expr` when found
        match expr {
            Expr::New(new_expr) => match &*new_expr.callee {
                Expr::Ident(ident) if is_error_class_name(ident.sym.as_str()) => {
                    if let Some(args) = &new_expr.args {
                        if let Some(first_arg) = args.first() {
                            new_error_expr = Some(new_expr.clone());
                            error_message = Some(stringify_new_error_arg(
                                &first_arg.expr,
                                &self.resolved_bindings,
                            ));
                        }
                    }
                }
                _ => {}
            },
            Expr::Call(call_expr) => match &call_expr.callee {
                Callee::Expr(expr) => match &**expr {
                    Expr::Ident(ident) if is_error_class_name(ident.sym.as_str()) => {
                        if let Some(first_arg) = call_expr.args.first() {
                            error_message = Some(stringify_new_error_arg(
                                &first_arg.expr,
                                &self.resolved_bindings,
                            ));