diff --git a/crates/oxc_codegen/src/comment.rs b/crates/oxc_codegen/src/comment.rs index 4126a98ea1ad29..f17057cfdc3544 100644 --- a/crates/oxc_codegen/src/comment.rs +++ b/crates/oxc_codegen/src/comment.rs @@ -123,4 +123,17 @@ impl<'a> Codegen<'a> { self.print_hard_space(); } } + + pub(crate) fn print_inner_comments(&mut self, start: u32) { + if self.options.minify { + return; + } + let Some(source_text) = self.source_text else { return }; + let Some(comments) = self.comments.remove(&start) else { return }; + + for comment in comments { + self.print_str(comment.real_span().source_text(source_text)); + self.print_hard_space(); + } + } } diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 092f9fb42117cf..c41865597fcdea 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1952,6 +1952,7 @@ impl<'a> GenExpr for ImportExpression<'a> { p.wrap(wrap, |p| { p.add_source_mapping(self.span.start); p.print_str("import("); + p.print_inner_comments(self.source.span().start); self.source.print_expr(p, Precedence::Comma, Context::empty()); if !self.arguments.is_empty() { p.print_comma(); diff --git a/crates/oxc_codegen/tests/integration/inner_comments.rs b/crates/oxc_codegen/tests/integration/inner_comments.rs new file mode 100644 index 00000000000000..7d0638f9d39588 --- /dev/null +++ b/crates/oxc_codegen/tests/integration/inner_comments.rs @@ -0,0 +1,8 @@ +use crate::snapshot; + +#[test] +fn comment() { + let cases = vec![r"import(/* @vite-ignore */ dynamicVar)"]; + + snapshot("inner_comments", &cases); +} diff --git a/crates/oxc_codegen/tests/integration/main.rs b/crates/oxc_codegen/tests/integration/main.rs index 31327f9a484d02..5b57bcc09f0214 100644 --- a/crates/oxc_codegen/tests/integration/main.rs +++ b/crates/oxc_codegen/tests/integration/main.rs @@ -1,5 +1,6 @@ #![allow(clippy::missing_panics_doc)] pub mod esbuild; +pub mod inner_comments; pub mod jsdoc; pub mod pure_comments; pub mod tester; diff --git a/crates/oxc_codegen/tests/integration/snapshots/inner_comments.snap b/crates/oxc_codegen/tests/integration/snapshots/inner_comments.snap new file mode 100644 index 00000000000000..8471bc5ae0d48c --- /dev/null +++ b/crates/oxc_codegen/tests/integration/snapshots/inner_comments.snap @@ -0,0 +1,7 @@ +--- +source: crates/oxc_codegen/tests/integration/main.rs +--- +########## 0 +import(/* @vite-ignore */ dynamicVar) +---------- +import(/* @vite-ignore */ dynamicVar);