fix(formatter): BestFitting doesn't exactly matches the conditinalGroup behavior in Prettier#14983
Merged
graphite-app[bot] merged 1 commit intomainfrom Oct 27, 2025
Conversation
This was referenced Oct 27, 2025
This was referenced Oct 27, 2025
Member
Author
This was referenced Oct 27, 2025
BestFitting doesn't exactly matches the conditinalGroup behavior in Prettier
CodSpeed Performance ReportMerging #14983 will not alter performanceComparing Summary
Footnotes
|
Contributor
Merge activity
|
…Group` behavior in Prettier (#14983) I found a case where Oxfmt's output differs from Prettier. Honestly, I think the `Oxfmt` output is prettier than `Prettier`. Anyway, I investigated this case to find the root cause. ### Failed example Input: ```ts const defaultColorDecoratorsEnablement = accessor .get(IConfigurationService) .getValue<"auto" | "always" | "never">( "longlonglonglonglonglonglonglonglong", ); ``` Oxfmt and Biome's output: ```ts const defaultColorDecoratorsEnablement = accessor .get(IConfigurationService) .getValue<"auto" | "always" | "never">( "longlonglonglonglonglonglonglonglong", ); ``` Prettier output: ```ts const defaultColorDecoratorsEnablement = accessor .get(IConfigurationService) .getValue< "auto" | "always" | "never" >("longlonglonglonglonglonglonglonglong"); ``` ### Investigating the cause Initially, I think the problem is that we generate inconsistent `IR`, so the output is different, which is a reasonable suspicion because, so far, almost all issues are due to this reason. However, after carefully comparing both IR of Oxfmt and Prettier, they are essentially the same, so the problem is moved to the printing IR part. After spending a few days on this case, I finally found the reason why they produce the same IR but print different outputs. The cause is inside the `BestFitting`, which is a graceful solution for Prettier's `conditionalGroup`. Let's take a look at how Prettier handles `conditionalGroup` #### Prettier implementation: **conditionalGroup** https://github.com/prettier/prettier/blob/7584432401a47a26943dd7a9ca9a8e032ead7285/src/document/builders.js#L99-L106 ```js /** * @param {Doc[]} states * @param {object} [opts] - TBD ??? * @returns Doc */ function conditionalGroup(states, opts) { return group(states[0], { ...opts, expandedStates: states }); } ``` **handling of conditionalGroup** https://github.com/prettier/prettier/blob/7584432401a47a26943dd7a9ca9a8e032ead7285/src/document/printer.js#L408-L445 ```js // Expanded states are a rare case where a document // can manually provide multiple representations of // itself. It provides an array of documents // going from the least expanded (most flattened) // representation first to the most expanded. If a // group has these, we need to manually go through // these states and find the first one that fits. // eslint-disable-next-line no-lonely-if if (doc.expandedStates) { const mostExpanded = doc.expandedStates.at(-1); if (doc.break) { cmds.push({ ind, mode: MODE_BREAK, doc: mostExpanded }); break; } else { for (let i = 1; i < doc.expandedStates.length + 1; i++) { if (i >= doc.expandedStates.length) { cmds.push({ ind, mode: MODE_BREAK, doc: mostExpanded }); break; } else { const state = doc.expandedStates[i]; /** @type {Command} */ const cmd = { ind, mode: MODE_FLAT, doc: state }; if (fits(cmd, cmds, rem, hasLineSuffix, groupModeMap)) { cmds.push(cmd); break; } } } } } else { cmds.push({ ind, mode: MODE_BREAK, doc: doc.contents }); } ``` #### Oxfmt implementation https://github.com/oxc-project/oxc/blob/d9f7df8bc6bf7e48c90ba99638502c87afba0e04/crates/oxc_formatter/src/formatter/printer/mod.rs#L366-L418 The difference between the implementations is in handling different `expandedStates` (Called as `variants` in Oxfmt). In prettier, they will try to get all `expandedStates` except the first one to check whether they fit or not in the `MODE_FLAT` mode. If not, they will fall back to the `mostExpanded` and print it in `MODE_BREAK` mode.
63c324d to
4a84e44
Compare
54c509e to
3940f3a
Compare
Base automatically changed from
10-25-fix_formatter_lign_the_logic_of_printing_type_parameters_parameters_and_return_type_for_functions_with_prettier
to
main
October 27, 2025 09:47
leaysgur
added a commit
that referenced
this pull request
Oct 30, 2025
## [0.9.0] - 2025-10-30 ### 🚀 Features - 8fe7e85 formatter: Support printing Formatter IR (#14855) (Dunqing) ### 🐛 Bug Fixes - a6b6ef8 formatter: Correct calculating layout for `TSNonNullExpression` in `StaticMemberExpression` (#15065) (Dunqing) - 99bd995 formatter: Print parenthesis for sequence expression in `ReturnStatement` and `ExpressionStatement` (#15062) (Dunqing) - f3fb998 formatter: Correct printing comments for `TSAsExpression` (#15061) (Dunqing) - 1e4a018 formatter: Correct checking of the short argument for `CallArguments` (#15055) (Dunqing) - c0dfd9b formatter: Print comments before fat arrow as-is for `ArrowFunctionExpression` (#15050) (Dunqing) - 206b519 formatter: Should hug parameter with `TSMappedType` type annotation (#15049) (Dunqing) - e48c604 formatter: Incorrect formatting of a function with `this` parameter (#15031) (Dunqing) - a9f0c45 formatter: Decorators and class method on the same line shouldn't be broken by a leading comment of the method (#15029) (Dunqing) - 43d74e4 formatter: Handle `<CRLF>` for `SourceText` (#15016) (leaysgur) - 34fab40 formatter: Correct calculating layout for `ChainExpression` in `StaticMemberExpression` (#14986) (Dunqing) - 68dc101 formatter: Should not break when the parent of root of `StaticMemberExpression` is used as the `Argument` of `NewExpression` (#14985) (Dunqing) - 071b739 formatter: Align the short argument handling for UnaryExpression with Prettier (#14984) (Dunqing) - 3940f3a formatter: `BestFitting` doesn't exactly matches the `conditinalGroup` behavior in Prettier (#14983) (Dunqing) - 4a84e44 formatter: Align the logic of printing type parameters, parameters, and return type for functions with Prettier (#14942) (Dunqing) - 68c1f2a formatter: Non-nested static member expressions will never break (#14929) (Dunqing) - 42adc47 formatter: Check whether a type alias is complex when its right hand side never break (#14928) (Dunqing) - e501f13 formatter: Should not add a trailing comma for long curried calls when `trailingComma` is es5 (#14913) (Dunqing) ### 🚜 Refactor - 7d64291 formatter: Simplify printing ClassElement with a semicolon (#15030) (Dunqing) - 5de99c2 formatter: Export unified way to get_parse_options (#15027) (leaysgur) - f6f22e2 formatter: Clean up unneeded implementations for printing comments (#14935) (Dunqing) - 7a2b9d1 formatter: Improve printing trailing comments (#14934) (Dunqing) - ba10caa formatter: Align printing trailing comments with Prettier (#14927) (Dunqing) - 597c9e8 formatter: Remove redundunt public API (#14915) (leaysgur) ### ⚡ Performance - 467b3a1 formatter: Optimize grouping logic for call arguments (#15033) (Dunqing) Co-authored-by: leaysgur <[email protected]>
graphite-app bot
pushed a commit
that referenced
this pull request
Oct 30, 2025
## [0.9.0] - 2025-10-30 ### 🚀 Features - 8fe7e85 formatter: Support printing Formatter IR (#14855) (Dunqing) ### 🐛 Bug Fixes - a6b6ef8 formatter: Correct calculating layout for `TSNonNullExpression` in `StaticMemberExpression` (#15065) (Dunqing) - 99bd995 formatter: Print parenthesis for sequence expression in `ReturnStatement` and `ExpressionStatement` (#15062) (Dunqing) - f3fb998 formatter: Correct printing comments for `TSAsExpression` (#15061) (Dunqing) - 1e4a018 formatter: Correct checking of the short argument for `CallArguments` (#15055) (Dunqing) - c0dfd9b formatter: Print comments before fat arrow as-is for `ArrowFunctionExpression` (#15050) (Dunqing) - 206b519 formatter: Should hug parameter with `TSMappedType` type annotation (#15049) (Dunqing) - e48c604 formatter: Incorrect formatting of a function with `this` parameter (#15031) (Dunqing) - a9f0c45 formatter: Decorators and class method on the same line shouldn't be broken by a leading comment of the method (#15029) (Dunqing) - 43d74e4 formatter: Handle `<CRLF>` for `SourceText` (#15016) (leaysgur) - 34fab40 formatter: Correct calculating layout for `ChainExpression` in `StaticMemberExpression` (#14986) (Dunqing) - 68dc101 formatter: Should not break when the parent of root of `StaticMemberExpression` is used as the `Argument` of `NewExpression` (#14985) (Dunqing) - 071b739 formatter: Align the short argument handling for UnaryExpression with Prettier (#14984) (Dunqing) - 3940f3a formatter: `BestFitting` doesn't exactly matches the `conditinalGroup` behavior in Prettier (#14983) (Dunqing) - 4a84e44 formatter: Align the logic of printing type parameters, parameters, and return type for functions with Prettier (#14942) (Dunqing) - 68c1f2a formatter: Non-nested static member expressions will never break (#14929) (Dunqing) - 42adc47 formatter: Check whether a type alias is complex when its right hand side never break (#14928) (Dunqing) - e501f13 formatter: Should not add a trailing comma for long curried calls when `trailingComma` is es5 (#14913) (Dunqing) ### 🚜 Refactor - 7d64291 formatter: Simplify printing ClassElement with a semicolon (#15030) (Dunqing) - 5de99c2 formatter: Export unified way to get_parse_options (#15027) (leaysgur) - f6f22e2 formatter: Clean up unneeded implementations for printing comments (#14935) (Dunqing) - 7a2b9d1 formatter: Improve printing trailing comments (#14934) (Dunqing) - ba10caa formatter: Align printing trailing comments with Prettier (#14927) (Dunqing) - 597c9e8 formatter: Remove redundunt public API (#14915) (leaysgur) ### ⚡ Performance - 467b3a1 formatter: Optimize grouping logic for call arguments (#15033) (Dunqing) ### 💼 Other - aceff66 oxfmt: V0.9.0 (#15088) (Boshen)
graphite-app bot
pushed a commit
that referenced
this pull request
Oct 30, 2025
## [0.9.0] - 2025-10-30 ### 🚀 Features - 8fe7e85 formatter: Support printing Formatter IR (#14855) (Dunqing) ### 🐛 Bug Fixes - a6b6ef8 formatter: Correct calculating layout for `TSNonNullExpression` in `StaticMemberExpression` (#15065) (Dunqing) - 99bd995 formatter: Print parenthesis for sequence expression in `ReturnStatement` and `ExpressionStatement` (#15062) (Dunqing) - f3fb998 formatter: Correct printing comments for `TSAsExpression` (#15061) (Dunqing) - 1e4a018 formatter: Correct checking of the short argument for `CallArguments` (#15055) (Dunqing) - c0dfd9b formatter: Print comments before fat arrow as-is for `ArrowFunctionExpression` (#15050) (Dunqing) - 206b519 formatter: Should hug parameter with `TSMappedType` type annotation (#15049) (Dunqing) - e48c604 formatter: Incorrect formatting of a function with `this` parameter (#15031) (Dunqing) - a9f0c45 formatter: Decorators and class method on the same line shouldn't be broken by a leading comment of the method (#15029) (Dunqing) - 43d74e4 formatter: Handle `<CRLF>` for `SourceText` (#15016) (leaysgur) - 34fab40 formatter: Correct calculating layout for `ChainExpression` in `StaticMemberExpression` (#14986) (Dunqing) - 68dc101 formatter: Should not break when the parent of root of `StaticMemberExpression` is used as the `Argument` of `NewExpression` (#14985) (Dunqing) - 071b739 formatter: Align the short argument handling for UnaryExpression with Prettier (#14984) (Dunqing) - 3940f3a formatter: `BestFitting` doesn't exactly matches the `conditinalGroup` behavior in Prettier (#14983) (Dunqing) - 4a84e44 formatter: Align the logic of printing type parameters, parameters, and return type for functions with Prettier (#14942) (Dunqing) - 68c1f2a formatter: Non-nested static member expressions will never break (#14929) (Dunqing) - 42adc47 formatter: Check whether a type alias is complex when its right hand side never break (#14928) (Dunqing) - e501f13 formatter: Should not add a trailing comma for long curried calls when `trailingComma` is es5 (#14913) (Dunqing) ### 🚜 Refactor - 7d64291 formatter: Simplify printing ClassElement with a semicolon (#15030) (Dunqing) - 5de99c2 formatter: Export unified way to get_parse_options (#15027) (leaysgur) - f6f22e2 formatter: Clean up unneeded implementations for printing comments (#14935) (Dunqing) - 7a2b9d1 formatter: Improve printing trailing comments (#14934) (Dunqing) - ba10caa formatter: Align printing trailing comments with Prettier (#14927) (Dunqing) - 597c9e8 formatter: Remove redundunt public API (#14915) (leaysgur) ### ⚡ Performance - 467b3a1 formatter: Optimize grouping logic for call arguments (#15033) (Dunqing) ### 💼 Other - 6368793 oxfmt: V0.9.0 (#15091) (Boshen) - aceff66 oxfmt: V0.9.0 (#15088) (Boshen)
taearls
pushed a commit
to taearls/oxc
that referenced
this pull request
Dec 11, 2025
…xc-project#16028) Revert the change made in oxc-project#14983. Even though Prettier doesn't fix the bug yet, Prettier at least confirms that this is a bug in Prettier, so it would be better to revert it to avoid introducing weird formatting
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.

