refactor(formatter): make Format::fmt non-failable#16036
refactor(formatter): make Format::fmt non-failable#16036graphite-app[bot] merged 1 commit intomainfrom
Format::fmt non-failable#16036Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
CodSpeed Performance ReportMerging #16036 will improve performances by 5.36%Comparing Summary
Benchmarks breakdown
Footnotes
|
76fddc3 to
b478d7f
Compare
adf4922 to
d4d6801
Compare
b478d7f to
e1f3d4d
Compare
d4d6801 to
b821907
Compare
b821907 to
622e000
Compare
Format::fmt non-failable
78cd591 to
cb8c1fd
Compare
Merge activity
|
e1f3d4d to
2da2fc1
Compare
cb8c1fd to
531dce4
Compare
531dce4 to
9c2ec78
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors the oxc formatter to make Format::fmt non-failable by removing the FormatResult<()> return type and changing it to return (). The change is based on the observation that only two error cases existed in the codebase (PoorLayout and InvalidDocument), neither of which truly required failing immediately.
Key Changes:
- Removed
FormatResultreturn types from allFormat::fmtandFormatWrite::writeimplementations - Removed
?operators from allwrite!,.fmt(), and formatting calls - Commented out
PoorLayouterror handling logic (to be fixed in #16093) - Changed
InvalidDocumenthandling to useunwrap()in template formatting
Trade-offs:
The PR notes a reduction in prettier compatibility scores (TypeScript: 94.85% → 94.68%, JavaScript: 96.05% → 95.26%) due to temporarily commenting out the PoorLayout logic. This will be addressed in a follow-up PR (#16093).
Reviewed changes
Copilot reviewed 71 out of 73 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tasks/prettier_conformance/snapshots/*.snap.md | Compatibility score reductions due to commented-out PoorLayout logic |
| tasks/ast_tools/src/generators/formatter/*.rs | Updated code generators to produce non-failable formatting code |
| crates/oxc_formatter/src/write/*.rs | Removed FormatResult returns and ? operators from all write implementations |
| crates/oxc_formatter/src/utils/*.rs | Removed FormatResult returns from utility formatting functions |
| crates/oxc_formatter/src/lib.rs | Removed unwrap() after format call (now non-failable) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
9c2ec78 to
a73b116
Compare
This PR is a follow-up to astral-sh/ruff#6613. ### Why can we do it https://github.com/oxc-project/oxc/blob/622e000934d97ade5588f1e8cc3badde96e7328e/crates/oxc_formatter/src/formatter/diagnostics.rs#L11-L29 We can also make it non-failable since the entire program only has two points that can trigger an error, neither of which needs to interrupt the program immediately, and both can be improved to avoid making FormatResult everywhere 1. `FormatError::PoorLayout` This error may occur only when handling call arguments, and only the first or the last argument is a `Function` or `ArrowFunctionExpression`. The error may be thrown during the formatting of them. I commented out the related logic of this in this PR and fixed it in #16093 to make it easier to review 2. `FormatError::InvalidDocument` This is a printer error, but it may also occur only when we format `TaggedTemplateExpression`. There is a formatting logic that will format the template expression and print the IR immediately. If the IR we generated is incorrect, then the error will occur, but this is quite rare! So it's not worth using `FormatResult` everywhere to handle it. Currently, I just `unwrap` it. If we really need to handle it, we can store the error somewhere and return it after formatting in the future. https://github.com/oxc-project/oxc/blob/78cd5910985cece594e74884717f77ad75347b8a/crates/oxc_formatter/src/write/template.rs#L602-L603 ### Key changes The most significant change is: ```diff pub trait Format<'ast, T = ()> { - fn fmt(&self, f: &mut Formatter<'_, 'ast>) -> FormatResult<()>; + fn fmt(&self, f: &mut Formatter<'_, 'ast>); } ``` All remaining changes to the order stem from the above change: 1. Remove all `FormatResult` usages 2. Remove all `?` operators that pair with the `format!` macro 4. Commented out `PoorLayout` related logic, which is going to be fixed in the #16093 ### Benefits We don't need to deal with `Option`, and removing all `Option` handling makes the code cleaner and more readable. Of course, the performance improved a little bit
a73b116 to
39fd864
Compare
This PR is a follow-up on #16036 Move `PoorLayout` handling from `Function` and `ArrowFunctionExpression` to call arguments
This PR is a follow-up to astral-sh/ruff#6613. ### Why can we do it https://github.com/oxc-project/oxc/blob/622e000934d97ade5588f1e8cc3badde96e7328e/crates/oxc_formatter/src/formatter/diagnostics.rs#L11-L29 We can also make it non-failable since the entire program only has two points that can trigger an error, neither of which needs to interrupt the program immediately, and both can be improved to avoid making FormatResult everywhere 1. `FormatError::PoorLayout` This error may occur only when handling call arguments, and only the first or the last argument is a `Function` or `ArrowFunctionExpression`. The error may be thrown during the formatting of them. I commented out the related logic of this in this PR and fixed it in #16093 to make it easier to review 2. `FormatError::InvalidDocument` This is a printer error, but it may also occur only when we format `TaggedTemplateExpression`. There is a formatting logic that will format the template expression and print the IR immediately. If the IR we generated is incorrect, then the error will occur, but this is quite rare! So it's not worth using `FormatResult` everywhere to handle it. Currently, I just `unwrap` it. If we really need to handle it, we can store the error somewhere and return it after formatting in the future. https://github.com/oxc-project/oxc/blob/78cd5910985cece594e74884717f77ad75347b8a/crates/oxc_formatter/src/write/template.rs#L602-L603 ### Key changes The most significant change is: ```diff pub trait Format<'ast, T = ()> { - fn fmt(&self, f: &mut Formatter<'_, 'ast>) -> FormatResult<()>; + fn fmt(&self, f: &mut Formatter<'_, 'ast>); } ``` All remaining changes to the order stem from the above change: 1. Remove all `FormatResult` usages 2. Remove all `?` operators that pair with the `format!` macro 4. Commented out `PoorLayout` related logic, which is going to be fixed in the #16093 ### Benefits We don't need to deal with `Option`, and removing all `Option` handling makes the code cleaner and more readable. Of course, the performance improved a little bit
This PR is a follow-up on #16036 Move `PoorLayout` handling from `Function` and `ArrowFunctionExpression` to call arguments
This PR is a follow-up to astral-sh/ruff#6613. ### Why can we do it https://github.com/oxc-project/oxc/blob/622e000934d97ade5588f1e8cc3badde96e7328e/crates/oxc_formatter/src/formatter/diagnostics.rs#L11-L29 We can also make it non-failable since the entire program only has two points that can trigger an error, neither of which needs to interrupt the program immediately, and both can be improved to avoid making FormatResult everywhere 1. `FormatError::PoorLayout` This error may occur only when handling call arguments, and only the first or the last argument is a `Function` or `ArrowFunctionExpression`. The error may be thrown during the formatting of them. I commented out the related logic of this in this PR and fixed it in oxc-project#16093 to make it easier to review 2. `FormatError::InvalidDocument` This is a printer error, but it may also occur only when we format `TaggedTemplateExpression`. There is a formatting logic that will format the template expression and print the IR immediately. If the IR we generated is incorrect, then the error will occur, but this is quite rare! So it's not worth using `FormatResult` everywhere to handle it. Currently, I just `unwrap` it. If we really need to handle it, we can store the error somewhere and return it after formatting in the future. https://github.com/oxc-project/oxc/blob/78cd5910985cece594e74884717f77ad75347b8a/crates/oxc_formatter/src/write/template.rs#L602-L603 ### Key changes The most significant change is: ```diff pub trait Format<'ast, T = ()> { - fn fmt(&self, f: &mut Formatter<'_, 'ast>) -> FormatResult<()>; + fn fmt(&self, f: &mut Formatter<'_, 'ast>); } ``` All remaining changes to the order stem from the above change: 1. Remove all `FormatResult` usages 2. Remove all `?` operators that pair with the `format!` macro 4. Commented out `PoorLayout` related logic, which is going to be fixed in the oxc-project#16093 ### Benefits We don't need to deal with `Option`, and removing all `Option` handling makes the code cleaner and more readable. Of course, the performance improved a little bit
…oject#16093) This PR is a follow-up on oxc-project#16036 Move `PoorLayout` handling from `Function` and `ArrowFunctionExpression` to call arguments

This PR is a follow-up to astral-sh/ruff#6613.
Why can we do it
oxc/crates/oxc_formatter/src/formatter/diagnostics.rs
Lines 11 to 29 in 622e000
We can also make it non-failable since the entire program only has two points that can trigger an error, neither of which needs to interrupt the program immediately, and both can be improved to avoid making FormatResult everywhere
FormatError::PoorLayoutThis error may occur only when handling call arguments, and only the first or the last argument is a
FunctionorArrowFunctionExpression. The error may be thrown during the formatting of them. I commented out the related logic of this in this PR and fixed it in #16093 to make it easier to reviewFormatError::InvalidDocumentThis is a printer error, but it may also occur only when we format
TaggedTemplateExpression. There is a formatting logic that will format the template expression and print the IR immediately. If the IR we generated is incorrect, then the error will occur, but this is quite rare! So it's not worth usingFormatResulteverywhere to handle it. Currently, I justunwrapit. If we really need to handle it, we can store the error somewhere and return it after formatting in the future.oxc/crates/oxc_formatter/src/write/template.rs
Lines 602 to 603 in 78cd591
Key changes
The most significant change is:
pub trait Format<'ast, T = ()> { - fn fmt(&self, f: &mut Formatter<'_, 'ast>) -> FormatResult<()>; + fn fmt(&self, f: &mut Formatter<'_, 'ast>); }All remaining changes to the order stem from the above change:
FormatResultusages?operators that pair with theformat!macroPoorLayoutrelated logic, which is going to be fixed in the fix(formatter): handle poor layout for grouped call arguments #16093Benefits
We don't need to deal with
Option, and removing allOptionhandling makes the code cleaner and more readable. Of course, the performance improved a little bit