fn_expr

Macro fn_expr 

Source
macro_rules! fn_expr {
    ($body_stmts:expr) => { ... };
    ($params:expr, $body_stmts:expr) => { ... };
}
Expand description

Creates an anonymous function expression with the given body statements.

This macro generates a FnExpr representing function() { ... } or function(params) { ... } in JavaScript/TypeScript.

§Variants

  • fn_expr!(body_stmts) - Creates a function with no parameters
  • fn_expr!(params, body_stmts) - Creates a function with the specified parameters

§Examples

Function with no parameters:

use macroforge_ts_syn::{fn_expr, parse_ts_str};
use swc_core::ecma::ast::Stmt;

let body: Vec<Stmt> = vec![
    parse_ts_str("return 42;").unwrap()
];

let func = fn_expr!(body);
// Generates: function() { return 42; }

Function with parameters:

use macroforge_ts_syn::{fn_expr, ident};
use swc_core::ecma::ast::{Param, Pat, Stmt};

let params = vec![
    Param {
        span: DUMMY_SP,
        decorators: vec![],
        pat: Pat::Ident(ident!("x").into()),
    }
];
let body: Vec<Stmt> = vec![/* ... */];

let func = fn_expr!(params, body);
// Generates: function(x) { ... }