Skip to content

Commit

Permalink
feat(codegen)!: new code gen API (#3740)
Browse files Browse the repository at this point in the history
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);
```
  • Loading branch information
Boshen committed Jun 18, 2024
1 parent 527bfc8 commit 5c38a0f
Show file tree
Hide file tree
Showing 26 changed files with 296 additions and 410 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{env, path::Path};

use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_codegen::{CodeGenerator, WhitespaceRemover};
use oxc_parser::Parser;
use oxc_span::SourceType;

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

let options = CodegenOptions::default();
let printed = Codegen::<false>::new("", &source_text, ret.trivias.clone(), options)
.build(&ret.program)
.source_text;
let printed = CodeGenerator::new().build(&ret.program).source_text;
println!("Printed:");
println!("{printed}");

let minified = Codegen::<true>::new("", &source_text, ret.trivias, options)
.build(&ret.program)
.source_text;
let minified = WhitespaceRemover::new().build(&ret.program).source_text;
println!("Minified:");
println!("{minified}");

Expand Down
14 changes: 4 additions & 10 deletions crates/oxc_codegen/examples/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{env, path::Path};

use base64::{prelude::BASE64_STANDARD, Engine};
use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions, CodegenReturn};
use oxc_codegen::{CodeGenerator, CodegenReturn};
use oxc_parser::Parser;
use oxc_span::SourceType;

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

let codegen_options = CodegenOptions { enable_source_map: true, ..Default::default() };

let CodegenReturn { source_text, source_map } = Codegen::<false>::new(
path.to_string_lossy().as_ref(),
&source_text,
ret.trivias,
codegen_options,
)
.build(&ret.program);
let CodegenReturn { source_text, source_map } = CodeGenerator::new()
.enable_source_map(path.to_string_lossy().as_ref(), &source_text)
.build(&ret.program);

if let Some(source_map) = source_map {
let result = source_map.to_json_string().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/annotation_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn print_comment<const MINIFY: bool>(
}

pub fn gen_comment<const MINIFY: bool>(node_start: u32, codegen: &mut Codegen<{ MINIFY }>) {
if !codegen.options.preserve_annotate_comments {
if !codegen.comment_options.preserve_annotate_comments {
return;
}
if let Some((comment_start, comment)) = codegen.try_take_moved_comment(node_start) {
Expand Down
10 changes: 6 additions & 4 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use oxc_syntax::{
};

use crate::annotation_comment::{gen_comment, get_leading_annotate_comment};
use crate::{Codegen, Context, Operator, Separator};
use crate::{Codegen, Context, Operator};

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

if p.options.preserve_annotate_comments
if p.comment_options.preserve_annotate_comments
&& matches!(self.kind, VariableDeclarationKind::Const)
{
if let Some(declarator) = self.declarations.first() {
Expand Down Expand Up @@ -823,7 +823,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for WithClause<'a> {
p.add_source_mapping(self.span.start);
self.attributes_keyword.gen(p, ctx);
p.print_soft_space();
p.print_block(&self.with_entries, Separator::Comma, ctx, self.span);
p.print_block_start(self.span.start);
p.print_sequence(&self.with_entries, ctx);
p.print_block_end(self.span.end);
}
}

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

0 comments on commit 5c38a0f

Please sign in to comment.