Expand description
§macroforge_ts_syn
TypeScript syntax types for compile-time macro code generation.
This crate provides a syn-like API for parsing and manipulating
TypeScript code, enabling macro authors to work with TypeScript AST in a familiar way.
It is the core infrastructure crate for the Macroforge TypeScript macro system.
§Overview
The crate is organized into several modules:
abi- Application Binary Interface types for stable macro communication- [
derive] - Derive input types that mirror Rust’ssyn::DeriveInput errors- Error types and diagnostics for macro expansionlower- AST lowering from SWC types to IR representationsparse- TypeScript parsing utilities wrapping SWCquote_helpers- Macros for ergonomic code generationstream- Parsing stream abstraction similar tosyn::parse::ParseBuffer
§Architecture
The crate follows a layered architecture:
┌─────────────────────────────────────────────────────────────┐
│ User-Facing API │
│ (DeriveInput, TsStream, parse_ts_macro_input!) │
├─────────────────────────────────────────────────────────────┤
│ Lowering Layer │
│ (lower_classes, lower_interfaces, lower_enums, ...) │
├─────────────────────────────────────────────────────────────┤
│ IR Types (ABI Stable) │
│ (ClassIR, InterfaceIR, EnumIR, TypeAliasIR, ...) │
├─────────────────────────────────────────────────────────────┤
│ SWC Parser │
│ (swc_core for TypeScript/JavaScript parsing) │
└─────────────────────────────────────────────────────────────┘§Usage Example
Here’s how to use this crate in a derive macro:
ⓘ
use macroforge_ts_syn::{parse_ts_macro_input, DeriveInput, MacroResult, Patch};
pub fn my_derive_macro(ctx: MacroContextIR) -> MacroResult {
// Parse the input using the syn-like API
let input = parse_ts_macro_input!(ctx);
// Access type information
println!("Processing type: {}", input.name());
// Match on the type kind
match &input.data {
Data::Class(class) => {
for field in class.fields() {
println!("Field: {}", field.name);
}
}
Data::Interface(iface) => {
// Handle interface...
}
Data::Enum(enum_) => {
// Handle enum...
}
Data::TypeAlias(alias) => {
// Handle type alias...
}
}
// Generate code and return patches
MacroResult::ok()
}§Helper Macros
This crate provides several helper macros for working with SWC AST nodes:
ident!- Create an identifier with optional formattingprivate_ident!- Create a private (marked) identifierstmt_block!- Create a block statement from statementsfn_expr!- Create an anonymous function expressionmember_expr!- Create a member access expression (obj.prop)assign_stmt!- Create an assignment statementfn_assign!- Create a function assignment (obj.prop = function() {…})proto_method!- Create a prototype method assignment
§Feature Flags
swc- Enables SWC integration and the helper macros (enabled by default)
§Re-exports
For convenience, the crate re-exports commonly used SWC types when the swc feature
is enabled:
swc_core- The full SWC core crateswc_common- Common SWC types (Span, SourceMap, etc.)swc_ecma_ast- ECMAScript/TypeScript AST typesquote!- SWC’s quote macro for AST generation
Re-exports§
pub use swc_core;pub use abi::*;pub use derive::*;pub use errors::*;pub use lower::*;pub use stream::*;
Modules§
- abi
- Application Binary Interface (ABI) types for stable macro communication.
- derive
syn-like derive input types for TypeScript macros.- errors
- Error types for the macro system.
- lower
- AST lowering from SWC types to IR representations.
- parse
- Low-level TypeScript parsing utilities.
- quote
- quote_
helpers - Helper macros for ergonomic code generation.
- stream
- A
syn-like parsing API for TypeScript using SWC. - swc_
common - swc_
ecma_ ast
Macros§
- assign_
stmt - Creates an assignment expression statement (
lhs = rhs;). - fn_
assign - Creates a function assignment statement (
obj.prop = function(params) { body };). - fn_expr
- Creates an anonymous function expression with the given body statements.
- ident
- Creates an SWC
Identwith a dummy span. - member_
expr - Creates a member access expression (
obj.prop). - parse_
ts_ macro_ input - Parse a
TsStreaminto aDeriveInput, returning early with an errorMacroResulton failure. - private_
ident - Creates a private (marked) SWC
Ident. - proto_
method - Creates a method assignment on a class prototype.
- quote
- Supported output types
- stmt_
block - Creates a block statement (
{ ... }) from a vector of statements. - stmt_
block_ from_ vec - Converts a
Vec<Stmt>into a single block statement for use ints_quote!. - stmt_
vec - Wraps a
Vec<Stmt>in aStmtVecfor use withts_quote!.
Structs§
- StmtVec
- A wrapper type for passing a
Vec<Stmt>to be used inline in function bodies.