Skip to content

Commit

Permalink
fix(codegen): converts line comment to block comment if it is a `PURE…
Browse files Browse the repository at this point in the history
…` comment (#6356)

The last part of fixing https://github.com/rolldown/rolldown/pull/2375/files#r1789011257

In the following case, the pure comment was written by `//`
```ts
const Component = // #__PURE__
React.forwardRef((props, ref) => {});
```

The printed code looks like this
```ts
const Component =
// #__PURE__ React.forwardRef((props, ref) => {});
```

As you can see, it is broken because the code also commented, so we need to replace `//` with `/* */`
  • Loading branch information
Dunqing committed Oct 8, 2024
1 parent 1380d8b commit 84b2d07
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ impl<'a> Codegen<'a> {
if !Self::is_annotation_comment(&comment, source_text) {
continue;
}
self.print_str(comment.real_span().source_text(source_text));
if comment.is_line() {
self.print_str("/*");
self.print_str(comment.span.source_text(source_text));
self.print_str("*/");
} else {
self.print_str(comment.real_span().source_text(source_text));
}
self.print_hard_space();
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_codegen/tests/integration/pure_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ const defineSSRCustomElement = () => {
return /* @__PURE__ */ /* @__NO_SIDE_EFFECTS__ */ /* #__NO_SIDE_EFFECTS__ */ defineCustomElement(options, extraOptions, hydrate);
};
",
"
const Component = // #__PURE__
React.forwardRef((props, ref) => {});
",
];

snapshot("pure_comments", &cases);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,11 @@ const defineSSRCustomElement = () => {
const defineSSRCustomElement = () => {
return /* @__PURE__ */ /* @__NO_SIDE_EFFECTS__ */ /* #__NO_SIDE_EFFECTS__ */ defineCustomElement(options, extraOptions, hydrate);
};

########## 20

const Component = // #__PURE__
React.forwardRef((props, ref) => {});

----------
const Component = /* #__PURE__*/ React.forwardRef((props, ref) => {});

0 comments on commit 84b2d07

Please sign in to comment.