-
-
Notifications
You must be signed in to change notification settings - Fork 720
fix(formatter): BestFitting doesn't exactly matches the conditinalGroup behavior in Prettier
#14983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
BestFitting doesn't exactly matches the conditinalGroup behavior in Prettier
CodSpeed Performance ReportMerging #14983 will not alter performanceComparing Summary
Footnotes
|
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
## [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 <6259812+leaysgur@users.noreply.github.com>
## [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)
## [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)

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.