Skip to content

fix(formatter): preserve trailing comma for single type parameter in .mts/.cts files#18934

Closed
Boshen wants to merge 1 commit intomainfrom
fix/formatter-mts-cts-trailing-comma
Closed

fix(formatter): preserve trailing comma for single type parameter in .mts/.cts files#18934
Boshen wants to merge 1 commit intomainfrom
fix/formatter-mts-cts-trailing-comma

Conversation

@Boshen
Copy link
Member

@Boshen Boshen commented Feb 4, 2026

Summary

  • Preserve trailing comma for single generic type parameters in arrow functions in .mts and .cts files
  • Previously only JSX files (.jsx, .tsx) had this behavior, but .mts/.cts files also require it for disambiguation
  • Add test cases for both .mts and .cts files

Closes #18924

🤖 Generated with Claude Code

….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>
@Boshen Boshen requested a review from Dunqing as a code owner February 4, 2026 09:38
Copilot AI review requested due to automatic review settings February 4, 2026 09:38
@github-actions github-actions bot added A-formatter Area - Formatter C-bug Category - Bug labels Feb 4, 2026
@Dunqing
Copy link
Member

Dunqing commented Feb 4, 2026

Already fixed in #18928

@Dunqing Dunqing closed this Feb 4, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 .mts and .cts files, matching the existing behavior for JSX files
  • Fixed a typo in the comment ("i.i." → "i.e.")
  • Added comprehensive test cases for both .mts and .cts file 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-formatter Area - Formatter C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

formatter: Diff with Prettier on generic parameter trailing comma in .mts

3 participants