Skip to main content

ts_gen/
lib.rs

1//! `ts-gen`: Generate `wasm-bindgen` Rust bindings from TypeScript `.d.ts` files.
2//!
3//! This crate provides both a library API and a CLI tool for converting
4//! TypeScript declaration files into Rust `#[wasm_bindgen]` extern blocks.
5//!
6//! # Pipeline
7//!
8//! ```text
9//! .d.ts files
10//!   → oxc_parser (AST)
11//!   → First Pass phase 1: collect all type names into TypeRegistry
12//!   → First Pass phase 2: populate full IR (resolve references, merge var+interface patterns)
13//!   → Assembly: group by ModuleContext, resolve inheritance chains, classify types
14//!   → Code Generation: IR → syn::File → prettyplease → .rs files
15//! ```
16
17pub mod codegen;
18pub mod context;
19pub mod external_map;
20pub mod ir;
21pub mod parse;
22pub mod util;
23
24use std::path::Path;
25
26use anyhow::Result;
27
28use crate::context::GlobalContext;
29
30/// Parse `.d.ts` files and return the module + global context.
31///
32/// Diagnostics are collected on `GlobalContext.diagnostics` — callers should
33/// inspect or display them as needed (e.g., `ctx.diagnostics.emit()`).
34///
35/// External type mappings can be configured on the returned `GlobalContext`
36/// before passing it to `codegen::generate`.
37pub fn parse(
38    paths: &[impl AsRef<Path>],
39    lib_name: Option<&str>,
40) -> Result<(ir::Module, GlobalContext)> {
41    parse::parse_dts_files(paths, lib_name)
42}
43
44/// Parse a single `.d.ts` source string and return the module + global context.
45///
46/// Diagnostics are collected on `GlobalContext.diagnostics` — callers should
47/// inspect or display them as needed (e.g., `ctx.diagnostics.emit()`).
48pub fn parse_source(source: &str, lib_name: Option<&str>) -> Result<(ir::Module, GlobalContext)> {
49    parse::parse_single_source(source, lib_name)
50}