Skip to content

feat(codegen): a way to keep legal comments after minification#10689

Merged
graphite-app[bot] merged 1 commit intomainfrom
04-29-feat_codegen_a_way_to_keep_legal_comments_after_minification
May 1, 2025
Merged

feat(codegen): a way to keep legal comments after minification#10689
graphite-app[bot] merged 1 commit intomainfrom
04-29-feat_codegen_a_way_to_keep_legal_comments_after_minification

Conversation

@Boshen
Copy link
Member

@Boshen Boshen commented Apr 29, 2025

fixes rolldown/rolldown#4118
fixes #10626

At present, there is no option to keep legal comments when minification is turn on.

This PR allows Codegen to set legal comments with minfication turned on, with the following behavior:

  • Inline: only if the attached code is not removed
  • Eof: move all legal comments to the end of file, regardless of where they appear
  • Linked and External: saved into CodegenReturn::legal_comments for the caller to use, regardless of where they appear

Code gets moved around or removed if minifier is turned on, so some legal comments may disappear from the final output.

The recommended legal comment option is Eof.


For Rolldown to keep legal comments, it should set LegalComment::Inline for chunk codegen and LegalComment::Eof for minification codegen.

@Boshen Boshen requested a review from Dunqing April 29, 2025 06:53
@graphite-app
Copy link
Contributor

graphite-app bot commented Apr 29, 2025

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

@github-actions github-actions bot added A-minifier Area - Minifier A-codegen Area - Code Generation C-enhancement Category - New feature or request labels Apr 29, 2025
@Boshen
Copy link
Member Author

Boshen commented Apr 29, 2025

@Dunqing can you double check

test module_runner_transform::test::jsx ... FAILED

failures:

