Expand description
§Macroforge TypeScript Macro Engine
This crate provides a TypeScript macro expansion engine that brings Rust-like derive macros to TypeScript. It is designed to be used via NAPI bindings from Node.js, enabling compile-time code generation for TypeScript projects.
§Overview
Macroforge processes TypeScript source files containing @derive decorators and expands them
into concrete implementations. For example, a class decorated with @derive(Debug, Clone)
will have toString() and clone() methods automatically generated.
§Architecture
The crate is organized into several key components:
- NAPI Bindings (
NativePlugin,expand_sync,transform_sync): Entry points for Node.js - Position Mapping (
NativePositionMapper,NativeMapper): Bidirectional source mapping for IDE integration - Macro Host (
hostmodule): Core expansion engine with registry and dispatcher - Built-in Macros (
builtinmodule): Standard derive macros (Debug, Clone, Serialize, etc.)
§Performance Considerations
- Uses a 32MB thread stack to prevent stack overflow during deep SWC AST recursion
- Implements early bailout for files without
@derivedecorators - Caches expansion results keyed by filepath and version
- Uses binary search for O(log n) position mapping lookups
§Usage from Node.js
const { NativePlugin, expand_sync } = require('macroforge-ts');
// Create a plugin instance with caching
const plugin = new NativePlugin();
// Process a file (uses cache if version matches)
const result = plugin.process_file(filepath, code, { version: '1.0' });
// Or use the sync function directly
const result = expand_sync(code, filepath, { keep_decorators: false });§Re-exports for Macro Authors
This crate re-exports several dependencies for convenience when writing custom macros:
ts_syn: TypeScript syntax types for AST manipulationmacros: Macro attributes and quote templatesswc_core,swc_common,swc_ecma_ast: SWC compiler infrastructure
Re-exports§
pub extern crate inventory;pub extern crate macroforge_ts_macros;pub extern crate macroforge_ts_quote;pub extern crate macroforge_ts_syn;pub extern crate napi;pub extern crate napi_derive;pub extern crate serde_json;
Re-exports§
pub use macroforge_ts_syn as ts_syn;pub use macroforge_ts_syn::swc_core;
Modules§
- abi
- Application Binary Interface (ABI) types for stable macro communication.
- builtin
- Built-in Derive Macros for Macroforge
- host
- TypeScript Macro Host
- macros
- Macro attributes and quote templates
Use:
use macroforge_ts::macros::*; - swc_
common - swc_
ecma_ ast
Macros§
- register_
macro_ package - Registers a macro package with the global package registry.
Structs§
- Decorator
Manifest Entry - Entry for a registered decorator in the manifest.
- Expand
Options - Options for macro expansion.
- Expand
Result - Result of expanding macros in TypeScript source code.
- Generated
Region Result - A region in the expanded source that was generated by a macro.
- Import
Source Result - Information about an imported identifier from a TypeScript module.
- JsDiagnostic
- A diagnostic from the TypeScript/JavaScript compiler or IDE.
- Load
Config Result - Result of loading a macroforge configuration file.
- Macro
Diagnostic - A diagnostic message produced during macro expansion.
- Macro
Manifest - Complete manifest of all available macros and decorators.
- Macro
Manifest Entry - Entry for a registered macro in the manifest.
- Mapping
Segment Result - A segment mapping a range in the original source to a range in the expanded source.
- Native
Mapper - Wrapper around
NativePositionMapperfor NAPI compatibility. - Native
Plugin - The main plugin class for macro expansion with caching support.
- Native
Position Mapper - Bidirectional position mapper for translating between original and expanded source positions.
- Process
File Options - Options for processing a file through the macro system.
- Source
Mapping Result - Complete source mapping information for a macro expansion.
- Span
Result - A span (range) in source code, represented as start position and length.
- Syntax
Check Result - Result of checking TypeScript syntax validity.
- Transform
Result - Result of transforming TypeScript code through the macro system.
Functions§
- check_
syntax - Checks if the given TypeScript code has valid syntax.
- clear_
config_ cache - Clears the configuration cache.
- debug_
descriptors - Returns debug information about all registered macro descriptors (debug API).
- debug_
get_ modules - Returns all registered macro module names (debug API).
- debug_
lookup - Looks up a macro by module and name (debug API).
- derive_
decorator - The
@Derivedecorator function exported to JavaScript/TypeScript. - expand_
sync - Synchronously expands macros in TypeScript code.
- get_
macro_ manifest - Returns the complete manifest of all registered macros and decorators.
- get_
macro_ names - Returns the names of all registered macros.
- is_
macro_ package - Checks if any macros are registered in this package.
- load_
config - Load and parse a macroforge configuration file.
- parse_
import_ sources - Parses import statements from TypeScript code and returns their sources.
- transform_
sync - Synchronously transforms TypeScript code through the macro expansion system.