Skip to main content
Glama
expand-sync.md2.53 kB
# expandSync() macroforge v0.1.43 Synchronously expands macros in TypeScript code. This is the standalone macro expansion function that doesn't use caching. For cached expansion, use \[\`NativePlugin::process\_file\`\] instead. ## Signature TypeScript ``` function expandSync(   code: string,   filepath: string,   options?: ExpandOptions ): ExpandResult ``` ## Parameters | Parameter | Type | Description | | ---------- | --------------- | ------------------------------------ | | `code` | `string` | TypeScript source code to transform | | `filepath` | `string` | File path (used for error reporting) | | `options` | `ExpandOptions` | Optional configuration | ## ExpandOptions TypeScript ``` interface ExpandOptions {   // Keep @derive decorators in output (default: false)   keepDecorators?: boolean; } ``` ## ExpandResult TypeScript ``` interface ExpandResult {   // Transformed TypeScript code   code: string;   // Generated type declarations (.d.ts content)   types?: string;   // Macro expansion metadata (JSON string)   metadata?: string;   // Warnings and errors from macro expansion   diagnostics: MacroDiagnostic[];   // Position mapping data for source maps   sourceMapping?: SourceMappingResult; } ``` ## MacroDiagnostic TypeScript ``` interface MacroDiagnostic {   message: string;   severity: "error" | "warning" | "info";   span: {     start: number;     end: number;   }; } ``` ## Example TypeScript ``` import { expandSync } from "macroforge"; const sourceCode = ` /** @derive(Debug) */ class User { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } } `; const result = expandSync(sourceCode, "user.ts"); console.log("Transformed code:"); console.log(result.code); if (result.types) { console.log("Type declarations:"); console.log(result.types); } if (result.diagnostics.length > 0) { for (const diag of result.diagnostics) { console.log(`[${diag.severity}] ${diag.message}`); } } ``` ## Error Handling Syntax errors and macro errors are returned in the `diagnostics` array, not thrown as exceptions: TypeScript ``` const result = expandSync(invalidCode, "file.ts"); for (const diag of result.diagnostics) { if (diag.severity === "error") { console.error(`Error at ${diag.span.start}: ${diag.message}`); } } ```

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/macroforge-ts/mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server