pub struct JsBuilder<'a> { /* private fields */ }Expand description
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!"));Implementations§
Source§impl<'a> JsBuilder<'a>
impl<'a> JsBuilder<'a>
Sourcepub fn ast(&self) -> &AstBuilder<'a>
pub fn ast(&self) -> &AstBuilder<'a>
Get the underlying AstBuilder (for advanced usage)
Sourcepub fn ident(&self, name: impl Into<Atom<'a>>) -> Expression<'a>
pub fn ident(&self, name: impl Into<Atom<'a>>) -> Expression<'a>
Create an identifier expression: name
Sourcepub fn string(&self, value: impl Into<Atom<'a>>) -> Expression<'a>
pub fn string(&self, value: impl Into<Atom<'a>>) -> Expression<'a>
Create a string literal: "value"
Sourcepub fn number(&self, value: f64) -> Expression<'a>
pub fn number(&self, value: f64) -> Expression<'a>
Create a number literal: 42
Sourcepub fn null(&self) -> Expression<'a>
pub fn null(&self) -> Expression<'a>
Create null: null
Sourcepub fn bool(&self, value: bool) -> Expression<'a>
pub fn bool(&self, value: bool) -> Expression<'a>
Create boolean: true or false
Sourcepub fn member(
&self,
object: Expression<'a>,
property: impl Into<Atom<'a>>,
) -> Expression<'a>
pub fn member( &self, object: Expression<'a>, property: impl Into<Atom<'a>>, ) -> Expression<'a>
Create a member expression: obj.prop
Sourcepub fn computed_member(
&self,
object: Expression<'a>,
index: Expression<'a>,
) -> Expression<'a>
pub fn computed_member( &self, object: Expression<'a>, index: Expression<'a>, ) -> Expression<'a>
Create a computed member expression: obj[expr]
This is critical for array indexing: arr[0], pageRoutes[idx], etc.
Sourcepub fn call(
&self,
callee: Expression<'a>,
args: Vec<Argument<'a>>,
) -> Expression<'a>
pub fn call( &self, callee: Expression<'a>, args: Vec<Argument<'a>>, ) -> Expression<'a>
Create a call expression: callee(args...)
Sourcepub fn arg(&self, expr: Expression<'a>) -> Argument<'a>
pub fn arg(&self, expr: Expression<'a>) -> Argument<'a>
Create an argument from an expression
Sourcepub fn object(&self, props: Vec<ObjectPropertyKind<'a>>) -> Expression<'a>
pub fn object(&self, props: Vec<ObjectPropertyKind<'a>>) -> Expression<'a>
Create an object expression: { key1: value1, key2: value2 }
Sourcepub fn prop(
&self,
key: impl Into<Atom<'a>>,
value: Expression<'a>,
) -> ObjectPropertyKind<'a>
pub fn prop( &self, key: impl Into<Atom<'a>>, value: Expression<'a>, ) -> ObjectPropertyKind<'a>
Create an object property: key: value
Sourcepub fn array(&self, elements: Vec<Expression<'a>>) -> Expression<'a>
pub fn array(&self, elements: Vec<Expression<'a>>) -> Expression<'a>
Create an array expression: [elem1, elem2, ...]
Sourcepub fn arrow_fn(
&self,
params: Vec<&'a str>,
body: Expression<'a>,
) -> Expression<'a>
pub fn arrow_fn( &self, params: Vec<&'a str>, body: Expression<'a>, ) -> Expression<'a>
Create an arrow function with expression body: (params) => expr
Sourcepub fn arrow_fn_block(
&self,
params: Vec<&'a str>,
stmts: Vec<Statement<'a>>,
) -> Expression<'a>
pub fn arrow_fn_block( &self, params: Vec<&'a str>, stmts: Vec<Statement<'a>>, ) -> Expression<'a>
Create an arrow function with block body: (params) => { stmts }
Sourcepub fn const_decl(
&self,
name: impl Into<Atom<'a>>,
init: Expression<'a>,
) -> Statement<'a>
pub fn const_decl( &self, name: impl Into<Atom<'a>>, init: Expression<'a>, ) -> Statement<'a>
Create a const declaration: const name = init;
Sourcepub fn let_decl(
&self,
name: impl Into<Atom<'a>>,
init: Option<Expression<'a>>,
) -> Statement<'a>
pub fn let_decl( &self, name: impl Into<Atom<'a>>, init: Option<Expression<'a>>, ) -> Statement<'a>
Create a let declaration: let name = init;
Sourcepub fn expr_stmt(&self, expr: Expression<'a>) -> Statement<'a>
pub fn expr_stmt(&self, expr: Expression<'a>) -> Statement<'a>
Create an expression statement
Sourcepub fn if_stmt(
&self,
test: Expression<'a>,
consequent: Vec<Statement<'a>>,
alternate: Option<Vec<Statement<'a>>>,
) -> Statement<'a>
pub fn if_stmt( &self, test: Expression<'a>, consequent: Vec<Statement<'a>>, alternate: Option<Vec<Statement<'a>>>, ) -> Statement<'a>
Create an if statement: if (test) { consequent } else { alternate }
Sourcepub fn return_stmt(&self, expr: Option<Expression<'a>>) -> Statement<'a>
pub fn return_stmt(&self, expr: Option<Expression<'a>>) -> Statement<'a>
Create a return statement: return expr;
Sourcepub fn throw(&self, expr: Expression<'a>) -> Statement<'a>
pub fn throw(&self, expr: Expression<'a>) -> Statement<'a>
Create a throw statement: throw expr;
Sourcepub fn import_default(
&self,
local: impl Into<Atom<'a>>,
source: impl Into<Atom<'a>>,
) -> ModuleDeclaration<'a>
pub fn import_default( &self, local: impl Into<Atom<'a>>, source: impl Into<Atom<'a>>, ) -> ModuleDeclaration<'a>
Create default import: import local from 'source';
Sourcepub fn import_side_effect(
&self,
source: impl Into<Atom<'a>>,
) -> ModuleDeclaration<'a>
pub fn import_side_effect( &self, source: impl Into<Atom<'a>>, ) -> ModuleDeclaration<'a>
Create side-effect import: import 'source';
Sourcepub fn import_named(
&self,
names: Vec<impl Into<Atom<'a>>>,
source: impl Into<Atom<'a>>,
) -> ModuleDeclaration<'a>
pub fn import_named( &self, names: Vec<impl Into<Atom<'a>>>, source: impl Into<Atom<'a>>, ) -> ModuleDeclaration<'a>
Create named imports: import { name1, name2 } from 'source';
Sourcepub fn export_default(&self, expr: Expression<'a>) -> ModuleDeclaration<'a>
pub fn export_default(&self, expr: Expression<'a>) -> ModuleDeclaration<'a>
Create default export: export default expr;
Sourcepub fn export_const(
&self,
name: impl Into<Atom<'a>>,
init: Expression<'a>,
) -> ModuleDeclaration<'a>
pub fn export_const( &self, name: impl Into<Atom<'a>>, init: Expression<'a>, ) -> ModuleDeclaration<'a>
Create named export of variable: export const name = init;
Sourcepub fn program(&self, statements: Vec<Statement<'a>>) -> Result<String>
pub fn program(&self, statements: Vec<Statement<'a>>) -> Result<String>
Generate a complete program from statements
Sourcepub fn program_with_format(
&self,
statements: Vec<Statement<'a>>,
_opts: &FormatOptions,
) -> Result<String>
pub fn program_with_format( &self, statements: Vec<Statement<'a>>, _opts: &FormatOptions, ) -> Result<String>
Generate a complete program from statements with formatting options
Sourcepub fn not(&self, expr: Expression<'a>) -> Expression<'a>
pub fn not(&self, expr: Expression<'a>) -> Expression<'a>
Unary not operator: !expr
Sourcepub fn binary(
&self,
left: Expression<'a>,
op: BinaryOperator,
right: Expression<'a>,
) -> Expression<'a>
pub fn binary( &self, left: Expression<'a>, op: BinaryOperator, right: Expression<'a>, ) -> Expression<'a>
Binary expression: left op right
Sourcepub fn logical(
&self,
left: Expression<'a>,
op: LogicalOperator,
right: Expression<'a>,
) -> Expression<'a>
pub fn logical( &self, left: Expression<'a>, op: LogicalOperator, right: Expression<'a>, ) -> Expression<'a>
Logical expression: left && right or left || right
Sourcepub fn conditional(
&self,
test: Expression<'a>,
consequent: Expression<'a>,
alternate: Expression<'a>,
) -> Expression<'a>
pub fn conditional( &self, test: Expression<'a>, consequent: Expression<'a>, alternate: Expression<'a>, ) -> Expression<'a>
Conditional/ternary expression: test ? consequent : alternate
Sourcepub fn new_expr(
&self,
callee: Expression<'a>,
args: Vec<Argument<'a>>,
) -> Expression<'a>
pub fn new_expr( &self, callee: Expression<'a>, args: Vec<Argument<'a>>, ) -> Expression<'a>
New expression: new Ctor(args)
Sourcepub fn template_literal(
&self,
parts: Vec<impl Into<Atom<'a>>>,
expressions: Vec<Expression<'a>>,
) -> Expression<'a>
pub fn template_literal( &self, parts: Vec<impl Into<Atom<'a>>>, expressions: Vec<Expression<'a>>, ) -> Expression<'a>
Sourcepub fn spread(&self, expr: Expression<'a>) -> SpreadElement<'a>
pub fn spread(&self, expr: Expression<'a>) -> SpreadElement<'a>
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for JsBuilder<'a>
impl<'a> !RefUnwindSafe for JsBuilder<'a>
impl<'a> !Send for JsBuilder<'a>
impl<'a> !Sync for JsBuilder<'a>
impl<'a> Unpin for JsBuilder<'a>
impl<'a> !UnwindSafe for JsBuilder<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more