#ast #javascript #oxc #generator #name

fob-gen

Ergonomic JavaScript code generation using OXC AST builders

8 unstable releases (3 breaking)

Uses new Rust 2024

0.5.0 Dec 29, 2025
0.4.2 Dec 11, 2025
0.3.0 Dec 1, 2025
0.2.4 Dec 1, 2025
0.2.2 Nov 29, 2025

#15 in #oxc


Used in 7 crates (3 directly)

MIT license

66KB
1.5K SLoC

Ergonomic JavaScript code generation using OXC AST builders

This crate provides a high-level, type-safe API for generating JavaScript code using the OXC (Oxidation Compiler) AST infrastructure.

Features

  • Type-safe AST building - Leverage Rust's type system for correct JavaScript generation
  • Zero-copy string handling - Efficient string interning via OXC's Atom
  • Ergonomic API - Intuitive method names that mirror JavaScript syntax
  • Full module support - Generate imports, exports, and ES modules
  • Modern JS features - Arrow functions, template literals, destructuring, and more

Examples

Basic Usage

use fob_gen::ProgramBuilder;
use oxc_allocator::Allocator;
use oxc_ast::ast::Statement;

let allocator = Allocator::default();
let mut js = ProgramBuilder::new(&allocator);

// Build: const x = 42;
let stmt = js.const_decl("x", js.number(42.0));
js.push(stmt);

// Build: import React from 'react';
let import = js.import_default("React", "react");
// ModuleDeclarations need to be converted to Statements
js.push(Statement::from(import));

// Generate code
let code = js.generate(&Default::default())?;
println!("{}", code);

Building Complex Expressions

use fob_gen::ProgramBuilder;
use oxc_allocator::Allocator;

let allocator = Allocator::default();
let mut js = ProgramBuilder::new(&allocator);

// Build: console.log("Hello, world!")
let console_log = js.call(
    js.member(js.ident("console"), "log"),
    vec![js.arg(js.string("Hello, world!"))],
);
let stmt = js.expr_stmt(console_log);
js.push(stmt);

let code = js.generate(&Default::default())?;

Arrow Functions and Arrays

use fob_gen::ProgramBuilder;
use oxc_allocator::Allocator;

let allocator = Allocator::default();
let mut js = ProgramBuilder::new(&allocator);

// Build: const double = x => x * 2;
let arrow = js.arrow_fn(
    vec!["x"],
    js.binary(
        js.ident("x"),
        oxc_ast::ast::BinaryOperator::Multiplication,
        js.number(2.0),
    ),
);
let stmt = js.const_decl("double", arrow);
js.push(stmt);

let code = js.generate(&Default::default())?;

Dependencies

~13MB
~227K SLoC