Skip to main content

Crate macroforge_ts_syn

Crate macroforge_ts_syn 

Source
Expand description

§macroforge_ts_syn

TypeScript syntax types for build-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
  • ast - Source-backed expression and identifier values for templates
  • config - Serializable configuration types shared between host and macro processes
  • context_registry - Thread-local storage for the active MacroContextIR
  • declarative - Grammar and parser for declarative (pattern-matching) macros
  • [derive] - Derive input types that mirror Rust’s syn::DeriveInput
  • errors - Error types and diagnostics for macro expansion
  • import_registry - Unified import registry built during IR lowering
  • jsdoc - JSDoc directive parsing used by lowering
  • lower - AST lowering from OXC types to IR representations
  • stream - Parsing stream abstraction similar to syn::parse::ParseBuffer
  • type_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, lower_interfaces, ...)                     │
├─────────────────────────────────────────────────────────────┤
│                    IR Types (ABI Stable)                    │
│  (ClassIR, InterfaceIR, EnumIR, TypeAliasIR, ...)           │
├─────────────────────────────────────────────────────────────┤
│                    Parser Backend                           │
│  (OXC)                                                      │
└─────────────────────────────────────────────────────────────┘

§Usage Example

Here’s how to use this crate in a derive macro:

use macroforge_ts_syn::{parse_ts_macro_input, Data, DeriveInput, MacroforgeError, TsStream};

// A typical derive macro entry point (normally annotated with
// `#[ts_macro_derive(...)]` from the `macroforge_ts_macros` crate)
pub fn my_derive_macro(mut input: TsStream) -> Result<TsStream, MacroforgeError> {
    // Parse the input using the syn-like API
    let input = parse_ts_macro_input!(input as DeriveInput);

    // 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...
        }
    }

    // Return the generated code as a TsStream
    Ok(TsStream::from_string(String::new()))
}

§Re-exports

  • oxc - The OXC crate, for macros that work on the parsed AST directly

Re-exports§

pub use import_registry::ImportRegistry;
pub use import_registry::clear_registry;
pub use import_registry::collect_file_imports;
pub use import_registry::install_registry;
pub use import_registry::take_registry;
pub use import_registry::with_registry;
pub use import_registry::with_registry_mut;
pub use lower::collect_exported_names;
pub use lower::lower_classes;
pub use lower::lower_enums;
pub use lower::lower_functions;
pub use lower::lower_interfaces;
pub use lower::lower_targets;
pub use lower::lower_type_aliases;
pub use ::oxc;
pub use abi::*;
pub use derive::*;
pub use errors::*;
pub use stream::*;

Modules§

abi
Application Binary Interface (ABI) types for stable macro communication.
ast
Owned, source-backed expression and identifier values.
config
Macroforge Configuration Types
context_registry
Thread-local storage for the MacroContextIR currently being processed.
declarative
Declarative (pattern-matching) macro grammar and parser.
derive
syn-like derive input types for TypeScript macros.
errors
Error types for the macro system.
import_registry
Import Registry
jsdoc
JSDoc directive parsing utilities.
lower
AST lowering from Oxc types to IR representations.
stream
A syn-like stream for TypeScript macro input and output.
type_normalize
Helpers for parsing TS type-string snippets into structural pieces.

Macros§

parse_ts_macro_input
Parse a TsStream into a DeriveInput, returning early with an error MacroResult on failure.
ts_ident
Creates an ast::Ident from a name or a format string.

Traits§

QuoteArena
The arena ts_quote! parses into, from either an owned Allocator or an &'a Allocator the caller holds. ts_quote! always borrows its arena argument, so a reference parameter keeps its own lifetime and the quoted node can be returned to the caller.
ToAssignTargetSource
ToExprSource
ToIdentSource
ToPatSource
ToStringLiteralSource
ToTsString
Trait for converting values to TypeScript string representations.
ToTypeSource

Functions§

assignment_target_to_string
binding_pattern_to_string
expr_to_string
parse_assignment_target
parse_binding_pattern
parse_expr
parse_module_item
parse_program
parse_prop_or_spread
parse_statement
parse_type
stmt_to_string
string_literal_to_string
type_to_string