perf(codegen): guard comment printing comments when there are no comments (#7856)

This commit is contained in:
Boshen 2024-12-13 15:12:09 +00:00
parent 34091b2e7a
commit 71a40a222f
3 changed files with 16 additions and 16 deletions

View file

@ -59,9 +59,6 @@ impl Codegen<'_> {
}
pub(crate) fn print_leading_comments(&mut self, start: u32) {
if self.options.minify {
return;
}
let Some(comments) = self.comments.remove(&start) else {
return;
};
@ -74,10 +71,6 @@ impl Codegen<'_> {
&mut self,
start: u32,
) -> Option<(Vec<Comment>, Vec<Comment>)> {
if self.options.minify {
return None;
}
let comments = self.comments.remove(&start)?;
let mut leading_comments = vec![];
@ -144,9 +137,6 @@ impl Codegen<'_> {
}
pub(crate) fn print_expr_comments(&mut self, start: u32) -> bool {
if self.options.minify {
return false;
}
let Some(comments) = self.comments.remove(&start) else { return false };
let (annotation_comments, comments): (Vec<_>, Vec<_>) =

View file

@ -2279,7 +2279,9 @@ impl Gen for ClassBody<'_> {
p.print_curly_braces(self.span, self.body.is_empty(), |p| {
for item in &self.body {
p.print_semicolon_if_needed();
p.print_leading_comments(item.span().start);
if p.print_comments {
p.print_leading_comments(item.span().start);
}
p.print_indent();
item.print(p, ctx);
}
@ -3636,7 +3638,9 @@ impl Gen for TSInterfaceDeclaration<'_> {
p.print_soft_space();
p.print_curly_braces(self.body.span, self.body.body.is_empty(), |p| {
for item in &self.body.body {
p.print_leading_comments(item.span().start);
if p.print_comments {
p.print_leading_comments(item.span().start);
}
p.print_indent();
item.print(p, ctx);
p.print_semicolon();
@ -3670,7 +3674,9 @@ impl Gen for TSEnumDeclaration<'_> {
p.print_space_before_identifier();
p.print_curly_braces(self.span, self.members.is_empty(), |p| {
for member in &self.members {
p.print_leading_comments(member.span().start);
if p.print_comments {
p.print_leading_comments(member.span().start);
}
p.print_indent();
member.print(p, ctx);
p.print_comma();

View file

@ -204,8 +204,12 @@ impl<'a> Codegen<'a> {
self.quote = if self.options.single_quote { b'\'' } else { b'"' };
self.source_text = program.source_text;
self.code.reserve(program.source_text.len());
if self.options.print_comments() {
self.build_comments(&program.comments);
if self.print_comments {
if program.comments.is_empty() {
self.print_comments = false;
} else {
self.build_comments(&program.comments);
}
}
if let Some(path) = &self.options.source_map_path {
self.sourcemap_builder = Some(SourcemapBuilder::new(path, program.source_text));
@ -474,7 +478,7 @@ impl<'a> Codegen<'a> {
if index != 0 {
self.print_comma();
}
if self.has_non_annotation_comment(item.span().start) {
if self.print_comments && self.has_non_annotation_comment(item.span().start) {
self.print_expr_comments(item.span().start);
self.print_indent();
} else {