Skip to content
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
18 changes: 17 additions & 1 deletion crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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()
Expand Down
32 changes: 13 additions & 19 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
19 changes: 7 additions & 12 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -477,22 +477,17 @@ impl<'a> Codegen<'a> {
}
}

fn print_list_with_comments<T: Gen + GetSpan>(&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);
}
}
Expand Down
Loading