fob-gen 0.2.0

Ergonomic JavaScript code generation using OXC AST builders
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Core JavaScript code builder using OXC AST

use crate::Result;
use crate::format::FormatOptions;
use oxc_allocator::Allocator;
use oxc_ast::ast::*;
use oxc_ast::{AstBuilder, NONE};
use oxc_codegen::Codegen;
use oxc_span::{Atom, SPAN, SourceType};

/// Main JavaScript code builder
///
/// Wraps OXC's AstBuilder and provides ergonomic methods for building AST nodes.
/// All AST nodes created by this builder share the same allocator lifetime.
///
/// # Examples
///
/// ```
/// use fob_gen::JsBuilder;
/// use oxc_allocator::Allocator;
///
/// let allocator = Allocator::default();
/// let js = JsBuilder::new(&allocator);
///
/// // Create a simple variable declaration
/// let stmt = js.const_decl("message", js.string("Hello, world!"));
/// ```
pub struct JsBuilder<'a> {
    ast: AstBuilder<'a>,
}

impl<'a> JsBuilder<'a> {
    /// Create a new builder with the given allocator
    pub fn new(alloc: &'a Allocator) -> Self {
        Self {
            ast: AstBuilder::new(alloc),
        }
    }

    /// Get the underlying allocator (for advanced usage)
    pub fn allocator(&self) -> &'a Allocator {
        self.ast.allocator
    }

    /// Get the underlying AstBuilder (for advanced usage)
    pub fn ast(&self) -> &AstBuilder<'a> {
        &self.ast
    }

    // ===== CORE PRIMITIVES =====

    /// Create an identifier expression: `name`
    pub fn ident(&self, name: impl Into<Atom<'a>>) -> Expression<'a> {
        self.ast.expression_identifier(SPAN, name)
    }

    /// Create a string literal: `"value"`
    pub fn string(&self, value: impl Into<Atom<'a>>) -> Expression<'a> {
        self.ast.expression_string_literal(SPAN, value, None)
    }

    /// Create a number literal: `42`
    pub fn number(&self, value: f64) -> Expression<'a> {
        self.ast
            .expression_numeric_literal(SPAN, value, None, NumberBase::Decimal)
    }

    /// Create null: `null`
    pub fn null(&self) -> Expression<'a> {
        self.ast.expression_null_literal(SPAN)
    }

    /// Create boolean: `true` or `false`
    pub fn bool(&self, value: bool) -> Expression<'a> {
        self.ast.expression_boolean_literal(SPAN, value)
    }

    // ===== COMPLEX EXPRESSIONS =====

    /// Create a member expression: `obj.prop`
    pub fn member(&self, object: Expression<'a>, property: impl Into<Atom<'a>>) -> Expression<'a> {
        let prop_name = self.ast.identifier_name(SPAN, property);
        let member = self
            .ast
            .member_expression_static(SPAN, object, prop_name, false);
        Expression::from(member)
    }

    /// Create a computed member expression: `obj[expr]`
    ///
    /// This is critical for array indexing: `arr[0]`, `pageRoutes[idx]`, etc.
    pub fn computed_member(&self, object: Expression<'a>, index: Expression<'a>) -> Expression<'a> {
        let member = self
            .ast
            .member_expression_computed(SPAN, object, index, false);
        Expression::from(member)
    }

    /// Create a call expression: `callee(args...)`
    pub fn call(&self, callee: Expression<'a>, args: Vec<Argument<'a>>) -> Expression<'a> {
        let args_vec = self.ast.vec_from_iter(args);
        let call = self
            .ast
            .call_expression(SPAN, callee, NONE, args_vec, false);
        Expression::CallExpression(self.ast.alloc(call))
    }

    /// Create an argument from an expression
    pub fn arg(&self, expr: Expression<'a>) -> Argument<'a> {
        Argument::from(expr)
    }

    /// Create an object expression: `{ key1: value1, key2: value2 }`
    pub fn object(&self, props: Vec<ObjectPropertyKind<'a>>) -> Expression<'a> {
        let props_vec = self.ast.vec_from_iter(props);
        self.ast.expression_object(SPAN, props_vec)
    }

    /// Create an object property: `key: value`
    pub fn prop(&self, key: impl Into<Atom<'a>>, value: Expression<'a>) -> ObjectPropertyKind<'a> {
        let key_name = self.ast.identifier_name(SPAN, key);
        let property = self.ast.object_property(
            SPAN,
            PropertyKind::Init,
            PropertyKey::StaticIdentifier(self.ast.alloc(key_name)),
            value,
            false,
            false,
            false,
        );
        ObjectPropertyKind::ObjectProperty(self.ast.alloc(property))
    }

    /// Create an array expression: `[elem1, elem2, ...]`
    pub fn array(&self, elements: Vec<Expression<'a>>) -> Expression<'a> {
        let array_elements: Vec<_> = elements
            .into_iter()
            .map(ArrayExpressionElement::from)
            .collect();
        let elements_vec = self.ast.vec_from_iter(array_elements);
        self.ast.expression_array(SPAN, elements_vec)
    }

    /// Create an arrow function with expression body: `(params) => expr`
    pub fn arrow_fn(&self, params: Vec<&'a str>, body: Expression<'a>) -> Expression<'a> {
        let param_items: Vec<_> = params
            .into_iter()
            .map(|name| {
                let pattern = self.ast.binding_pattern(
                    self.ast.binding_pattern_kind_binding_identifier(SPAN, name),
                    NONE,
                    false,
                );
                self.ast
                    .formal_parameter(SPAN, self.ast.vec(), pattern, None, false, false)
            })
            .collect();

        let items_vec = self.ast.vec_from_iter(param_items);
        let formal_params = self.ast.formal_parameters(
            SPAN,
            FormalParameterKind::ArrowFormalParameters,
            items_vec,
            NONE,
        );

        // Create a FunctionBody with just an expression (return statement)
        let return_stmt = self.ast.statement_return(SPAN, Some(body));
        let stmts = self.ast.vec1(return_stmt);
        let function_body = self.ast.function_body(SPAN, self.ast.vec(), stmts);

        self.ast.expression_arrow_function(
            SPAN,
            false, // expression = false because we're using a block
            false, // async
            NONE,
            formal_params,
            NONE,
            self.ast.alloc(function_body),
        )
    }

    /// Create an arrow function with block body: `(params) => { stmts }`
    pub fn arrow_fn_block(
        &self,
        params: Vec<&'a str>,
        stmts: Vec<Statement<'a>>,
    ) -> Expression<'a> {
        let param_items: Vec<_> = params
            .into_iter()
            .map(|name| {
                let pattern = self.ast.binding_pattern(
                    self.ast.binding_pattern_kind_binding_identifier(SPAN, name),
                    NONE,
                    false,
                );
                self.ast
                    .formal_parameter(SPAN, self.ast.vec(), pattern, None, false, false)
            })
            .collect();

        let items_vec = self.ast.vec_from_iter(param_items);
        let formal_params = self.ast.formal_parameters(
            SPAN,
            FormalParameterKind::ArrowFormalParameters,
            items_vec,
            NONE,
        );

        let body_stmts = self.ast.vec_from_iter(stmts);
        let function_body = self.ast.function_body(SPAN, self.ast.vec(), body_stmts);

        self.ast.expression_arrow_function(
            SPAN,
            false, // block body (not expression)
            false, // async
            Option::<TSTypeParameterDeclaration>::None,
            formal_params,
            Option::<TSTypeAnnotation>::None,
            self.ast.alloc(function_body),
        )
    }

    // ===== STATEMENTS =====

    /// Create a const declaration: `const name = init;`
    pub fn const_decl(&self, name: impl Into<Atom<'a>>, init: Expression<'a>) -> Statement<'a> {
        let pattern = self.ast.binding_pattern(
            self.ast.binding_pattern_kind_binding_identifier(SPAN, name),
            NONE,
            false,
        );
        let declarator = self.ast.variable_declarator(
            SPAN,
            VariableDeclarationKind::Const,
            pattern,
            Some(init),
            false,
        );
        let declarations = self.ast.vec1(declarator);
        let var_decl = self.ast.variable_declaration(
            SPAN,
            VariableDeclarationKind::Const,
            declarations,
            false,
        );
        Statement::VariableDeclaration(self.ast.alloc(var_decl))
    }

    /// Create a let declaration: `let name = init;`
    pub fn let_decl(
        &self,
        name: impl Into<Atom<'a>>,
        init: Option<Expression<'a>>,
    ) -> Statement<'a> {
        let pattern = self.ast.binding_pattern(
            self.ast.binding_pattern_kind_binding_identifier(SPAN, name),
            NONE,
            false,
        );
        let declarator =
            self.ast
                .variable_declarator(SPAN, VariableDeclarationKind::Let, pattern, init, false);
        let declarations = self.ast.vec1(declarator);
        let var_decl =
            self.ast
                .variable_declaration(SPAN, VariableDeclarationKind::Let, declarations, false);
        Statement::VariableDeclaration(self.ast.alloc(var_decl))
    }

    /// Create an expression statement
    pub fn expr_stmt(&self, expr: Expression<'a>) -> Statement<'a> {
        self.ast.statement_expression(SPAN, expr)
    }

    /// Create an if statement: `if (test) { consequent } else { alternate }`
    pub fn if_stmt(
        &self,
        test: Expression<'a>,
        consequent: Vec<Statement<'a>>,
        alternate: Option<Vec<Statement<'a>>>,
    ) -> Statement<'a> {
        let consequent_stmts = self.ast.vec_from_iter(consequent);
        let consequent_block = self.ast.statement_block(SPAN, consequent_stmts);

        let alternate_block = alternate.map(|stmts| {
            let alt_stmts = self.ast.vec_from_iter(stmts);
            self.ast.statement_block(SPAN, alt_stmts)
        });

        Statement::IfStatement(self.ast.alloc(IfStatement {
            span: SPAN,
            test,
            consequent: consequent_block,
            alternate: alternate_block,
        }))
    }

    /// Create a return statement: `return expr;`
    pub fn return_stmt(&self, expr: Option<Expression<'a>>) -> Statement<'a> {
        Statement::ReturnStatement(self.ast.alloc(ReturnStatement {
            span: SPAN,
            argument: expr,
        }))
    }

    /// Create a throw statement: `throw expr;`
    pub fn throw(&self, expr: Expression<'a>) -> Statement<'a> {
        Statement::ThrowStatement(self.ast.alloc(ThrowStatement {
            span: SPAN,
            argument: expr,
        }))
    }

    // ===== IMPORTS & EXPORTS =====

    /// Create default import: `import local from 'source';`
    pub fn import_default(
        &self,
        local: impl Into<Atom<'a>>,
        source: impl Into<Atom<'a>>,
    ) -> ModuleDeclaration<'a> {
        let binding = self.ast.binding_identifier(SPAN, local);
        let specifier = self.ast.import_default_specifier(SPAN, binding);
        let specifiers = self
            .ast
            .vec1(ImportDeclarationSpecifier::ImportDefaultSpecifier(
                self.ast.alloc(specifier),
            ));
        let source_literal = self.ast.string_literal(SPAN, source, None);
        self.ast.module_declaration_import_declaration(
            SPAN,
            Some(specifiers),
            source_literal,
            None, // phase
            NONE, // with_clause
            ImportOrExportKind::Value,
        )
    }

    /// Create side-effect import: `import 'source';`
    pub fn import_side_effect(&self, source: impl Into<Atom<'a>>) -> ModuleDeclaration<'a> {
        let source_literal = self.ast.string_literal(SPAN, source, None);
        self.ast.module_declaration_import_declaration(
            SPAN,
            None, // no specifiers for side-effect imports
            source_literal,
            None, // phase
            NONE, // with_clause
            ImportOrExportKind::Value,
        )
    }

    /// Create named imports: `import { name1, name2 } from 'source';`
    pub fn import_named(
        &self,
        names: Vec<impl Into<Atom<'a>>>,
        source: impl Into<Atom<'a>>,
    ) -> ModuleDeclaration<'a> {
        let specifiers: Vec<_> = names
            .into_iter()
            .map(|name| {
                let atom = name.into();
                let imported_name = self.ast.identifier_name(SPAN, atom);
                let local_binding = self.ast.binding_identifier(SPAN, atom);
                let specifier = self.ast.import_specifier(
                    SPAN,
                    ModuleExportName::IdentifierName(imported_name),
                    local_binding,
                    ImportOrExportKind::Value,
                );
                ImportDeclarationSpecifier::ImportSpecifier(self.ast.alloc(specifier))
            })
            .collect();

        let specifiers_vec = self.ast.vec_from_iter(specifiers);
        let source_literal = self.ast.string_literal(SPAN, source, None);
        self.ast.module_declaration_import_declaration(
            SPAN,
            Some(specifiers_vec),
            source_literal,
            None, // phase
            NONE, // with_clause
            ImportOrExportKind::Value,
        )
    }

    /// Create default export: `export default expr;`
    pub fn export_default(&self, expr: Expression<'a>) -> ModuleDeclaration<'a> {
        // ExportDefaultDeclarationKind inherits Expression variants, so we can convert
        let kind: ExportDefaultDeclarationKind = expr.into();
        self.ast
            .module_declaration_export_default_declaration(SPAN, kind)
    }

    /// Create named export of variable: `export const name = init;`
    pub fn export_const(
        &self,
        name: impl Into<Atom<'a>>,
        init: Expression<'a>,
    ) -> ModuleDeclaration<'a> {
        let decl = self.const_decl(name, init);
        let declaration = match decl {
            Statement::VariableDeclaration(var_decl) => Declaration::VariableDeclaration(var_decl),
            _ => unreachable!(),
        };
        ModuleDeclaration::ExportNamedDeclaration(self.ast.alloc(ExportNamedDeclaration {
            span: SPAN,
            declaration: Some(declaration),
            specifiers: self.ast.vec(),
            source: None,
            export_kind: ImportOrExportKind::Value,
            with_clause: None,
        }))
    }

    // ===== PROGRAM GENERATION =====

    /// Generate a complete program from statements
    pub fn program(&self, statements: Vec<Statement<'a>>) -> Result<String> {
        self.program_with_format(statements, &FormatOptions::default())
    }

    /// Generate a complete program from statements with formatting options
    pub fn program_with_format(
        &self,
        statements: Vec<Statement<'a>>,
        _opts: &FormatOptions,
    ) -> Result<String> {
        let body_vec = self.ast.vec_from_iter(statements);
        let program = self.ast.program(
            SPAN,
            SourceType::mjs(),
            "",
            self.ast.vec(), // imports/exports (empty for now)
            None,           // hashbang
            self.ast.vec(), // directives
            body_vec,
        );

        // TODO: Apply formatting options when oxc_codegen supports it
        let codegen = Codegen::new();
        let result = codegen.build(&program);

        Ok(result.code)
    }

    // ===== HELPER METHODS =====

    /// Unary not operator: `!expr`
    pub fn not(&self, expr: Expression<'a>) -> Expression<'a> {
        self.ast
            .expression_unary(SPAN, UnaryOperator::LogicalNot, expr)
    }

    /// Binary expression: `left op right`
    pub fn binary(
        &self,
        left: Expression<'a>,
        op: BinaryOperator,
        right: Expression<'a>,
    ) -> Expression<'a> {
        self.ast.expression_binary(SPAN, left, op, right)
    }

    /// Logical expression: `left && right` or `left || right`
    pub fn logical(
        &self,
        left: Expression<'a>,
        op: LogicalOperator,
        right: Expression<'a>,
    ) -> Expression<'a> {
        self.ast.expression_logical(SPAN, left, op, right)
    }

    /// Conditional/ternary expression: `test ? consequent : alternate`
    pub fn conditional(
        &self,
        test: Expression<'a>,
        consequent: Expression<'a>,
        alternate: Expression<'a>,
    ) -> Expression<'a> {
        self.ast
            .expression_conditional(SPAN, test, consequent, alternate)
    }

    /// New expression: `new Ctor(args)`
    pub fn new_expr(&self, callee: Expression<'a>, args: Vec<Argument<'a>>) -> Expression<'a> {
        let args_vec = self.ast.vec_from_iter(args);
        self.ast.expression_new(SPAN, callee, NONE, args_vec)
    }

    /// Template literal: backtick string with expressions
    /// For simple strings without interpolation, use `string()` instead
    ///
    /// # Example
    /// ```ignore
    /// // Generates: `Hello, ${name}!`
    /// js.template_literal(
    ///     vec!["Hello, ", "!"],
    ///     vec![js.ident("name")],
    /// )
    /// ```
    pub fn template_literal(
        &self,
        parts: Vec<impl Into<Atom<'a>>>,
        expressions: Vec<Expression<'a>>,
    ) -> Expression<'a> {
        // In `a${x}b${y}c`, parts = ["a", "b", "c"], expressions = [x, y]
        // The last quasi (index == expressions.len()) has tail = true
        let quasis: Vec<_> = parts
            .into_iter()
            .enumerate()
            .map(|(i, part)| {
                let atom = part.into();
                let tail = i == expressions.len(); // Last quasi element
                let value = TemplateElementValue {
                    raw: atom,
                    cooked: Some(atom),
                };
                TemplateElement {
                    span: SPAN,
                    value,
                    tail,
                    lone_surrogates: false,
                }
            })
            .collect();

        let quasis_vec = self.ast.vec_from_iter(quasis);
        let expressions_vec = self.ast.vec_from_iter(expressions);

        self.ast
            .expression_template_literal(SPAN, quasis_vec, expressions_vec)
    }

    /// Spread element for arrays or function calls: `...expr`
    ///
    /// # Example
    /// ```ignore
    /// // In array: [...items]
    /// js.array(vec![js.spread(js.ident("items"))])
    /// ```
    pub fn spread(&self, expr: Expression<'a>) -> SpreadElement<'a> {
        SpreadElement {
            span: SPAN,
            argument: expr,
        }
    }
}

impl<'a> Default for JsBuilder<'a> {
    fn default() -> Self {
        panic!("JsBuilder requires an allocator - use JsBuilder::new(allocator)");
    }
}