API Stability Notice

Macroforge is under active development. The API is not yet stable and may change between versions. Some documentation sections may be outdated.

transformSync()

macroforge v0.1.42

Synchronously transforms TypeScript code through the macro expansion system. This is similar to [`expand_sync`] but returns a [`TransformResult`] which includes source map information (when available).

Signature

TypeScript
function transformSync(
  code: string,
  filepath: string
): TransformResult

Parameters

ParameterTypeDescription
codestringTypeScript source code to transform
filepathstringFile path (used for error reporting)

TransformResult

TypeScript
interface TransformResult {
  // Transformed TypeScript code
  code: string;

  // Source map (JSON string, not yet implemented)
  map?: string;

  // Generated type declarations
  types?: string;

  // Macro expansion metadata
  metadata?: string;
}

Comparison with expandSync()

FeatureexpandSynctransformSync
OptionsYesNo
DiagnosticsYesNo
Source MappingYesLimited
Use CaseGeneral purposeBuild tools

Example

TypeScript
import { transformSync } from "macroforge";

const sourceCode = \`
/** @derive(Debug) */
class User {
  name: string;
}
\`;

const result = transformSync(sourceCode, "user.ts");

console.log(result.code);

if (result.types) {
  // Write to .d.ts file
  fs.writeFileSync("user.d.ts", result.types);
}

if (result.metadata) {
  // Parse and use metadata
  const meta = JSON.parse(result.metadata);
  console.log("Macros expanded:", meta);
}

When to Use

Use transformSync when:

  • Building custom integrations
  • You need raw output without diagnostics
  • You're implementing a build tool plugin

Use expandSync for most other use cases, as it provides better error handling.