fix(formatter): preserve trailing comma for single type parameter in .mts/.cts files#18934
Closed
fix(formatter): preserve trailing comma for single type parameter in .mts/.cts files#18934
Conversation
….mts/.cts files
In `.mts` and `.cts` files, single type parameters in arrow functions require
a trailing comma to disambiguate from JSX-like syntax (e.g., `<T,>() => {}`).
The formatter was incorrectly removing this comma because it only checked for
JSX files, not `.mts`/`.cts` extensions.
Closes #18924
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Member
|
Already fixed in #18928 |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a formatter issue where trailing commas were incorrectly removed from single type parameters in arrow functions within .mts and .cts files. TypeScript requires these trailing commas (or explicit constraints) to disambiguate generic type parameters from JSX-like syntax in these module file extensions.
Changes:
- Extended the formatter logic to preserve trailing commas for single type parameters in
.mtsand.ctsfiles, matching the existing behavior for JSX files - Fixed a typo in the comment ("i.i." → "i.e.")
- Added comprehensive test cases for both
.mtsand.ctsfile extensions
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| crates/oxc_formatter/src/print/type_parameters.rs | Updated logic to check for .mts and .cts file extensions in addition to JSX files when determining whether to preserve trailing commas; fixed comment typo; added explanatory documentation |
| crates/oxc_formatter/tests/fixtures/ts/trailing-comma/issue-18924-mts.mts | Added test input for .mts file with single type parameter, constrained parameter, and multiple parameters |
| crates/oxc_formatter/tests/fixtures/ts/trailing-comma/issue-18924-mts.mts.snap | Snapshot showing that trailing comma is correctly preserved for single type parameter in .mts files |
| crates/oxc_formatter/tests/fixtures/ts/trailing-comma/issue-18924-cts.cts | Added test input for .cts file with the same test scenarios as .mts |
| crates/oxc_formatter/tests/fixtures/ts/trailing-comma/issue-18924-cts.cts.snap | Snapshot showing that trailing comma is correctly preserved for single type parameter in .cts files |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
.mtsand.ctsfiles.jsx,.tsx) had this behavior, but.mts/.ctsfiles also require it for disambiguation.mtsand.ctsfilesCloses #18924
🤖 Generated with Claude Code