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>
impl<'a> Codegen<'a>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new code generator.
This is equivalent to Codegen::default.
Sourcepub fn with_options(self, options: CodegenOptions) -> Self
pub fn with_options(self, options: CodegenOptions) -> Self
Pass options to the code generator.
Sourcepub fn with_source_text(self, source_text: &'a str) -> Self
pub fn with_source_text(self, source_text: &'a str) -> Self
Sets the source text for the code generator.
Sourcepub fn with_source_type(self, source_type: SourceType) -> Self
pub fn with_source_type(self, source_type: SourceType) -> Self
Sets the source type for code fragments printed without a Program.
Sourcepub fn with_scoping(self, scoping: Option<Scoping>) -> Self
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).
Sourcepub fn with_private_member_mappings(
self,
mappings: Option<IndexVec<ClassId, FxHashMap<String, CompactStr>>>,
) -> Self
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.
Sourcepub fn build(self, program: &Program<'a>) -> CodegenReturn<'a>
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.
Sourcepub fn into_source_text(self) -> String
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.
Sourcepub fn print_ascii_byte(&mut self, byte: u8)
pub fn print_ascii_byte(&mut self, byte: u8)
Sourcepub fn print_name(&mut self, s: &str)
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.
Sourcepub fn print_str_escaping_script_close_tag(&mut self, s: &str)
pub fn print_str_escaping_script_close_tag(&mut self, s: &str)
Push str into the buffer, escaping </script to <\/script.
Sourcepub fn print_expression(&mut self, expr: &Expression<'_>)
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.
Sourcepub fn print_string(&mut self, s: &str)
pub fn print_string(&mut self, s: &str)
Print a string as a JavaScript string literal.
Trait Implementations§
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> 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 more