-
-
Notifications
You must be signed in to change notification settings - Fork 525
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
Rework oxc_prettier
#5068
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@leaysgur is writing a series of articles in preparation of this task: I'm also working on comment attachments to unblock prettier. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
For those who are interested in algorithms under the hood, prettier is based on https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf, |
It has been 3 weeks since I started reading the Prettier source code.
There are 3 ways:
It is written in Japanese, but it is all code, so you can understand it. 😉 I also recommend to run
I will post some topics for discussion in a few days. |
As you may know, Prettier's formatting process consists of roughly 3 phases:
Comments are collected in P1 and used in P2. In P1:
As a result, some AST nodes have In P2 (I haven’t read the code in detail here yet),
In OXC, part of the necessary information is already implemented and can be obtained. / #5785 However, just like with Babel, that information may be different from what Prettier requires... So, I think I’ve generally understood "what" Prettier is doing. However, as for "why" Prettier does it that way, I can only say it’s because that’s Prettier’s opinion. Incidentally, there seem to be at least around 120 issues related to JS/TS at the moment, but about 50 of them are related to comments, with some remaining unresolved since as far back as 2017. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Does this mean that oxc_prettier will provide a wide range of configurable options, and offer a preset called |
(Follow up of #5068 (comment)) As I posted above, comments are collected and attached to AST nodes in P1. Most comments are printed with their attached nodes like: [leadingCommentsDoc, nodeDoc]
// or
[nodeDoc, trailingCommentsDoc] But the rest of the comments are handled as needed.
There are about 40 files for printing ESTree AST to Doc.
And 15 files of them print comments on demand.
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@leaysgur will lead this project, and is free to make any changes to the |
Part of #5068 - ExportAllDeclaration - ExportNamedDeclaration - ExportDefaultDeclaration - (ExportNamespaceSpecifier = ExportAllDeclaration+exported) --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Part of #5068 Mostly trivial this time. 😪
Part of #5068 - MemberExpression - Computed - Static, PrivateField
Part of #5068 - ObjectExpression, ObjectPattern - ObjectProperty - ObjectMethod
Part of #5068 Now all AST nodes are once verified. ✌🏻
Part of #5068 - VariableDeclaration - AwaitExpression - SequenceExpression - (ParenthesizedExpression)
Part of #5068 Verified and completed `print/array.rs`, except for comment handling.
Part of #5068 Update `doc.to_string()` output to `Prettier.__debug.formatDoc()` compatible Doc AST json format. ```sh # Usecase cargo run -p oxc_prettier --example prettier --quiet -- --debug | jq . # Advanced cargo run -p oxc_prettier --example prettier --quiet -- --debug | pbcopy # Open Prettier playground, select doc-explorer as parser option, then paste as input! ```
Part of #5068 Full rewrite `print/object`, slight improvement. 😇
Part of #5068 Support `objectWrap` option added in v3.5.0. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Part of #5068 Verify and refactor `print/*`. - Verify the main printing logic w/ refactoring - The `propagate_breaks()` needs to be reworked soon - Test coverage drops slightly because of this, but the root cause is the original `Doc` structure - Export `print_doc_to_string` as a function - Some functions in `format/*` require the printed result to determine formatting... - Update the `Doc` macro comments - Properly escape strings in `Doc` JSON
I'm probably a bit late here but what we did for ruff (Python formatter) was to vendor I also think that building/vendoring is the right choice. It's a fundamental piece of your formatter and you want to have control over it. |
@MichaReiser Thanks for the very useful information! It's not too late at all. I'm still asking myself every day whether to base on Prettier or So far, I mainly checked if it is possible to use On top of that,
To determine which directions is more promising, I spent time to diff the current code with Prettier, examine what is missing while picking low-hanging fruit. (And it's been two months already...!) Even though, if I want to base it on The situation might be different for JS(+JSX+TS) and Python, but do you think it would also be better for OXC to be based on |
Would love to try the prototype from npm 😇 |
Yeah, it's a hard decision!
I can tell you why we decided to use
Our approach was to copy over You could also take a middle ground and copy code more incrementally. That could also be a good learning experience. Start with the From there, you can move on and vendor more of biomejs (or decide to go different ways). Vendor the You can then move on to start investigating comments. Biome comes with a I hope you find some of this helpful. Overall, I encourage you to take what you find useful and throw away/rewrite everything that you don't. You can also take a look at ruff's formatter to see how we changed |
Thank you so much, senpai! 🥹 Your help is extremely valuable, and I really appreciate it. It was a nice discovery for me that there is an example of using the Now that I have a general idea of the approach based on Prettier, I will try to start by copying the Thanks again~! |
Note
@leaysgur is currently examining the following options:
For option 1, see #5068 (comment) for the rest of the details and feel free to contribute.
For option 2, stay tuned for more details. 🚧
Original description
crates/oxc_prettier
was my attempt at the prettier bounty.I thought I could finish it in time, except the fact that I rushed too quickly without looking at all the requirements ... It was too late when I got blocked by printing comments.
In order to rework
oxc_prettier
, we need to understand at least:Doc
IR https://github.com/prettier/prettier/blob/main/commands.md https://github.com/oxc-project/oxc/blob/main/crates/oxc_prettier/src/doc.rsAs for the infrastructure, we already have most of the code:
Feel free to remove everything and start from scratch, and copy over the format code https://github.com/oxc-project/oxc/tree/main/crates/oxc_prettier/src/format
The text was updated successfully, but these errors were encountered: