Skip to content

Commit

Permalink
perf(codegen): guard comment printing comments when there are no comm…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
Boshen committed Dec 13, 2024
1 parent 1eab12a commit cda00a3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
10 changes: 0 additions & 10 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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![];
Expand Down Expand Up @@ -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<_>) =
Expand Down
12 changes: 9 additions & 3 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 7 additions & 3 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cda00a3

Please sign in to comment.