Crate fob_gen

Crate fob_gen 

Source
Expand description

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())?;

Structs§

Allocator
A bump-allocated memory arena.
Atom
An inlinable string for oxc_allocator.
FormatOptions
Formatting options for code generation
HtmlBuilder
HTML builder for generating dev server HTML
JsxBuilder
JSX element builder
ProgramBuilder
Comprehensive program builder for JavaScript code generation.
RouteSpec
Route specification for manifest generation

Enums§

BinaryOperator
Operators used in binary expressions. Does not include logical binary operators, which are in LogicalOperator.
GenError
Errors that can occur during JavaScript code generation
IndentStyle
Indentation style
LogicalOperator
Logical binary operators
QuoteStyle
Quote style for string literals
UnaryOperator
Operators used in unary operators.

Type Aliases§

Result
Result type for code generation operations