reluxscript 0.1.4

Write AST transformations once. Compile to Babel, SWC, and beyond.
Documentation
/**
 * Test: Null-Coalescing Operator (??)
 *
 * C# developers write:
 *   var name = node.Name ?? "default";
 *
 * Instead of:
 *   let name = node.name.unwrap_or("default");
 */
plugin NullCoalescing {

    struct State {
        result: Str,
    }

    fn init() -> State {
        State { result: "" }
    }

    pub fn visit_variable_declarator(node: &VariableDeclarator) {
        // Chained null coalescing with local Options
        let first_option: Option<Str> = Some("first".to_string());
        let second_option: Option<Str> = None;
        let value = first_option ?? second_option ?? "fallback".to_string();

        // Null coalescing with same type
        let opt1: Option<Str> = Some("a".to_string());
        let opt2: Option<Str> = Some("b".to_string());
        let result = opt1 ?? opt2 ?? "c".to_string();

        // Simple fallback
        let opt3: Option<Str> = None;
        let fallback_result = opt3 ?? "default".to_string();

        self.state.result = result;
    }

    fn finish() -> Str {
        self.state.result
    }
}