feat(transformers): add transformerRemoveComments#1144
Conversation
✅ Deploy Preview for shiki-next ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for shiki-matsu ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
@antfu Raised PR Please take a look review and merge it |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1144 +/- ##
==========================================
+ Coverage 95.14% 95.20% +0.05%
==========================================
Files 91 92 +1
Lines 7873 7922 +49
Branches 1673 1690 +17
==========================================
+ Hits 7491 7542 +51
+ Misses 376 374 -2
Partials 6 6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@antfu All chechks have passed |
|
@antfu Please Update if any Changes are required on this this PR |
|
@antfu Any Updates regarding this PR |
There was a problem hiding this comment.
Pull request overview
This PR introduces a new transformer transformerRemoveComments that automatically removes comments from code snippets. The transformer works by examining the TextMate grammar scope information to identify comment tokens and filter them out. It's designed for use cases like presentation slides or minimal examples where comments might be distracting.
Key changes:
- New transformer implementation with support for removing lines that become empty after comment removal
- Integration with the
includeExplanationoption to access token scope metadata - Basic test coverage and documentation
Reviewed changes
Copilot reviewed 9 out of 10 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
packages/transformers/src/transformers/remove-comments.ts |
Core transformer implementation that filters tokens with comment scopes |
packages/transformers/src/index.ts |
Exports the new transformer function |
packages/types/src/options.ts |
Adds includeExplanation to the CodeToHastOptions interface to support the transformer |
packages/transformers/test/fixtures.test.ts |
Adds test suite for the new transformer |
packages/transformers/test/fixtures/remove-comments/basic.js |
Test input fixture with various comment types |
packages/transformers/test/fixtures/remove-comments/basic.js.output.html |
Expected output for the test |
docs/packages/transformers.md |
Documentation with usage examples and option descriptions |
test/exports/@shikijs/transformers.yaml |
Export validation test ensuring the transformer is properly exported |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| suite( | ||
| 'remove-comments', | ||
| import.meta.glob('./fixtures/remove-comments/*.*', { query: '?raw', import: 'default', eager: true }), | ||
| [ | ||
| transformerRemoveComments(), | ||
| transformerRemoveLineBreak(), | ||
| ], | ||
| code => `${code} | ||
| <style> | ||
| body { margin: 0; } | ||
| .shiki { padding: 1em; } | ||
| .line { display: block; width: 100%; height: 1.2em; } | ||
| </style>`, | ||
| undefined, | ||
| { includeExplanation: true }, | ||
| ) |
There was a problem hiding this comment.
Test coverage for transformerRemoveComments is limited to a single basic JavaScript file. Consider adding test fixtures for:
- The
removeEmptyLines: falseoption - Different comment types (JSDoc comments, multi-line comments spanning multiple lines)
- Different languages (TypeScript, Python, etc.) to ensure scope detection works across grammars
- Edge cases like comments within strings (which should not be removed)
Other transformers in this codebase (e.g., highlight) have multiple test fixtures covering various scenarios.
| @@ -0,0 +1,6 @@ | |||
| // This is a comment | |||
| const x = 1; // Inline comment | |||
There was a problem hiding this comment.
Unused variable x.
| const x = 1; // Inline comment |
| const y = 2; | ||
|
|
There was a problem hiding this comment.
Unused variable y.
| const y = 2; |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Title: feat(transformers): add transformerRemoveComments
Description
This PR introduces a new transformer [transformerRemoveComments]
This transformer allows users to automatically remove comments from their code snippets. This is particularly useful for scenarios like presentation slides or minimal examples where comments might be distracting or consume valuable vertical space.
It uses
includeExplanation: trueto identify tokens with comment scopes (e.g.,comment.line,comment.block) and filters them out. It also includes an optionremoveEmptyLines(defaulting totrue) to clean up lines that become empty after comment removal.Type of Change
Checklist