diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 620dbdf392165..3201ec5a40275 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -98,7 +98,9 @@ impl Gen for Directive<'_> { impl Gen for Statement<'_> { fn gen(&self, p: &mut Codegen, ctx: Context) { - p.print_statement_comments(self.span().start); + if p.print_comments { + p.print_statement_comments(self.span().start); + } match self { Self::BlockStatement(stmt) => stmt.print(p, ctx), Self::BreakStatement(stmt) => stmt.print(p, ctx), @@ -686,7 +688,7 @@ impl Gen for Function<'_> { impl Gen for FunctionBody<'_> { fn gen(&self, p: &mut Codegen, ctx: Context) { let span_end = self.span.end; - let comments_at_end = if !p.options.minify && span_end > 0 { + let comments_at_end = if p.print_comments && span_end > 0 { p.get_statement_comments(span_end - 1) } else { None diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index 0b6f9054d107d..5ec657a3790c4 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -104,6 +104,8 @@ pub struct Codegen<'a> { /// Fast path for [CodegenOptions::single_quote] quote: u8, + /// Fast path for if print comments + print_comments: bool, // Builders comments: CommentsMap, @@ -151,8 +153,10 @@ impl<'a> Codegen<'a> { /// This is equivalent to [`Codegen::default`]. #[must_use] pub fn new() -> Self { + let options = CodegenOptions::default(); + let print_comments = options.print_comments(); Self { - options: CodegenOptions::default(), + options, source_text: "", mangler: None, code: CodeBuffer::default(), @@ -168,6 +172,7 @@ impl<'a> Codegen<'a> { start_of_default_export: 0, indent: 0, quote: b'"', + print_comments, comments: CommentsMap::default(), start_of_annotation_comment: None, legal_comments: vec![], @@ -179,6 +184,7 @@ impl<'a> Codegen<'a> { #[must_use] pub fn with_options(mut self, options: CodegenOptions) -> Self { self.quote = if options.single_quote { b'\'' } else { b'"' }; + self.print_comments = options.print_comments(); self.options = options; self }