proto_method

Macro proto_method 

Source
macro_rules! proto_method {
    ($class:expr, $method:expr, $body:expr) => { ... };
}
Expand description

Creates a method assignment on a class prototype.

This macro generates code of the form:

ClassName.prototype.methodName = function() { /* body */ }

This is the standard pattern for adding methods to classes in JavaScript/TypeScript when you need to add methods outside the class declaration (e.g., in macro-generated code).

§Arguments

  • $class - An identifier expression for the class name (use ident!)
  • $method - A string literal for the method name
  • $body - A Vec<Stmt> containing the function body

§Example: Basic Usage

use macroforge_ts_syn::{proto_method, ident};

// Generate: MyClass.prototype.toString = function() { return "MyClass"; }
let body_stmts = vec![
    ts_quote!( return "MyClass"; as Stmt ),
];

let stmt = proto_method!(ident!("MyClass"), "toString", body_stmts);

§Example: In a Derive Macro

fn derive_debug(input: &DeriveInput) -> MacroResult {
    let class = input.as_class().expect("Expected class");
    let class_name = ident!(&input.name());

    // Build the debug() method body
    let body = vec![
        ts_quote!( return `${#class_name} { ... }`; as Stmt ),
    ];

    let method_stmt = proto_method!(class_name, "debug", body);

    // Return patch that inserts this method after the class
    MacroResult {
        runtime_patches: vec![
            Patch::InsertAfter {
                target: input.target_span(),
                code: method_stmt.into(),
                source_macro: Some("Debug".into()),
            }
        ],
        ..Default::default()
    }
}

§Under the Hood

This macro expands to:

fn_assign!(
    member_expr!(Expr::Ident(class), "prototype"),
    method_name,
    body
)