No description
  • OCaml 76.1%
  • Standard ML 9.6%
  • Perl 7.3%
  • Raku 5.7%
  • Dune 1.3%
Find a file
2026-02-25 12:47:24 +00:00
bin Initial commit 2026-02-25 12:47:24 +00:00
examples/simple_function Initial commit 2026-02-25 12:47:24 +00:00
lib Initial commit 2026-02-25 12:47:24 +00:00
test Initial commit 2026-02-25 12:47:24 +00:00
.gitignore Initial commit 2026-02-25 12:47:24 +00:00
ARCHITECTURE.md Initial commit 2026-02-25 12:47:24 +00:00
dune-project Initial commit 2026-02-25 12:47:24 +00:00
melange-bindings-against-tsc.opam Initial commit 2026-02-25 12:47:24 +00:00
README.md Initial commit 2026-02-25 12:47:24 +00:00
STATUS.md Initial commit 2026-02-25 12:47:24 +00:00

melange-bindings-against-tsc

Validate Melange bindings against TypeScript definitions

An OCaml binary tool that validates Melange bindings by type-checking the compiled JavaScript output against original TypeScript type definitions using tsserver.

🎯 Overview

This tool helps ensure that Melange FFI bindings correctly represent TypeScript APIs by:

  1. Parsing TypeScript .d.ts definition files
  2. Generating Melange bindings (.ml/.mli files) with @genType annotations
  3. Compiling the bindings via melc (Melange compiler)
  4. Validating the generated TypeScript types against the original definitions using tsserver
  5. Reporting any type mismatches or binding errors

✨ Features

  • βœ… Automatic validation - Compare generated types with original TypeScript definitions
  • πŸ”„ TypeScript AST parsing - Uses TypeScript's own compiler API via Node.js
  • 🎨 Beautiful reports - Console, JSON, and HTML output formats
  • πŸš€ genType integration - Leverages genType for bidirectional type generation
  • πŸ“¦ DefinitelyTyped support (planned) - Pull type definitions from @types packages

πŸ“‹ Prerequisites

  • OCaml >= 5.1.0
  • Dune >= 3.12
  • Node.js >= 18 (for TypeScript parsing and tsserver)
  • TypeScript (install globally: npm install -g typescript)
  • Melange (forked version with genType support - coming soon)

πŸš€ Installation

From Source

# Clone the repository
git clone https://github.com/yourusername/melange-bindings-against-tsc.git
cd melange-bindings-against-tsc

# Install dependencies
opam install . --deps-only

# Build
dune build

# Install
dune install

Via OPAM (coming soon)

opam install melange-bindings-against-tsc

πŸ“– Usage

Basic Usage

Validate a single TypeScript definition file:

melange-bindings-against-tsc path/to/types.d.ts

With Options

# Specify output directory for generated bindings
melange-bindings-against-tsc types.d.ts --output-dir ./generated

# JSON output format
melange-bindings-against-tsc types.d.ts --format json

# HTML report
melange-bindings-against-tsc types.d.ts --format html > report.html

# Verbose logging
melange-bindings-against-tsc types.d.ts --verbose

Future: Pull from DefinitelyTyped

# Pull and validate React types
melange-bindings-against-tsc --pull react

# Pull specific version
melange-bindings-against-tsc --pull react@18.2.0

πŸ—οΈ Project Structure

melange-bindings-against-tsc/
β”œβ”€β”€ bin/                       # CLI entry point
β”œβ”€β”€ lib/
β”‚   β”œβ”€β”€ typescript/            # TypeScript parsing and tsserver integration
β”‚   β”œβ”€β”€ melange/               # Binding generation and type mapping
β”‚   β”œβ”€β”€ validator/             # Validation orchestration and reporting
β”‚   β”œβ”€β”€ fetcher/               # DefinitelyTyped integration (future)
β”‚   └── utils/                 # Shared utilities
β”œβ”€β”€ test/
β”‚   β”œβ”€β”€ fixtures/              # Test TypeScript definition files
β”‚   └── unit/                  # Unit tests
└── examples/                  # Example usage

πŸ”§ How It Works

1. TypeScript Parsing

(* Parse .d.ts file into OCaml AST *)
let%lwt module_ = Typescript.Parser.parse_file "lodash.d.ts" in
(* Returns: { declarations; imports; file_path } *)

2. Binding Generation

(* Generate Melange bindings *)
let config = {
  module_name = "lodash";
  output_dir = "./_melange_bindings";
  use_gentype = true;
} in
let%lwt output = Melange.Generator.generate_bindings module_ config in

Example output (lodash.ml):

external map : 'a array -> ('a -> 'b) -> 'b array = "map"
  [@@mel.module "lodash"] [@@genType]

external filter : 'a array -> ('a -> bool) -> 'a array = "filter"
  [@@mel.module "lodash"] [@@genType]

3. Compilation & Validation

(* Compile with melc - generates .js and .gen.ts files *)
(* Start tsserver and validate types *)
let%lwt result = Validator.validate module_ config in

(* Generate report *)
let report = Report.generate Console result in
print_endline report

🎨 Example Output

πŸ” Validating lodash.d.ts...

βœ… map - PASS
   Expected: <T, U>(array: T[], fn: (item: T) => U) => U[]
   Generated: <T, U>(array: T[], fn: (item: T) => U) => U[]

❌ filter - FAIL
   Expected: <T>(array: T[], predicate: (item: T) => boolean) => T[]
   Generated: <T>(array: T[], predicate: (item: T) => bool) => T[]
   Error: Type mismatch - 'bool' should be 'boolean'

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Summary: 1/2 passed (50%)

πŸ› οΈ Development

Building

# Build the project
dune build

# Run in development mode
dune exec melange-bindings-against-tsc -- test/fixtures/simple.d.ts --verbose

# Run tests
dune runtest

# Watch mode
dune build --watch

Testing

# Run all tests
dune test

# Run specific test
dune exec test/test_parser.exe

Documentation

# Generate API documentation
dune build @doc

# View documentation
open _build/default/_doc/_html/index.html

πŸ“š Architecture

Type Mapping

TypeScript types are mapped to OCaml/Melange types:

TypeScript OCaml/Melange
number int / float
string string
boolean bool
T[] 'a array
T | null 'a option
{ x: T } { x : t }
(x: T) => U t -> u
Promise<T> t Lwt.t

Melange Fork with genType

This project requires a fork of Melange with integrated genType support (following the conservative approach from issue #916):

  • Port genType to Melange's typed tree
  • Add Dune rules for post-compilation genType processing
  • Generate .gen.ts files alongside .js output

πŸ—ΊοΈ Roadmap

  • TypeScript AST parsing via Node.js
  • Basic type mapping (TS β†’ OCaml)
  • Melange binding generation
  • CLI with cmdliner
  • Complete JSON to OCaml AST conversion
  • tsserver integration and type comparison
  • Melange fork with genType
  • Comprehensive test suite
  • DefinitelyTyped integration (--pull command)
  • Watch mode for continuous validation
  • VS Code extension

🀝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details.

Development Setup

# Install development dependencies
opam install --deps-only --with-test --with-doc .

# Install pre-commit hooks
# (coming soon)

πŸ“„ License

MIT License - see LICENSE for details.

πŸ™ Acknowledgments

πŸ“¬ Contact


Status: 🚧 Early development - Core features implemented, validation pipeline in progress