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

Code before:
                                }))),
                                PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
                                    key: PropName::Ident("configurable".into()),
                                    value: Box::new(Expr::Lit(Lit::Bool(Bool {
                                        span: new_error_expr.span,
                                        value: true,
                                    }))),
                                }))),
                            ],
                        })), // { value: "$code", enumerable: false }
                    },
                ],
                type_args: None,
                ctxt: new_error_expr.ctxt,
            });
        }
    }
}

#[plugin_transform]
pub fn process_transform(
    mut program: Program,
    _metadata: TransformPluginProgramMetadata,
) -> Program {
    let errors_json = fs::read_to_string("/cwd/errors.json")
        .unwrap_or_else(|e| panic!("failed to read errors.json: {}", e));
    let errors: FxHashMap<String, String> = serde_json::from_str(&errors_json)
        .unwrap_or_else(|e| panic!("failed to parse errors.json: {}", e));

    let mut visitor = TransformVisitor {
        errors,
        resolved_bindings: FxHashMap::default(),
    };

    visitor.visit_mut_program(&mut program);
    program
}

test_inline!(
    Default::default(),
    |_| visit_mut_pass(TransformVisitor {
        errors: FxHashMap::from_iter([
            ("1".to_string(), "Failed to fetch user %s: %s".to_string()),
            ("2".to_string(), "Request failed: %s".to_string()),
            ("3".to_string(), "Generic error".to_string()),
            ("4".to_string(), "Empty error".to_string()),
            (
                "5".to_string(),
                "Pattern should define hostname but found\n%s".to_string()
            ),
            (

Review comment (suggestion):
alternatively, you could also visit here before hand
```suggestion
    let mut collector = BindingCollector {
 	   bindings: FxHashMap::default(),
    };
    collector.visit_mut_program(&mut program);
    let mut visitor = TransformVisitor {
        errors,
        resolved_bindings: collector.resolved_bindings
    };
	visitor.visit_mut_program(&mut program);
```

Code after:
                        })), // { value: "$code", enumerable: false }
                    },
                ],
                type_args: None,
                ctxt: new_error_expr.ctxt,
            });
        }
    }
}

#[plugin_transform]
pub fn process_transform(
    mut program: Program,
    _metadata: TransformPluginProgramMetadata,
) -> Program {
    let errors_json = fs::read_to_string("/cwd/errors.json")
        .unwrap_or_else(|e| panic!("failed to read errors.json: {}", e));
    let errors: FxHashMap<String, String> = serde_json::from_str(&errors_json)
        .unwrap_or_else(|e| panic!("failed to parse errors.json: {}", e));

    let mut visitor = TransformVisitor {
        errors,
        resolved_bindings: FxHashMap::default(),
    };

    visitor.visit_mut_program(&mut program);
    program
}

test_inline!(
    Default::default(),
    |_| visit_mut_pass(TransformVisitor {
        errors: FxHashMap::from_iter([
            ("1".to_string(), "Failed to fetch user %s: %s".to_string()),
            ("2".to_string(), "Request failed: %s".to_string()),
            ("3".to_string(), "Generic error".to_string()),
            ("4".to_string(), "Empty error".to_string()),
            (
                "5".to_string(),
                "Pattern should define hostname but found\n%s".to_string()
            ),
            (
                "6".to_string(),
                "This is an extracted error message.".to_string()
            ),
        ]),
        resolved_bindings: FxHashMap::default(),
    }),
    realistic_api_handler,
    // Input codes
    r#"