Logo Xantham

Xantham.Fable Project Documentation

The Xantham.Fable project is the TypeScript extractor component of the Xantham schema-driven TypeScript-to-F# bindings generator. It leverages Fable to compile to JavaScript and uses the TypeScript Compiler API (via ts-morph) to crawl .d.ts files and emit JSON conforming to the common schema.

Architecture Overview

Xantham.Fable is designed specifically to work with Fable (an F# to JavaScript compiler) and is compiled to JavaScript via Fable. It serves as the extraction phase of the Xantham architecture:

flowchart LR input[.d.ts Input] --> tsc subgraph Fable / Xantham.Fable tsc[TypeScript Compiler API] crawler[AST Crawler] thoth_enc[Thoth encoder] tsc --> crawler --> thoth_enc end subgraph Common schema[Common Schema] end schema -.->|contract| thoth_enc schema -.->|contract| thoth_dec thoth_enc --> json[JSON] subgraph .NET / Xantham.Decoder json --> thoth_dec[Thoth decoder] thoth_dec --> util[Utility layer] end util --> types[F# Types] subgraph Userland Generators types --> gen1[Xantham.SimpleGenerator\nFabulous.AST] types --> gen2[Your Generator\nString Concatenation / SyntaxOak / ...] end gen1 & gen2 --> out[F# Bindings]

Core Architecture Components

Signal-based Reactive Architecture

Xantham.Fable uses a sophisticated signal-based reactive system for managing data flow through the AST processing:

flowchart LR subgraph Signal_System Source[Source Signals] --> Auto[Auto Computed Signals] Source --> Computed[Explicit Computed Signals] Auto --> Value1[Computed Value] Computed --> Value2[Computed Value] end subgraph Dependency_Tracking Collector[Dependency Collector] --> Auto Collector --> Computed Auto --> Propagator[Propagation] Computed --> Propagator Propagator --> Invalidated[Invalidated Events] end Value1 --> Cache[Signal Cache] Value2 --> Cache Invalidated --> Recompute[Recompute Cached Values]

Guarded Properties Pattern

Xantham.Fable employs a sophisticated guarded properties pattern to ensure type safety and clean data handling:

flowchart LR subgraph Guarded_Properties Tag[XanthamTag] --> Slot1[Named Accessors] Tag --> Slot2[Keyed Bag] Slot1 --> Data1[Data Access/Modification] Slot2 --> Data2[Partitioned Data] Slot1 --> Pending[Pending Signals] Slot2 --> Pending end subgraph Data_Flow Tag -.->|Data Associated| Processor[Processing Logic] Processor --> Tag Tag --> Cache[Caching System] Cache --> Processor end

Stack-based AST Traversal

To handle deep or complex TypeScript AST structures and avoid JavaScript stack overflows, Xantham.Fable uses:

flowchart TD A[Entry Files] --> B[TypeScript Program] B --> C[Module Resolution] C --> D[Stack Initialization] D --> E[Tag Stack] E --> F{Process Tag} F -->|XanthamTag| G[Dispatcher] G --> H[Handler] H --> I[Process Node] I --> J[Child Nodes] J --> K{Push to Stack} K -->|Yes| E K -->|No| F F -->|Complete| L[Cache & Assemble] L --> M[Duplicate Handling] M --> N[Final Encoding] N --> O[JSON Output]

File Structure

The project structure is organized as follows:

Key Concepts

AST Node Processing

The system processes TypeScript AST nodes through specialized handlers that translate them into the common schema format:

Processing Pipeline

The typical workflow in Xantham.Fable is:

  1. Initialization: Create TypeScript program, type checker, and caches
  2. AST Crawling: Traverse AST nodes using stack-based recursion
  3. Type Processing: Translate each TypeScript node to common schema format
  4. Signal Management: Use signals to track dependencies and manage data flow
  5. Duplication Handling: Merge duplicate types and exports
  6. Encoding: Serialize final results to JSON using Thoth.Json

Key Design Patterns

Signal-based Computation

Tag-based Architecture

Caching and Memoization

Extensibility

This architecture allows Xantham.Fable to be robust, scalable, and maintainable while properly converting complex TypeScript constructs into a standardized schema that can be consumed by downstream F# generators.

Type something to start searching.