Skip to content

Commit

Permalink
fix(codegen): print statements when block is empty
Browse files Browse the repository at this point in the history
part of #7190
  • Loading branch information
Boshen committed Nov 11, 2024
1 parent 0d6a66a commit 57127c4
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 9 deletions.
29 changes: 21 additions & 8 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ impl<'a> Codegen<'a> {
self.print_comments(start, &comments, unused_comments);
}

/// A statement comment also includes legal comments
pub(crate) fn print_statement_comments(&mut self, start: u32) {
pub(crate) fn get_statement_comments(
&mut self,
start: u32,
) -> Option<(Vec<Comment>, Vec<Comment>)> {
if self.options.minify {
return;
return None;
}
let Some(comments) = self.comments.remove(&start) else {
return;
};

let comments = self.comments.remove(&start)?;

let mut leading_comments = vec![];
let mut unused_comments = vec![];
Expand Down Expand Up @@ -107,7 +108,14 @@ impl<'a> Codegen<'a> {
unused_comments.push(comment);
}

self.print_comments(start, &leading_comments, unused_comments);
Some((leading_comments, unused_comments))
}

/// A statement comment also includes legal comments
pub(crate) fn print_statement_comments(&mut self, start: u32) {
if let Some((comments, unused)) = self.get_statement_comments(start) {
self.print_comments(start, &comments, unused);
}
}

pub(crate) fn print_annotation_comments(&mut self, node_start: u32) {
Expand Down Expand Up @@ -162,7 +170,12 @@ impl<'a> Codegen<'a> {
}
}

fn print_comments(&mut self, start: u32, comments: &[Comment], unused_comments: Vec<Comment>) {
pub(crate) fn print_comments(
&mut self,
start: u32,
comments: &[Comment],
unused_comments: Vec<Comment>,
) {
for (i, comment) in comments.iter().enumerate() {
if i == 0 {
if comment.preceded_by_newline {
Expand Down
19 changes: 18 additions & 1 deletion crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ impl<'a> Gen for Program<'a> {
stmt.print(p, ctx);
p.print_semicolon_if_needed();
}
// Print trailing statement comments.
p.print_statement_comments(self.span.end);
}
}

Expand Down Expand Up @@ -662,14 +664,29 @@ impl<'a> Gen for Function<'a> {

impl<'a> Gen for FunctionBody<'a> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.print_curly_braces(self.span, self.is_empty(), |p| {
let span_end = self.span.end;
let comments_at_end = if !p.options.minify && span_end != 0 {
p.get_statement_comments(span_end - 1)
} else {
None
};
let is_empty = if self.is_empty() {
comments_at_end.is_none() || comments_at_end.as_ref().is_some_and(|c| c.0.is_empty())
} else {
false
};
p.print_curly_braces(self.span, is_empty, |p| {
for directive in &self.directives {
directive.print(p, ctx);
}
for stmt in &self.statements {
p.print_semicolon_if_needed();
stmt.print(p, ctx);
}
// Print trailing statement comments.
if let Some((comments, unused)) = comments_at_end {
p.print_comments(span_end - 1, &comments, unused);
}
});
p.needs_semicolon = false;
}
Expand Down
17 changes: 17 additions & 0 deletions crates/oxc_codegen/tests/integration/legal_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ fn cases() -> Vec<&'static str> {
bar;
}",
"function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } }",
"function foo() {
(() => {
/**
* @preserve
*/
})();
/**
* @preserve
*/
}
/**
* @preserve
*/",
"/**
* @preserve
*/
",
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,41 @@ function bar() {
function() {}
}
/*! #__NO_SIDE_EFFECTS__ */

########## 7
function foo() {
(() => {
/**
* @preserve
*/
})();
/**
* @preserve
*/
}
/**
* @preserve
*/
----------
function foo() {
(() => {
/**
* @preserve
*/
})();
/**
* @preserve
*/
}
/**
* @preserve
*/
########## 8
/**
* @preserve
*/

----------
/**
* @preserve
*/
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,41 @@ function bar() {
var foo;
/*! #__NO_SIDE_EFFECTS__ */ function() {}
}

########## 7
function foo() {
(() => {
/**
* @preserve
*/
})();
/**
* @preserve
*/
}
/**
* @preserve
*/
----------
function foo() {
(() => {
/**
* @preserve
*/
})();
/**
* @preserve
*/
}
/**
* @preserve
*/
########## 8
/**
* @preserve
*/

----------
/**
* @preserve
*/
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,41 @@ function bar() {
function() {}
}
/*! For license information please see test.js */
########## 7
function foo() {
(() => {
/**
* @preserve
*/
})();
/**
* @preserve
*/
}
/**
* @preserve
*/
----------
function foo() {
(() => {
/**
* @preserve
*/
})();
/**
* @preserve
*/
}
/**
* @preserve
*//*! For license information please see test.js */
########## 8
/**
* @preserve
*/

----------
/**
* @preserve
*/
/*! For license information please see test.js */

0 comments on commit 57127c4

Please sign in to comment.