Skip to main content

Codegen

Struct Codegen 

Source
pub struct Codegen<'a> { /* private fields */ }
Expand description

A code generator for printing JavaScript and TypeScript code.

§Example

use oxc_codegen::{Codegen, CodegenOptions};
use oxc_ast::ast::Program;
use oxc_parser::Parser;
use oxc_allocator::Allocator;
use oxc_span::SourceType;

let allocator = Allocator::default();
let source = "const a = 1 + 2;";
let parsed = Parser::new(&allocator, source, SourceType::mjs()).parse();
assert!(parsed.diagnostics.is_empty());

let js = Codegen::new().build(&parsed.program);
assert_eq!(js.code, "const a = 1 + 2;\n");

Implementations§

Source§

impl<'a> Codegen<'a>

Source

pub fn new() -> Self

Create a new code generator.

This is equivalent to Codegen::default.

Source

pub fn with_options(self, options: CodegenOptions) -> Self

Pass options to the code generator.

Source

pub fn with_source_text(self, source_text: &'a str) -> Self

Sets the source text for the code generator.

Source

pub fn with_source_type(self, source_type: SourceType) -> Self

Sets the source type for code fragments printed without a Program.

Source

pub fn with_scoping(self, scoping: Option<Scoping>) -> Self

Set the symbol table used for identifier renaming.

Can be used for easy renaming of variables (based on semantic analysis).

Source

pub fn with_private_member_mappings( self, mappings: Option<IndexVec<ClassId, FxHashMap<String, CompactStr>>>, ) -> Self

Set private member name mappings for mangling.

This allows renaming of private class members like #field -> #a. The Vec contains per-class mappings, indexed by class declaration order.

Source

pub fn build(self, program: &Program<'a>) -> CodegenReturn<'a>

Print a Program into a string of source code.

A source map will be generated if CodegenOptions::source_map_path is set.

Source

pub fn into_source_text(self) -> String

Turn what’s been built so far into a string. Like build, this fininishes a print and returns the generated source code. Unlike build, no source map is generated.

This is more useful for cases that progressively build code using print_expression.

Source

pub fn print_ascii_byte(&mut self, byte: u8)

Push a single ASCII byte into the buffer.

§Panics

Panics if byte is not an ASCII byte (0 - 0x7F).

Source

pub fn print_str(&mut self, s: &str)

Push str into the buffer

Source

pub fn print_name(&mut self, s: &str)

Print an identifier name (binding, reference, label, private name, property key), \u-escaping non-ASCII characters when CodegenOptions::ascii_only is set.

Source

pub fn print_str_escaping_script_close_tag(&mut self, s: &str)

Push str into the buffer, escaping </script to <\/script.

Source

pub fn print_expression(&mut self, expr: &Expression<'_>)

Print a single Expression, adding it to the code generator’s internal buffer. Unlike Codegen::build, this does not consume self.

Source

pub fn print_string(&mut self, s: &str)

Print a string as a JavaScript string literal.

Trait Implementations§

Source§

impl Default for Codegen<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> From<Codegen<'a>> for String

Source§

fn from(val: Codegen<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Codegen<'a>> for Cow<'a, str>

Source§

fn from(val: Codegen<'a>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Codegen<'a>

§

impl<'a> !Send for Codegen<'a>

§

impl<'a> !Sync for Codegen<'a>

§

impl<'a> !UnwindSafe for Codegen<'a>

§

impl<'a> Freeze for Codegen<'a>

§

impl<'a> Unpin for Codegen<'a>

§

impl<'a> UnsafeUnpin for Codegen<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'a, T> FromIn<'a, T> for T

Source§

fn from_in(t: T, _: &'a Allocator) -> T

Converts to this type from the input type within the given allocator.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<'a, T, U> IntoIn<'a, U> for T
where U: FromIn<'a, T>,

Source§

fn into_in(self, allocator: &'a Allocator) -> U

Converts this type into the (usually inferred) input type within the given allocator.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.