I found a case where Oxfmt's output differs from Prettier. Honestly, I think the
Oxfmtoutput is prettier thanPrettier.Anyway, I investigated this case to find the root cause.
Failed example
Input:
Oxfmt and Biome's output:
Prettier output:
Investigating the cause
Initially, I think the problem is that we generate inconsistent
IR, so the output is different, which is a reasonable suspicion because, so far, almost all issues are due to this reason. However, after carefully comparing both IR of Oxfmt and Prettier, they are essentially the same, so the problem is moved to the printing IR part.After spending a few days on this case, I finally found the reason why they produce the same IR but print different outputs. The cause is inside the
BestFitting, which is a graceful solution for Prettier'sconditionalGroup.Let's take a look at how Prettier handles
conditionalGroupPrettier implementation:
conditionalGroup
https://github.com/prettier/prettier/blob/7584432401a47a26943dd7a9ca9a8e032ead7285/src/document/builders.js#L99-L106
handling of conditionalGroup
https://github.com/prettier/prettier/blob/7584432401a47a26943dd7a9ca9a8e032ead7285/src/document/printer.js#L408-L445
Oxfmt implementation
oxc/crates/oxc_formatter/src/formatter/printer/mod.rs
Lines 366 to 418 in d9f7df8
The difference between the implementations is in handling different
expandedStates(Called asvariantsin Oxfmt).In prettier, it will try to get all
expandedStatesto check whether they fit or not in theMODE_FLATmode. If not, they will fall back to themostExpandedand print it inMODE_BREAKmode.In Oxfmt, it will try to get all
variantsexcept the last one (most expanded) to check whether they fit or not in thePrintMode::Flatmode. If not, they will fall back to themostExpandedand print it inPrintMode::Expandedmode.So the root cause is Oxfmt, which doesn't check the
mostExpandedone, whether it fits in thePrintMode::Fitmode. After aligning this, the issue was solved.Thinking
After understanding the use of
conditionalGroup, although this issue is due to Oxfmt not exactly aligning with Prettier's behavior, I suspect this is a bug in Prettier becausemostExpandedis expected to be printed in break mode, butPrettierprints it in flat mode if it fits. It's not intuitive, and it's not as if the variable names describe it. Additionally, this case is a little complex, and the Prettier tests don't include such cases, so the Prettier team probably wasn't aware of this case.