macroforge_ts_syn
TypeScript syntax types for compile-time macro code generation
Overview
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 communicationconfig- Serializable configuration types shared between host and macro processescontext_registry- Thread-local storage for the activeMacroContextIRdeclarative- Grammar and parser for declarative (pattern-matching) macrosderive- Derive input types that mirror Rust'ssyn::DeriveInputerrors- Error types and diagnostics for macro expansionimport_registry- Unified import registry built during IR loweringjsdoc- JSDoc directive parsing shared by both lowering backendslower- AST lowering from SWC types to IR representations (swcfeature)lower_oxc- AST lowering from OXC types to IR representations (oxcfeature)parse- TypeScript parsing utilities wrapping SWC (swcfeature)quote_helpers- Macros for ergonomic code generationstream- Parsing stream abstraction similar tosyn::parse::ParseBuffertype_normalize- Helpers for splitting TS type-string snippets into structural pieces
Architecture
The crate follows a layered architecture:
┌─────────────────────────────────────────────────────────────┐
│ User-Facing API │
│ (DeriveInput, TsStream, parse_ts_macro_input!) │
├─────────────────────────────────────────────────────────────┤
│ Lowering Layer │
│ (lower_classes_oxc / lower_classes, ...) │
├─────────────────────────────────────────────────────────────┤
│ IR Types (ABI Stable) │
│ (ClassIR, InterfaceIR, EnumIR, TypeAliasIR, ...) │
├─────────────────────────────────────────────────────────────┤
│ Parser Backend │
│ (OXC by default; SWC behind the `swc` feature) │
└─────────────────────────────────────────────────────────────┘
Usage Example
Here's how to use this crate in a derive macro:
use ;
// A typical derive macro entry point (normally annotated with
// `#[ts_macro_derive(...)]` from the `macroforge_ts_macros` crate)
Helper Macros
This crate provides several helper macros for working with SWC AST nodes:
ts_ident!- Create an identifier with optional formattingts_private_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
oxc- Enables the OXC parser backend (enabled by default)swc- Enables SWC integration and the SWC-based helper macros (opt-in)
Re-exports
For convenience, the crate re-exports commonly used SWC types when the swc feature is enabled (it
is not part of the default feature set):
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
Installation
Key Exports
Structs
StmtVec- A wrapper type for passing aVec<Stmt>to be used inline in function bodies.TsExpr- Wrapper forswc_core::ecma::ast::Exprthat implementsDisplayandToTsString.TsIdent- Wrapper forswc_core::ecma::ast::Identthat implementsDisplay.TsTypeWrapper- Wrapper forswc_core::ecma::ast::TsTypethat implementsDisplay.TsStmt- Wrapper forswc_core::ecma::ast::Stmtthat implementsDisplay.
Functions
to_ts_expr- Convert a value into a TypeScriptExpr.to_ts_type- Convert a value into a TypeScriptTsType.to_ts_ident- Convert a value into a TypeScriptIdent.to_ts_stmt- Convert a value into a TypeScriptStmt.expr_to_string- Converts an expression to its TypeScript string representation.type_to_string- Converts a type to its TypeScript string representation.ident_to_string- Converts an identifier to its TypeScript string representation.emit_module_items- Emits a list of module items to a TypeScript source string.emit_expr- Emits an expression to a string representation.emit_ts_type- Emits a TypeScript type to a string representation.- ... and 4 more
Traits
ToTsExpr- Converts common Rust values into SWCExprnodes.ToTsType- Converts common Rust values into SWCTsTypenodes.ToTsIdent- Converts common Rust values into SWCIdentnodes.ToTsStmt- Converts common Rust values into SWCStmtnodes.ToTsTypeName- Trait for converting values to type name strings.ToTsString- Trait for converting values to TypeScript string representations.
API Reference
See the full API documentation on docs.rs.
License
MIT