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::JsBuilder;
20//! use oxc_allocator::Allocator;
21//! use oxc_ast::ast::Statement;
22//!
23//! let allocator = Allocator::default();
24//! let js = JsBuilder::new(&allocator);
25//!
26//! // Build: const x = 42;
27//! let stmt = js.const_decl("x", js.number(42.0));
28//!
29//! // Build: import React from 'react';
30//! let import = js.import_default("React", "react");
31//!
32//! // Generate code
33//! let code = js.program(vec![
34//! Statement::from(import),
35//! stmt,
36//! ])?;
37//! println!("{}", code);
38//! # Ok::<(), fob_gen::GenError>(())
39//! ```
40//!
41//! ## Building Complex Expressions
42//!
43//! ```rust
44//! use fob_gen::JsBuilder;
45//! use oxc_allocator::Allocator;
46//!
47//! let allocator = Allocator::default();
48//! let js = JsBuilder::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//!
57//! let code = js.program(vec![stmt])?;
58//! # Ok::<(), fob_gen::GenError>(())
59//! ```
60//!
61//! ## Arrow Functions and Arrays
62//!
63//! ```rust
64//! use fob_gen::JsBuilder;
65//! use oxc_allocator::Allocator;
66//!
67//! let allocator = Allocator::default();
68//! let js = JsBuilder::new(&allocator);
69//!
70//! // Build: const double = x => x * 2;
71//! let arrow = js.arrow_fn(
72//! vec!["x"],
73//! js.binary(
74//! js.ident("x"),
75//! oxc_ast::ast::BinaryOperator::Multiplication,
76//! js.number(2.0),
77//! ),
78//! );
79//! let stmt = js.const_decl("double", arrow);
80//!
81//! let code = js.program(vec![stmt])?;
82//! # Ok::<(), fob_gen::GenError>(())
83//! ```
84
85mod builder;
86mod dev_ui;
87mod error;
88mod format;
89mod jsx;
90mod program_builder;
91
92#[cfg(feature = "parser")]
93mod parser;
94
95#[cfg(feature = "query-api")]
96mod query;
97
98#[cfg(feature = "transform-engine")]
99mod transform;
100
101#[cfg(feature = "fob_internal")]
102mod internal;
103
104pub use builder::JsBuilder;
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, 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;