diff --git a/crates/oxc_codegen/src/comment.rs b/crates/oxc_codegen/src/comment.rs index 0e991d4a3627c..e8067a763f3dc 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}; @@ -23,6 +24,21 @@ impl Codegen<'_> { self.comments.contains_key(&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) + } + /// Whether to keep leading comments. fn should_keep_leading_comment(comment: &Comment) -> bool { comment.preceded_by_newline && comment.is_annotation() diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 0c57bea42b096..634ef00babe58 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1453,15 +1453,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(); @@ -2124,7 +2121,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(); @@ -2218,13 +2215,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(); @@ -3877,15 +3875,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 8611ca551a476..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,22 +477,17 @@ 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 { - let start = item.span().start; - if self.has_comment(start) { - self.print_expr_comments(start); - self.print_indent(); - item.print(self, ctx); - continue; - } + if self.print_expr_comments(item.span().start) { + self.print_indent(); + } else { + self.print_soft_newline(); + self.print_indent(); } - self.print_soft_newline(); - self.print_indent(); item.print(self, ctx); } }