Skip to content

Commit

Permalink
refactor(codegen) print inner comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Sep 25, 2024
1 parent 138ccc9 commit c7a7586
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
1 change: 1 addition & 0 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_codegen/tests/integration/inner_comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::snapshot;

#[test]
fn comment() {
let cases = vec![r"import(/* @vite-ignore */ dynamicVar)"];

snapshot("inner_comments", &cases);
}
1 change: 1 addition & 0 deletions crates/oxc_codegen/tests/integration/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/oxc_codegen/tests/integration/main.rs
---
########## 0
import(/* @vite-ignore */ dynamicVar)
----------
import(/* @vite-ignore */ dynamicVar);

0 comments on commit c7a7586

Please sign in to comment.