Skip to content

Commit 2c5fa58

Browse files
authored
feat(codegen): allow attaching comment to the top of the file. (#13048)
closes #12982
1 parent fe5965d commit 2c5fa58

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

crates/oxc_codegen/src/gen.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ impl Gen for Program<'_> {
4646
fn r#gen(&self, p: &mut Codegen, ctx: Context) {
4747
p.is_jsx = self.source_type.is_jsx();
4848

49+
// Allow for inserting comments to the top of the file.
50+
p.print_comments_at(0);
4951
if let Some(hashbang) = &self.hashbang {
5052
hashbang.print(p, ctx);
5153
}

crates/oxc_codegen/tests/integration/comments.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
use crate::tester::{test, test_same};
22

3+
#[test]
4+
fn test_comment_at_top_of_file() {
5+
use oxc_allocator::Allocator;
6+
use oxc_ast::CommentPosition;
7+
use oxc_codegen::Codegen;
8+
use oxc_parser::Parser;
9+
use oxc_span::SourceType;
10+
let source_type = SourceType::mjs();
11+
let allocator = Allocator::default();
12+
let mut ret = Parser::new(&allocator, "export{} /** comment */", source_type).parse();
13+
// Move comment to top of the file.
14+
ret.program.comments[0].attached_to = 0;
15+
ret.program.comments[0].position = CommentPosition::Leading;
16+
let code = Codegen::new().build(&ret.program).code;
17+
assert_eq!(code, "/** comment */ export {};\n");
18+
}
19+
320
#[test]
421
fn unit() {
522
test_same("<div>{/* Hello */}</div>;\n");

0 commit comments

Comments
 (0)