Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!(codegen): remove the unecessary 4th argument from Codegen::new #3640

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ fn main() -> std::io::Result<()> {

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

let ret = Parser::new(&allocator, &printed, source_type).parse();
let minified =
Codegen::<true>::new("", &source_text, options, None).build(&ret.program).source_text;
let minified = Codegen::<true>::new("", &source_text, ret.trivias, options)
.build(&ret.program)
.source_text;
println!("Minified:");
println!("{minified}");

Expand Down
10 changes: 7 additions & 3 deletions crates/oxc_codegen/examples/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ fn main() -> std::io::Result<()> {
let codegen_options =
CodegenOptions { enable_source_map: true, enable_typescript: true, ..Default::default() };

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

if let Some(source_map) = source_map {
let result = source_map.to_json_string().unwrap();
Expand Down
38 changes: 16 additions & 22 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub use crate::{
};
// use crate::mangler::Mangler;

pub type MoveCommentMap = FxHashMap<u32, (u32, Comment)>;

#[derive(Debug, Default, Clone, Copy)]
pub struct CodegenOptions {
/// Pass in the filename to enable source map support.
Expand Down Expand Up @@ -66,9 +68,12 @@ pub struct CodegenReturn {
}

pub struct Codegen<'a, const MINIFY: bool> {
#[allow(unused)]
options: CodegenOptions,

source_code: &'a str,

trivias: Trivias,

// mangler: Option<Mangler>,
/// Output Code
code: Vec<u8>,
Expand All @@ -90,17 +95,13 @@ pub struct Codegen<'a, const MINIFY: bool> {
/// Track the current indentation level
indentation: u8,

// Builders
sourcemap_builder: Option<SourcemapBuilder>,
comment_gen_related: Option<CommentGenRelated>,
source_code: &'a str,
}

pub struct CommentGenRelated {
pub trivials: Trivias,
/// The key of map is the node start position,
/// the first element of value is the start of the comment
/// the second element of value includes the end of the comment and comment kind.
pub move_comment_map: FxHashMap<u32, (u32, Comment)>,
move_comment_map: MoveCommentMap,
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -114,8 +115,8 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
pub fn new(
source_name: &str,
source_code: &'a str,
trivias: Trivias,
options: CodegenOptions,
comment_gen_related: Option<CommentGenRelated>,
) -> Self {
// Initialize the output code buffer to reduce memory reallocation.
// Minification will reduce by at least half of the original size.
Expand All @@ -130,6 +131,8 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {

Self {
options,
source_code,
trivias,
// mangler: None,
code: Vec::with_capacity(capacity),
needs_semicolon: false,
Expand All @@ -142,8 +145,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
start_of_default_export: 0,
indentation: 0,
sourcemap_builder,
comment_gen_related,
source_code,
move_comment_map: MoveCommentMap::default(),
}
}

Expand Down Expand Up @@ -205,27 +207,19 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
/// }, b = 10000;
/// ```
pub fn move_comment(&mut self, position: u32, full_comment_info: (u32, Comment)) {
if let Some(comment_gen_related) = &mut self.comment_gen_related {
comment_gen_related.move_comment_map.insert(position, full_comment_info);
}
self.move_comment_map.insert(position, full_comment_info);
}

pub fn try_get_leading_comment(&self, start: u32) -> Option<(&u32, &Comment)> {
self.comment_gen_related.as_ref().and_then(|comment_gen_related| {
comment_gen_related.trivials.comments_range(0..start).next_back()
})
self.trivias.comments_range(0..start).next_back()
}

pub fn try_take_moved_comment(&mut self, node_start: u32) -> Option<(u32, Comment)> {
self.comment_gen_related.as_mut().and_then(|comment_gen_related| {
comment_gen_related.move_comment_map.remove(&node_start)
})
self.move_comment_map.remove(&node_start)
}

pub fn try_get_leading_comment_from_move_map(&self, start: u32) -> Option<&(u32, Comment)> {
self.comment_gen_related
.as_ref()
.and_then(|comment_gen_related| comment_gen_related.move_comment_map.get(&start))
self.move_comment_map.get(&start)
}

fn print_soft_space(&mut self) {
Expand Down
28 changes: 9 additions & 19 deletions crates/oxc_codegen/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@ use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
use rustc_hash::FxHashMap;

fn test(source_text: &str, expected: &str, options: Option<CodegenOptions>) {
let allocator = Allocator::default();
let source_type = SourceType::default().with_module(true);
let parse_return = Parser::new(&allocator, source_text, source_type).parse();
let program = parse_return.program;
let program = allocator.alloc(program);
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let options = options.unwrap_or_default();
let result = Codegen::<false>::new(
"",
source_text,
options,
Some(oxc_codegen::CommentGenRelated {
trivials: parse_return.trivias,
move_comment_map: FxHashMap::default(),
}),
)
.build(program)
.source_text;
let result =
Codegen::<false>::new("", source_text, ret.trivias, options).build(program).source_text;
assert_eq!(expected, result, "for source {source_text}, expect {expected}, got {result}");
}

Expand All @@ -31,11 +20,12 @@ fn test_ts(source_text: &str, expected: &str, is_typescript_definition: bool) {
.with_typescript(true)
.with_typescript_definition(is_typescript_definition)
.with_module(true);
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let codegen_options = CodegenOptions { enable_typescript: true, ..CodegenOptions::default() };
let result =
Codegen::<false>::new("", source_text, codegen_options, None).build(program).source_text;
let result = Codegen::<false>::new("", source_text, ret.trivias, codegen_options)
.build(program)
.source_text;
assert_eq!(expected, result, "for source {source_text}, expect {expected}, got {result}");
}

Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/fixer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;

use oxc_ast::Trivias;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{GetSpan, Span};
Expand Down Expand Up @@ -64,7 +65,7 @@ impl<'c, 'a: 'c> RuleFixer<'c, 'a> {

#[allow(clippy::unused_self)]
pub fn codegen(self) -> Codegen<'a, false> {
Codegen::<false>::new("", "", CodegenOptions::default(), None)
Codegen::<false>::new("", "", Trivias::default(), CodegenOptions::default())
}
}

Expand Down
9 changes: 5 additions & 4 deletions crates/oxc_minifier/examples/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ fn main() -> std::io::Result<()> {

fn minify(source_text: &str, source_type: SourceType, mangle: bool, whitespace: bool) -> String {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let options = MinifierOptions { mangle, ..MinifierOptions::default() };
Minifier::new(options).build(&allocator, program);
if whitespace {
Codegen::<true>::new("", source_text, CodegenOptions::default(), None).build(program)
Codegen::<true>::new("", source_text, ret.trivias, CodegenOptions::default()).build(program)
} else {
Codegen::<false>::new("", source_text, CodegenOptions::default(), None).build(program)
Codegen::<false>::new("", source_text, ret.trivias, CodegenOptions::default())
.build(program)
}
.source_text
}
6 changes: 3 additions & 3 deletions crates/oxc_minifier/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub(crate) fn minify(
options: MinifierOptions,
) -> String {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
Minifier::new(options).build(&allocator, program);
Codegen::<true>::new("", source_text, CodegenOptions::default(), None)
Codegen::<true>::new("", source_text, ret.trivias, CodegenOptions::default())
.build(program)
.source_text
}
Expand Down
15 changes: 11 additions & 4 deletions crates/oxc_transformer/examples/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ fn main() {
},
..Default::default()
};
Transformer::new(&allocator, path, source_type, &source_text, ret.trivias, transform_options)
.build(&mut program)
.unwrap();
Transformer::new(
&allocator,
path,
source_type,
&source_text,
ret.trivias.clone(),
transform_options,
)
.build(&mut program)
.unwrap();

let printed = Codegen::<false>::new("", &source_text, CodegenOptions::default(), None)
let printed = Codegen::<false>::new("", &source_text, ret.trivias, CodegenOptions::default())
.build(&program)
.source_text;
println!("Transformed:\n");
Expand Down
20 changes: 15 additions & 5 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,15 @@ impl Oxc {

if run_options.transform() {
let options = TransformOptions::default();
let result =
Transformer::new(&allocator, &path, source_type, source_text, ret.trivias, options)
.build(program);
let result = Transformer::new(
&allocator,
&path,
source_type,
source_text,
ret.trivias.clone(),
options,
)
.build(program);
if let Err(errs) = result {
self.save_diagnostics(errs);
}
Expand Down Expand Up @@ -273,9 +279,13 @@ impl Oxc {
..CodegenOptions::default()
};
self.codegen_text = if minifier_options.whitespace() {
Codegen::<true>::new("", source_text, codegen_options, None).build(program).source_text
Codegen::<true>::new("", source_text, ret.trivias, codegen_options)
.build(program)
.source_text
} else {
Codegen::<false>::new("", source_text, codegen_options, None).build(program).source_text
Codegen::<false>::new("", source_text, ret.trivias, codegen_options)
.build(program)
.source_text
};

Ok(())
Expand Down
13 changes: 9 additions & 4 deletions tasks/benchmark/benches/codegen_sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ fn bench_codegen_sourcemap(criterion: &mut Criterion) {
let source_type = SourceType::from_path(&file.file_name).unwrap();
group.bench_with_input(id, &file.source_text, |b, source_text| {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let ret = Parser::new(&allocator, source_text, source_type).parse();
let codegen_options =
CodegenOptions { enable_source_map: true, ..CodegenOptions::default() };
b.iter_with_large_drop(|| {
Codegen::<false>::new(file.file_name.as_str(), source_text, codegen_options, None)
.build(&program)
.source_map
Codegen::<false>::new(
file.file_name.as_str(),
source_text,
ret.trivias.clone(),
codegen_options,
)
.build(&ret.program)
.source_map
});
});
}
Expand Down
6 changes: 3 additions & 3 deletions tasks/benchmark/benches/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ fn bench_sourcemap(criterion: &mut Criterion) {
let source_type = SourceType::from_path(&file.file_name).unwrap();
group.bench_with_input(id, &file.source_text, |b, source_text| {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let ret = Parser::new(&allocator, source_text, source_type).parse();
let codegen_options =
CodegenOptions { enable_source_map: true, ..CodegenOptions::default() };
b.iter(|| {
let CodegenReturn { source_map, source_text } = Codegen::<false>::new(
file.file_name.as_str(),
source_text,
ret.trivias.clone(),
codegen_options,
None,
)
.build(&program);
.build(&ret.program);
let line = source_text.matches('\n').count() as u32;
if let Some(sourcemap) = source_map {
let mut concat_sourcemap_builder = ConcatSourceMapBuilder::default();
Expand Down
13 changes: 7 additions & 6 deletions tasks/coverage/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ fn get_normal_result(
let options = CodegenOptions::default().with_typescript(source_type.is_typescript());
let allocator = Allocator::default();
let parse_result1 = Parser::new(&allocator, source_text, source_type).parse();
let source_text1 = Codegen::<false>::new("", source_text, options, None)
let source_text1 = Codegen::<false>::new("", source_text, parse_result1.trivias, options)
.build(&parse_result1.program)
.source_text;
let parse_result2 = Parser::new(&allocator, &source_text1, source_type).parse();
let source_text2 = Codegen::<false>::new("", &source_text1, options, None)
let source_text2 = Codegen::<false>::new("", &source_text1, parse_result2.trivias, options)
.build(&parse_result2.program)
.source_text;
let result = source_text1 == source_text2;
Expand Down Expand Up @@ -111,11 +111,12 @@ fn get_minify_result(
let options = CodegenOptions::default().with_typescript(source_type.is_typescript());
let allocator = Allocator::default();
let parse_result1 = Parser::new(&allocator, source_text, source_type).parse();
let source_text1 = Codegen::<true>::new("", source_text, options, None)
.build(&parse_result1.program)
.source_text;
let source_text1 =
Codegen::<true>::new("", source_text, parse_result1.trivias.clone(), options)
.build(&parse_result1.program)
.source_text;
let parse_result2 = Parser::new(&allocator, source_text1.as_str(), source_type).parse();
let source_text2 = Codegen::<true>::new("", &source_text1, options, None)
let source_text2 = Codegen::<true>::new("", &source_text1, parse_result2.trivias, options)
.build(&parse_result2.program)
.source_text;
let result = source_text1 == source_text2;
Expand Down
6 changes: 3 additions & 3 deletions tasks/coverage/src/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ fn get_result(source_text: &str, source_type: SourceType) -> TestResult {

fn minify(source_text: &str, source_type: SourceType, options: MinifierOptions) -> String {
let allocator = Allocator::default();
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
Minifier::new(options).build(&allocator, program);
Codegen::<true>::new("", source_text, CodegenOptions::default(), None)
Codegen::<true>::new("", source_text, ret.trivias, CodegenOptions::default())
.build(program)
.source_text
}
Loading