Skip to content

Commit

Permalink
refactor(codegen): simplify printing annotation comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Mar 3, 2025
1 parent a8d1d48 commit 80309aa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
24 changes: 15 additions & 9 deletions 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 @@ -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
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 @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 3 additions & 4 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,13 +477,12 @@ 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 && 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();
Expand Down

0 comments on commit 80309aa

Please sign in to comment.