Skip to content

Commit 5c38a0f

Browse files
committed
feat(codegen)!: new code gen API (#3740)
This PR introduces two type alias to avoid the confusing const generic `pub struct Codegen<'a, const MINIFY: bool>` * CodeGenerator - Code generator without whitespace removal. * WhitespaceRemover - Code generator with whitespace removal. Usage is changed to a builder pattern: ```rust CodeGenerator::new() .enable_comment(...) .enable_sourcemap(...) .build(&program); ```
1 parent 527bfc8 commit 5c38a0f

File tree

26 files changed

+296
-410
lines changed

26 files changed

+296
-410
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_codegen/examples/codegen.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::{env, path::Path};
33

44
use oxc_allocator::Allocator;
5-
use oxc_codegen::{Codegen, CodegenOptions};
5+
use oxc_codegen::{CodeGenerator, WhitespaceRemover};
66
use oxc_parser::Parser;
77
use oxc_span::SourceType;
88

@@ -29,16 +29,11 @@ fn main() -> std::io::Result<()> {
2929
println!("Original:");
3030
println!("{source_text}");
3131

32-
let options = CodegenOptions::default();
33-
let printed = Codegen::<false>::new("", &source_text, ret.trivias.clone(), options)
34-
.build(&ret.program)
35-
.source_text;
32+
let printed = CodeGenerator::new().build(&ret.program).source_text;
3633
println!("Printed:");
3734
println!("{printed}");
3835

39-
let minified = Codegen::<true>::new("", &source_text, ret.trivias, options)
40-
.build(&ret.program)
41-
.source_text;
36+
let minified = WhitespaceRemover::new().build(&ret.program).source_text;
4237
println!("Minified:");
4338
println!("{minified}");
4439

crates/oxc_codegen/examples/sourcemap.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{env, path::Path};
33

44
use base64::{prelude::BASE64_STANDARD, Engine};
55
use oxc_allocator::Allocator;
6-
use oxc_codegen::{Codegen, CodegenOptions, CodegenReturn};
6+
use oxc_codegen::{CodeGenerator, CodegenReturn};
77
use oxc_parser::Parser;
88
use oxc_span::SourceType;
99

@@ -27,15 +27,9 @@ fn main() -> std::io::Result<()> {
2727
return Ok(());
2828
}
2929

30-
let codegen_options = CodegenOptions { enable_source_map: true, ..Default::default() };
31-
32-
let CodegenReturn { source_text, source_map } = Codegen::<false>::new(
33-
path.to_string_lossy().as_ref(),
34-
&source_text,
35-
ret.trivias,
36-
codegen_options,
37-
)
38-
.build(&ret.program);
30+
let CodegenReturn { source_text, source_map } = CodeGenerator::new()
31+
.enable_source_map(path.to_string_lossy().as_ref(), &source_text)
32+
.build(&ret.program);
3933

4034
if let Some(source_map) = source_map {
4135
let result = source_map.to_json_string().unwrap();

crates/oxc_codegen/src/annotation_comment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn print_comment<const MINIFY: bool>(
5555
}
5656

5757
pub fn gen_comment<const MINIFY: bool>(node_start: u32, codegen: &mut Codegen<{ MINIFY }>) {
58-
if !codegen.options.preserve_annotate_comments {
58+
if !codegen.comment_options.preserve_annotate_comments {
5959
return;
6060
}
6161
if let Some((comment_start, comment)) = codegen.try_take_moved_comment(node_start) {

crates/oxc_codegen/src/gen.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use oxc_syntax::{
1111
};
1212

1313
use crate::annotation_comment::{gen_comment, get_leading_annotate_comment};
14-
use crate::{Codegen, Context, Operator, Separator};
14+
use crate::{Codegen, Context, Operator};
1515

1616
pub trait Gen<const MINIFY: bool> {
1717
fn gen(&self, _p: &mut Codegen<{ MINIFY }>, _ctx: Context) {}
@@ -591,7 +591,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for VariableDeclaration<'a> {
591591
p.print_str(b"declare ");
592592
}
593593

594-
if p.options.preserve_annotate_comments
594+
if p.comment_options.preserve_annotate_comments
595595
&& matches!(self.kind, VariableDeclarationKind::Const)
596596
{
597597
if let Some(declarator) = self.declarations.first() {
@@ -823,7 +823,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for WithClause<'a> {
823823
p.add_source_mapping(self.span.start);
824824
self.attributes_keyword.gen(p, ctx);
825825
p.print_soft_space();
826-
p.print_block(&self.with_entries, Separator::Comma, ctx, self.span);
826+
p.print_block_start(self.span.start);
827+
p.print_sequence(&self.with_entries, ctx);
828+
p.print_block_end(self.span.end);
827829
}
828830
}
829831

@@ -845,7 +847,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for ExportNamedDeclaration<'a> {
845847
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
846848
p.add_source_mapping(self.span.start);
847849
p.print_indent();
848-
if p.options.preserve_annotate_comments {
850+
if p.comment_options.preserve_annotate_comments {
849851
match &self.declaration {
850852
Some(Declaration::FunctionDeclaration(_)) => {
851853
gen_comment(self.span.start, p);

0 commit comments

Comments
 (0)