fob_gen/lib.rs
1//! Ergonomic JavaScript code generation using OXC AST builders
2//!
3//! This crate provides a high-level, type-safe API for generating JavaScript code
4//! using the OXC (Oxidation Compiler) AST infrastructure.
5//!
6//! # Features
7//!
8//! - **Type-safe AST building** - Leverage Rust's type system for correct JavaScript generation
9//! - **Zero-copy string handling** - Efficient string interning via OXC's `Atom`
10//! - **Ergonomic API** - Intuitive method names that mirror JavaScript syntax
11//! - **Full module support** - Generate imports, exports, and ES modules
12//! - **Modern JS features** - Arrow functions, template literals, destructuring, and more
13//!
14//! # Examples
15//!
16//! ## Basic Usage
17//!
18//! ```rust
19//! use fob_gen::ProgramBuilder;
20//! use oxc_allocator::Allocator;
21//! use oxc_ast::ast::Statement;
22//!
23//! let allocator = Allocator::default();
24//! let mut js = ProgramBuilder::new(&allocator);
25//!
26//! // Build: const x = 42;
27//! let stmt = js.const_decl("x", js.number(42.0));
28//! js.push(stmt);
29//!
30//! // Build: import React from 'react';
31//! let import = js.import_default("React", "react");
32//! // ModuleDeclarations need to be converted to Statements
33//! js.push(Statement::from(import));
34//!
35//! // Generate code
36//! let code = js.generate(&Default::default())?;
37//! println!("{}", code);
38//! # Ok::<(), fob_gen::GenError>(())
39//! ```
40//!
41//! ## Building Complex Expressions
42//!
43//! ```rust
44//! use fob_gen::ProgramBuilder;
45//! use oxc_allocator::Allocator;
46//!
47//! let allocator = Allocator::default();
48//! let mut js = ProgramBuilder::new(&allocator);
49//!
50//! // Build: console.log("Hello, world!")
51//! let console_log = js.call(
52//! js.member(js.ident("console"), "log"),
53//! vec![js.arg(js.string("Hello, world!"))],
54//! );
55//! let stmt = js.expr_stmt(console_log);
56//! js.push(stmt);
57//!
58//! let code = js.generate(&Default::default())?;
59//! # Ok::<(), fob_gen::GenError>(())
60//! ```
61//!
62//! ## Arrow Functions and Arrays
63//!
64//! ```rust
65//! use fob_gen::ProgramBuilder;
66//! use oxc_allocator::Allocator;
67//!
68//! let allocator = Allocator::default();
69//! let mut js = ProgramBuilder::new(&allocator);
70//!
71//! // Build: const double = x => x * 2;
72//! let arrow = js.arrow_fn(
73//! vec!["x"],
74//! js.binary(
75//! js.ident("x"),
76//! oxc_ast::ast::BinaryOperator::Multiplication,
77//! js.number(2.0),
78//! ),
79//! );
80//! let stmt = js.const_decl("double", arrow);
81//! js.push(stmt);
82//!
83//! let code = js.generate(&Default::default())?;
84//! # Ok::<(), fob_gen::GenError>(())
85//! ```
86
87mod dev_ui;
88mod error;
89mod format;
90mod jsx;
91mod program_builder;
92
93#[cfg(feature = "parser")]
94mod parser;
95
96#[cfg(feature = "query-api")]
97pub mod query;
98
99#[cfg(feature = "transform-engine")]
100mod transform;
101
102#[cfg(feature = "fob_internal")]
103mod internal;
104
105pub use dev_ui::{HtmlBuilder, RouteSpec};
106pub use error::{GenError, Result};
107pub use format::{FormatOptions, IndentStyle, QuoteStyle};
108pub use jsx::JsxBuilder;
109pub use program_builder::ProgramBuilder;
110
111#[cfg(feature = "parser")]
112pub use parser::{ParseDiagnostic, ParseOptions, ParsedProgram, parse};
113
114#[cfg(feature = "query-api")]
115pub use query::{CallQuery, ExportDeclaration, ExportQuery, ImportQuery, JsxQuery, QueryBuilder};
116
117#[cfg(feature = "transform-engine")]
118pub use transform::{TransformEngine, TransformOutput, TransformPass, TransformResult};
119
120#[cfg(feature = "fob_internal")]
121pub use internal::{AstMutations, DevInjection, ImportManipulation};
122
123// Re-export commonly used OXC types for convenience
124pub use oxc_allocator::Allocator;
125pub use oxc_ast::ast::{BinaryOperator, LogicalOperator, UnaryOperator};
126pub use oxc_span::Atom;