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 d0f6e7d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
7 changes: 6 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,17 @@ 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 is_empty = self.is_empty() && !p.comments.contains_key(&(self.span.end - 1));
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.
p.print_statement_comments(self.span.end - 1);
});
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 d0f6e7d

Please sign in to comment.