---- module_runner_transform::test::jsx stdout ----
-function Bar({ Slot = __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) {
-	return __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, __vite_ssr_import_0__.default.createElement(Slot, null));
+function Bar({ Slot = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) {
+	return /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(Slot, null));

thread 'module_runner_transform::test::jsx' panicked at crates/oxc_transformer_plugins/src/module_runner_transform.rs:1861:9:

Should these nodes be pure so that pure comments gets printed?

@codspeed-hq
Copy link

codspeed-hq bot commented Apr 29, 2025

CodSpeed Instrumentation Performance Report

Merging #10689 will not alter performance

Comparing 04-29-feat_codegen_a_way_to_keep_legal_comments_after_minification (b01cb45) with main (d1d05d3)

Summary

✅ 36 untouched benchmarks

@Dunqing
Copy link
Member

Dunqing commented Apr 29, 2025

@Dunqing can you double check

test module_runner_transform::test::jsx ... FAILED

failures:

---- module_runner_transform::test::jsx stdout ----
-function Bar({ Slot = __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) {
-	return __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, __vite_ssr_import_0__.default.createElement(Slot, null));
+function Bar({ Slot = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_1__.Foo, null) }) {
+	return /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(Slot, null));

thread 'module_runner_transform::test::jsx' panicked at crates/oxc_transformer_plugins/src/module_runner_transform.rs:1861:9:

Should these nodes be pure so that pure comments gets printed?

Yes, they should be printed. I don't know why they disappeared before.

@Boshen Boshen force-pushed the 04-29-feat_codegen_a_way_to_keep_legal_comments_after_minification branch 2 times, most recently from 56c8b9f to 29acf1a Compare April 29, 2025 10:31
@Boshen Boshen marked this pull request as ready for review April 29, 2025 10:34
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Apr 29, 2025
Copy link
Member Author

Boshen commented Apr 29, 2025

Merge activity

  • Apr 29, 6:34 AM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Apr 29, 1:12 PM EDT: Graphite disabled "merge when ready" on this PR due to: a merge conflict with the target branch; resolve the conflict and try again..
  • Apr 29, 1:12 PM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • May 1, 12:19 AM EDT: Graphite disabled "merge when ready" on this PR due to: a merge conflict with the target branch; resolve the conflict and try again..
  • May 1, 12:19 AM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • May 1, 12:26 AM EDT: Boshen added this pull request to the Graphite merge queue.
  • May 1, 2:39 AM EDT: Merged by the Graphite merge queue.

@Boshen Boshen force-pushed the 04-29-feat_codegen_a_way_to_keep_legal_comments_after_minification branch 2 times, most recently from 2110373 to 344779e Compare May 1, 2025 04:18
@coderabbitai
Copy link

coderabbitai bot commented May 1, 2025

Walkthrough

This set of changes refactors and expands the handling of code generation options related to comments in the codebase. The previous single boolean flag for printing comments is replaced with three distinct flags: print_any_comment, print_legal_comment, and print_annotation_comment, allowing for more granular control over which types of comments are included in generated output. The CodegenOptions struct is updated to reflect these changes, with new default values and a new minify() constructor that disables all comments for minification scenarios except legal comments. The logic for collecting and printing legal comments is refined, ensuring that legal comments are only included according to the specified options and are not duplicated. Several code paths are updated to use the new flags and helper methods. Additionally, new tests are added to verify the correct behavior of legal comment preservation during minification. Example and test code are updated to use the new configuration interfaces, and some JSX transform output is enhanced with pure annotations.

Possibly related issues


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9773a0c and b01cb45.

⛔ Files ignored due to path filters (3)
  • crates/oxc_codegen/tests/integration/snapshots/legal_eof_comments.snap is excluded by !**/*.snap
  • crates/oxc_codegen/tests/integration/snapshots/legal_eof_minify_comments.snap is excluded by !**/*.snap
  • crates/oxc_codegen/tests/integration/snapshots/legal_linked_comments.snap is excluded by !**/*.snap
📒 Files selected for processing (11)
  • crates/oxc_codegen/examples/codegen.rs (1 hunks)
  • crates/oxc_codegen/src/comment.rs (6 hunks)
  • crates/oxc_codegen/src/gen.rs (8 hunks)
  • crates/oxc_codegen/src/lib.rs (5 hunks)
  • crates/oxc_codegen/src/options.rs (2 hunks)
  • crates/oxc_codegen/tests/integration/comments.rs (1 hunks)
  • crates/oxc_minifier/examples/minifier.rs (1 hunks)
  • crates/oxc_transformer_plugins/src/module_runner_transform.rs (1 hunks)
  • napi/minify/src/options.rs (1 hunks)
  • napi/playground/src/lib.rs (2 hunks)
  • tasks/minsize/src/lib.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (11)
  • tasks/minsize/src/lib.rs
  • crates/oxc_codegen/examples/codegen.rs
  • napi/minify/src/options.rs
  • crates/oxc_codegen/tests/integration/comments.rs
  • crates/oxc_minifier/examples/minifier.rs
  • crates/oxc_transformer_plugins/src/module_runner_transform.rs
  • napi/playground/src/lib.rs
  • crates/oxc_codegen/src/comment.rs
  • crates/oxc_codegen/src/gen.rs
  • crates/oxc_codegen/src/lib.rs
  • crates/oxc_codegen/src/options.rs
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: Benchmark (parser)
  • GitHub Check: Benchmark (transformer)
  • GitHub Check: Benchmark (lexer)
  • GitHub Check: Build Linter Benchmark
  • GitHub Check: Test (ubuntu-latest)
  • GitHub Check: Clippy
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Boshen Boshen force-pushed the 04-29-feat_codegen_a_way_to_keep_legal_comments_after_minification branch from 344779e to 6928736 Compare May 1, 2025 04:20
@graphite-app graphite-app bot force-pushed the 04-29-feat_codegen_a_way_to_keep_legal_comments_after_minification branch from 6928736 to 9773a0c Compare May 1, 2025 04:27
graphite-app bot pushed a commit that referenced this pull request May 1, 2025
fixes rolldown/rolldown#4118
fixes #10626

At present, there is no option to keep legal comments when minification is turn on.

This PR allows Codegen to set legal comments with minfication turned on, with the following behavior:

* Inline: only if the attached code is not removed
* Eof: move all legal comments to the end of file, regardless of where they appear
* Linked and External: saved into `CodegenReturn::legal_comments` for the caller to use, regardless of where they appear

Code gets moved around or removed if minifier is turned on, so some legal comments may disappear from the final output.

The recommended legal comment option is Eof.

---

For Rolldown to keep legal comments, it should set `LegalComment::Inline` for chunk codegen and `LegalComment::Eof` for minification codegen.
fixes rolldown/rolldown#4118
fixes #10626

At present, there is no option to keep legal comments when minification is turn on.

This PR allows Codegen to set legal comments with minfication turned on, with the following behavior:

* Inline: only if the attached code is not removed
* Eof: move all legal comments to the end of file, regardless of where they appear
* Linked and External: saved into `CodegenReturn::legal_comments` for the caller to use, regardless of where they appear

Code gets moved around or removed if minifier is turned on, so some legal comments may disappear from the final output.

The recommended legal comment option is Eof.

---

For Rolldown to keep legal comments, it should set `LegalComment::Inline` for chunk codegen and `LegalComment::Eof` for minification codegen.
@graphite-app graphite-app bot force-pushed the 04-29-feat_codegen_a_way_to_keep_legal_comments_after_minification branch from 9773a0c to b01cb45 Compare May 1, 2025 06:31
@graphite-app graphite-app bot merged commit b01cb45 into main May 1, 2025
26 checks passed
@graphite-app graphite-app bot deleted the 04-29-feat_codegen_a_way_to_keep_legal_comments_after_minification branch May 1, 2025 06:39
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-codegen Area - Code Generation A-minifier Area - Minifier C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

codegen: preserve legal comments with whitespace removal [Bug]: comments: "preserve-legal" not working

2 participants