-
-
Notifications
You must be signed in to change notification settings - Fork 573
Rework oxc_prettier
#5068
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
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 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~! |
I've been busy with another matter, so there's been a slight delay, but I wanted to report my progress. For the past few weeks, I have been working on a PoC implementation based on As a first step, I have implemented the foundational part that performs the minimal formatting. But at this point, I'd like to consult on the future direction. In simple terms, I want to decide whether to continue with the current Prettier-based or to go with the Biome-based. Originally, considering future extensibility and maintainability, it seemed better to go with the already normalized Biome-based code, which is why I started this PoC. However, while the IR and Printer could be mostly ported as-is, I'm concerned that the amount of code has turned out to be larger than expected to port IR builders and other stuff. Furthermore, I feel that more time is needed to understand the purpose and background of them.(And for future contributors.) Of course, I think that could be simplified with some effort, but judging is the matter, and there is no way to ensure that those changes wouldn't cause issues for now. @Boshen (I apologize for bothering you when you're busy, but...) Until then, I'll continue working on the Biome-based approach. 💪🏻 FYI: This is also a WIP, but the numbers are quite interesting. 👀 |
Can you tell more about which parts your concerned? Or what has turned out to be larger than expected? I saw a question around source text in the git repo. You may want to take a look at the I saw another question around
This is interesting indeed and might be a good and very easy improvement for downstream users |
Thanks as always!
It was mainly about things related to the However, on second thought, this impression might just be due to the fact that I'm just feeling unfamiliar with these mechanisms because they are not present in Prettier (right?). And as soon as I encounter real use cases, I might understand their necessity. And as for the total amount of code, it might just be that I’m not very familiar with Rust yet, and for experienced developers it might not be a significant concern at all.
Sorry, this is my skill issue too. 😓 When I attempted to include In any case, I think I need to spend a little more time on it for myself. |
Prettier doesn't have If I were you, I'd probably delete
|
Thank you, I've learned a lot from you!
Actually, I hadn't copied them in the initial stage. However, since they seemed to be used in builders related to More importantly, I'm glad I learned how |
Oh, I wasn't aware that it is used in |
Hello, apologies if this has been discussed before but would the formatter be opinionated as prettier ? |
Ultimately, we talked about making it configurable/flexible. |
Part of #5068, suggested by @ematipico and @MichaReiser, where @leaysgur and I explored using biome formatter as the base and made a success. All credits to the old Rome and the new Biome team. This PR is in a boilerplate state where some of the functions are marked as `todo!()`. I also made the following changes: * changed the base to be less generic, removed the `Language` and `Context` generics * expanded the formatter lifetime to include an `'ast` lifetime * added access to the source text and comments * added a mechanism for tracking and accessing parent ast nodes (done by our codegen) * removed most of the unit tests because they no longer compile * removed the `oxc_prettier` crate
Uh oh!
There was an error while loading. Please reload this page.
Note
We have decided to fork the Biome formatter code and rework as
oxc_formatter
.@Dunqing is now actively working on this to improve compatibility and resolve #10179.
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: