Skip to content

Commit 622cb5e

Browse files
committed
fix(parser): preserve legal comments with @preserve/@license when preceded by other annotations (#15929)
Legal comments like `/* @foo @preserve */` were not being preserved when legal comment output was enabled. The parser only checked if the first `@` annotation matched known patterns, missing `@preserve`/`@license` later in the comment. ## Changes **Parser (`oxc_parser/src/lexer/trivia_builder.rs`)** - Add fallback check using `contains_license_or_preserve_comment` after `__PURE__`/`__NO_SIDE_EFFECTS__` checks - Ensures `@license`/`@preserve` detected anywhere in comment text ## Example ```javascript // Before: Legal comment not detected by parser /* @foo @preserve */ function foo() {} // Parser classifies as: CommentContent::None ❌ // After: Legal comment correctly detected /* @foo @preserve */ function foo() {} // Parser classifies as: CommentContent::Legal ✅ ``` Related: rolldown/rolldown#7160 <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > `/* @foo @preserve */` should be preserved when legal comment output is enabled even if comment output is disabled. rolldown/rolldown#7160 is related </details> <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/oxc-project/oxc/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.
1 parent 512e43a commit 622cb5e

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

crates/oxc_parser/src/lexer/trivia_builder.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,19 @@ impl TriviaBuilder {
248248
if rest.starts_with(b"PURE__") {
249249
comment.content = CommentContent::Pure;
250250
self.has_pure_comment = true;
251+
return;
251252
} else if rest.starts_with(b"NO_SIDE_EFFECTS__") {
252253
comment.content = CommentContent::NoSideEffects;
253254
self.has_no_side_effects_comment = true;
255+
return;
254256
}
255257
}
258+
259+
// Fallback: check for @license or @preserve anywhere in the comment
260+
// This handles cases like /* @foo @preserve */ where the first @ doesn't match known patterns
261+
if contains_license_or_preserve_comment(s) {
262+
comment.content = CommentContent::Legal;
263+
}
256264
}
257265
}
258266

@@ -504,6 +512,8 @@ token /* Trailing 1 */
504512
("/* @license */", CommentContent::Legal),
505513
("/* foo @preserve */", CommentContent::Legal),
506514
("/* foo @license */", CommentContent::Legal),
515+
("/* @foo @preserve */", CommentContent::Legal),
516+
("/* @foo @license */", CommentContent::Legal),
507517
("/** foo @preserve */", CommentContent::JsdocLegal),
508518
("/** foo @license */", CommentContent::JsdocLegal),
509519
("/** jsdoc */", CommentContent::Jsdoc),

0 commit comments

Comments
 (0)