mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
parent
0d6a66aa9e
commit
1282221e36
6 changed files with 170 additions and 9 deletions
|
|
@ -70,14 +70,15 @@ impl<'a> Codegen<'a> {
|
||||||
self.print_comments(start, &comments, unused_comments);
|
self.print_comments(start, &comments, unused_comments);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A statement comment also includes legal comments
|
pub(crate) fn get_statement_comments(
|
||||||
pub(crate) fn print_statement_comments(&mut self, start: u32) {
|
&mut self,
|
||||||
|
start: u32,
|
||||||
|
) -> Option<(Vec<Comment>, Vec<Comment>)> {
|
||||||
if self.options.minify {
|
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 leading_comments = vec![];
|
||||||
let mut unused_comments = vec![];
|
let mut unused_comments = vec![];
|
||||||
|
|
@ -107,7 +108,14 @@ impl<'a> Codegen<'a> {
|
||||||
unused_comments.push(comment);
|
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) {
|
pub(crate) fn print_annotation_comments(&mut self, node_start: u32) {
|
||||||
|
|
@ -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() {
|
for (i, comment) in comments.iter().enumerate() {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
if comment.preceded_by_newline {
|
if comment.preceded_by_newline {
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,8 @@ impl<'a> Gen for Program<'a> {
|
||||||
stmt.print(p, ctx);
|
stmt.print(p, ctx);
|
||||||
p.print_semicolon_if_needed();
|
p.print_semicolon_if_needed();
|
||||||
}
|
}
|
||||||
|
// Print trailing statement comments.
|
||||||
|
p.print_statement_comments(self.span.end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -662,7 +664,18 @@ impl<'a> Gen for Function<'a> {
|
||||||
|
|
||||||
impl<'a> Gen for FunctionBody<'a> {
|
impl<'a> Gen for FunctionBody<'a> {
|
||||||
fn gen(&self, p: &mut Codegen, ctx: Context) {
|
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 {
|
for directive in &self.directives {
|
||||||
directive.print(p, ctx);
|
directive.print(p, ctx);
|
||||||
}
|
}
|
||||||
|
|
@ -670,6 +683,10 @@ impl<'a> Gen for FunctionBody<'a> {
|
||||||
p.print_semicolon_if_needed();
|
p.print_semicolon_if_needed();
|
||||||
stmt.print(p, ctx);
|
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;
|
p.needs_semicolon = false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,23 @@ fn cases() -> Vec<&'static str> {
|
||||||
bar;
|
bar;
|
||||||
}",
|
}",
|
||||||
"function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } }",
|
"function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } }",
|
||||||
|
"function foo() {
|
||||||
|
(() => {
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
})();
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/",
|
||||||
|
"/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
",
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,3 +76,41 @@ function bar() {
|
||||||
function() {}
|
function() {}
|
||||||
}
|
}
|
||||||
/*! #__NO_SIDE_EFFECTS__ */
|
/*! #__NO_SIDE_EFFECTS__ */
|
||||||
|
|
||||||
|
########## 7
|
||||||
|
function foo() {
|
||||||
|
(() => {
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
})();
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
----------
|
||||||
|
function foo() {
|
||||||
|
(() => {
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
})();
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
########## 8
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
|
||||||
|
----------
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -75,3 +75,41 @@ function bar() {
|
||||||
var foo;
|
var foo;
|
||||||
/*! #__NO_SIDE_EFFECTS__ */ function() {}
|
/*! #__NO_SIDE_EFFECTS__ */ function() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
########## 7
|
||||||
|
function foo() {
|
||||||
|
(() => {
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
})();
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
----------
|
||||||
|
function foo() {
|
||||||
|
(() => {
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
})();
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
########## 8
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
|
||||||
|
----------
|
||||||
|
/**
|
||||||
|
* @preserve
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -62,3 +62,41 @@ function bar() {
|
||||||
function() {}
|
function() {}
|
||||||
}
|
}
|
||||||
/*! For license information please see test.js */
|
/*! 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 */
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue