Skip to content

Commit 098c973

Browse files
Boshenclaude
andcommitted
fix(codegen): print legal comments above directives
Fixes #14953 Legal comments (e.g., `/*! ... */`) that appear before directives like "use strict" are now properly preserved in the generated code. The fix adds `print_comments_at(self.span.start)` to the Directive::gen implementation to ensure leading comments are printed before directives, matching the behavior of statements. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 384ea3c commit 098c973

File tree

7 files changed

+72
-4
lines changed

7 files changed

+72
-4
lines changed

crates/oxc_codegen/src/gen.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl Gen for Hashbang<'_> {
6868

6969
impl Gen for Directive<'_> {
7070
fn r#gen(&self, p: &mut Codegen, _ctx: Context) {
71+
p.print_comments_at(self.span.start);
7172
p.add_source_mapping(self.span);
7273
p.print_indent();
7374
// A Use Strict Directive may not contain an EscapeSequence or LineContinuation.

crates/oxc_codegen/tests/integration/comments.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ pub mod legal {
224224
* @preserve
225225
*/
226226
",
227+
// Issue #14953: legal comments above directives
228+
"/*!\n * legal comment\n */\n\n\"use strict\";\n\nexport const foo = 'foo';",
227229
]
228230
}
229231

crates/oxc_codegen/tests/integration/snapshots/legal_eof_comments.snap

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: crates/oxc_codegen/tests/integration/main.rs
2+
source: crates/oxc_codegen/tests/integration/tester.rs
33
---
44
########## 0
55
/* @license */
@@ -127,3 +127,19 @@ function foo() {
127127
/**
128128
* @preserve
129129
*/
130+
131+
########## 9
132+
/*!
133+
* legal comment
134+
*/
135+
136+
"use strict";
137+
138+
export const foo = 'foo';
139+
----------
140+
'use strict';
141+
export const foo = 'foo';
142+
143+
/*!
144+
* legal comment
145+
*/

crates/oxc_codegen/tests/integration/snapshots/legal_eof_minify_comments.snap

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: crates/oxc_codegen/tests/integration/main.rs
2+
source: crates/oxc_codegen/tests/integration/tester.rs
33
---
44
########## 0
55
/* @license */
@@ -109,3 +109,17 @@ function foo(){(()=>{
109109
/**
110110
* @preserve
111111
*/
112+
113+
########## 9
114+
/*!
115+
* legal comment
116+
*/
117+
118+
"use strict";
119+
120+
export const foo = 'foo';
121+
----------
122+
'use strict';export const foo=`foo`;
123+
/*!
124+
* legal comment
125+
*/

crates/oxc_codegen/tests/integration/snapshots/legal_inline_comments.snap

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: crates/oxc_codegen/tests/integration/main.rs
2+
source: crates/oxc_codegen/tests/integration/tester.rs
33
---
44
########## 0
55
/* @license */
@@ -112,3 +112,18 @@ function foo() {
112112
/**
113113
* @preserve
114114
*/
115+
116+
########## 9
117+
/*!
118+
* legal comment
119+
*/
120+
121+
"use strict";
122+
123+
export const foo = 'foo';
124+
----------
125+
/*!
126+
* legal comment
127+
*/
128+
'use strict';
129+
export const foo = 'foo';

crates/oxc_codegen/tests/integration/snapshots/legal_linked_comments.snap

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
source: crates/oxc_codegen/tests/integration/main.rs
2+
source: crates/oxc_codegen/tests/integration/tester.rs
33
---
44
########## 0
55
/* @license */
@@ -108,3 +108,16 @@ function foo() {
108108
*/
109109

110110
/*! For license information please see test.js */
111+
########## 9
112+
/*!
113+
* legal comment
114+
*/
115+
116+
"use strict";
117+
118+
export const foo = 'foo';
119+
----------
120+
'use strict';
121+
export const foo = 'foo';
122+
123+
/*! For license information please see test.js */

crates/oxc_codegen/tests/integration/snapshots/stacktrace_is_correct.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn();
2121
throw new Error()
2222
^
2323

24+
2425
Error
2526
at fn (/project/input.js:3:11)
2627
at <anonymous> (/project/input.js:5:1)
@@ -48,6 +49,7 @@ obj.fn();
4849
throw new Error()
4950
^
5051

52+
5153
Error
5254
at Object.fn (/project/input.js:4:15)
5355
at <anonymous> (/project/input.js:7:5)
@@ -77,6 +79,7 @@ obj.obj2.fn();
7779
throw new Error()
7880
^
7981

82+
8083
Error
8184
at Object.fn (/project/input.js:5:19)
8285
at <anonymous> (/project/input.js:9:10)
@@ -108,6 +111,7 @@ obj.fn()();
108111
throw new Error()
109112
^
110113

114+
111115
Error
112116
at fn2 (/project/input.js:5:19)
113117
at <anonymous> (/project/input.js:9:9)
@@ -139,6 +143,7 @@ obj.fn([1])();
139143
throw new Error()
140144
^
141145

146+
142147
Error
143148
at <anonymous> (/project/input.js:5:19)
144149
at <anonymous> (/project/input.js:9:12)
@@ -172,6 +177,7 @@ obj.fn({ a })();
172177
throw new Error()
173178
^
174179

180+
175181
Error
176182
at <anonymous> (/project/input.js:6:19)
177183
at <anonymous> (/project/input.js:10:12)
@@ -201,6 +207,7 @@ fn("name", () => {
201207
throw new Error()
202208
^
203209

210+
204211
Error
205212
at <anonymous> (/project/input.js:6:11)
206213
at fn (/project/input.js:2:5)

0 commit comments

Comments
 (0)