diff --git a/crates/oxc_codegen/src/comment.rs b/crates/oxc_codegen/src/comment.rs index 93a75c0708a19..3cdc69f25e0ca 100644 --- a/crates/oxc_codegen/src/comment.rs +++ b/crates/oxc_codegen/src/comment.rs @@ -1,6 +1,7 @@ +use oxc_span::{GetSpan, Span}; use rustc_hash::FxHashMap; -use oxc_ast::{Comment, CommentKind}; +use oxc_ast::{Comment, CommentKind, ast::Argument}; use oxc_syntax::identifier::is_line_terminator; use crate::{Codegen, LegalComment}; @@ -18,14 +19,19 @@ impl Codegen<'_> { self.comments.contains_key(&start) } - pub(crate) fn has_non_annotation_comment(&self, start: u32) -> bool { - if self.options.print_annotation_comments() { - self.comments.get(&start).is_some_and(|comments| { - comments.iter().any(|comment| !self.is_pure_comment(comment)) - }) - } else { - self.has_comment(start) - } + pub(crate) fn contains_comment_in_call_like_expression( + &self, + span: Span, + arguments: &[Argument<'_>], + ) -> (bool, bool) { + let has_comment_before_right_paren = + self.print_comments && span.end > 0 && self.has_comment(span.end - 1); + + let has_comment = has_comment_before_right_paren + || self.print_comments + && arguments.iter().any(|item| self.has_comment(item.span().start)); + + (has_comment, has_comment_before_right_paren) } /// `#__PURE__` Notation Specification diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 5a743258f5563..86eeeec0bd319 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1452,15 +1452,12 @@ impl GenExpr for CallExpression<'_> { type_parameters.print(p, ctx); } p.print_ascii_byte(b'('); - let print_comments = p.options.print_comments(); - let has_comment_before_right_paren = - print_comments && self.span.end > 0 && p.has_comment(self.span.end - 1); - let has_comment = print_comments - && (has_comment_before_right_paren - || self.arguments.iter().any(|item| p.has_comment(item.span().start))); + + let (has_comment, has_comment_before_right_paren) = + p.contains_comment_in_call_like_expression(self.span, self.arguments.as_slice()); if has_comment { p.indent(); - p.print_list_with_comments(&self.arguments, ctx); + p.print_list_with_comments(self.arguments.as_slice(), ctx); // Handle `/* comment */);` if !has_comment_before_right_paren || !p.print_expr_comments(self.span.end - 1) { p.print_soft_newline(); @@ -2123,7 +2120,7 @@ impl GenExpr for ImportExpression<'_> { } if has_comment { // Handle `/* comment */);` - if self.span.end > 0 && !p.print_expr_comments(self.span.end - 1) { + if !has_comment_before_right_paren || !p.print_expr_comments(self.span.end - 1) { p.print_soft_newline(); } p.dedent(); @@ -2217,13 +2214,14 @@ impl GenExpr for NewExpression<'_> { if !p.options.minify || !self.arguments.is_empty() || precedence >= Precedence::Postfix { p.print_ascii_byte(b'('); - let has_comment = (self.span.end > 0 && p.has_comment(self.span.end - 1)) - || self.arguments.iter().any(|item| p.has_comment(item.span().start)); + let (has_comment, has_comment_before_right_paren) = p + .contains_comment_in_call_like_expression(self.span, self.arguments.as_slice()); if has_comment { p.indent(); - p.print_list_with_comments(&self.arguments, ctx); + p.print_list_with_comments(self.arguments.as_slice(), ctx); // Handle `/* comment */);` - if self.span.end > 0 && !p.print_expr_comments(self.span.end - 1) { + if !has_comment_before_right_paren || !p.print_expr_comments(self.span.end - 1) + { p.print_soft_newline(); } p.dedent(); @@ -3876,15 +3874,11 @@ impl GenExpr for V8IntrinsicExpression<'_> { p.print_ascii_byte(b'%'); self.name.print(p, Context::empty()); p.print_ascii_byte(b'('); - let print_comments = p.options.print_comments(); - let has_comment_before_right_paren = - print_comments && self.span.end > 0 && p.has_comment(self.span.end - 1); - let has_comment = print_comments - && (has_comment_before_right_paren - || self.arguments.iter().any(|item| p.has_comment(item.span().start))); + let (has_comment, has_comment_before_right_paren) = + p.contains_comment_in_call_like_expression(self.span, self.arguments.as_slice()); if has_comment { p.indent(); - p.print_list_with_comments(&self.arguments, ctx); + p.print_list_with_comments(self.arguments.as_slice(), ctx); // Handle `/* comment */);` if !has_comment_before_right_paren || !p.print_expr_comments(self.span.end - 1) { p.print_soft_newline(); diff --git a/crates/oxc_codegen/src/lib.rs b/crates/oxc_codegen/src/lib.rs index 012da904afe5c..107c707ea2157 100644 --- a/crates/oxc_codegen/src/lib.rs +++ b/crates/oxc_codegen/src/lib.rs @@ -15,7 +15,7 @@ mod sourcemap_builder; use std::borrow::Cow; use oxc_ast::ast::{ - BindingIdentifier, BlockStatement, Comment, Expression, IdentifierReference, Program, + Argument, BindingIdentifier, BlockStatement, Comment, Expression, IdentifierReference, Program, Statement, StringLiteral, }; use oxc_data_structures::{code_buffer::CodeBuffer, stack::Stack}; @@ -477,13 +477,12 @@ impl<'a> Codegen<'a> { } } - fn print_list_with_comments(&mut self, items: &[T], ctx: Context) { + fn print_list_with_comments(&mut self, items: &[Argument<'_>], ctx: Context) { for (index, item) in items.iter().enumerate() { if index != 0 { self.print_comma(); } - if self.print_comments && self.has_non_annotation_comment(item.span().start) { - self.print_expr_comments(item.span().start); + if self.print_expr_comments(item.span().start) { self.print_indent(); } else { self.print_soft_newline();