-
-
Notifications
You must be signed in to change notification settings - Fork 475
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. |
Does this mean that you aim to implement Prettier equivalent which achieves with 60+ utils? 🫨 FYI: Babel also has relevant code(just 300 loc), but that did not seem to meet Prettier's requirement. |
We need to figure out what exactly is prettier doing with those 60+ utils 🥲 |
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. |
So, what I would like to discuss(confirm?) is: where should If the goal is to achieve compatibility, I think it would mean porting almost all of Prettier’s lines.
I believe the original aim of this issue was this, but again, is this an acceptable? If we decide to proceed, let’s consider how we might specifically approach it... Fortunately, the 3 phases have isolated inputs and outputs, so:
I think reducing the gaps between each phase will be important. For that, we’ll need some way to directly compare the result of
// OXC
let oxcASTWithComments = oxc_prettier::parse(text);
let oxcEstreeASTWithComments = estreeJSON(oxcASTWithComments);
// let oxcBabelASTWithComments = babelJSON(oxcASTWithComments);
// Prettier
let prettierEstreeASTWithComments = Prettier.parse(text, { parser: "meriyah" });
// let prettierBabelASTWithComments = Prettier.parse(text, { parser: "babel" });
// Diff!
assert(oxcEstreeASTWithComments, prettierEstreeASTWithComments); What if we don’t aim for full compatibility?
But in that case, maybe it would be more like Anyway, this is what I was thinking these days. What do you think? |
For the long run, I envision a more configurable and less opinionated formatter. Under this assumption, we can relax the "100% compatibility" constraint. My intention is to reach a high compatibility with prettier so we can bootstrap ourself, and then start deviating behavior. I've also looked at the prettier and rustfmt issues before, comment positioning is a really difficult subject due to the natural of matching the original intent of the comment. To move things forward, I suggest @leaysgur to start refactoring our prettier code in preparation for what to come, I believe you know more about prettier than I do. You may ask @Sysix for help given they have started working on this area as well. Our code for P2 and P3 is also incomplete, I suggest to do a little bit of rework first, as well as adding more debug utilities. |
I don't think we need to do this. This will leave us too coupled with the prettier implementation, and it may end up being a waste of effort. We are already matching half of the generated text, a few more iterations of refactoring and implementing details should close our gap to prettier. |
I see, what I likely concerned about was: what exactly is meant by "high compatibility". 😅 So, It's good to know that we're not necessarily aiming for 100% compatibility. It's still uncertain how much the percentage will drop if we give up on porting completeness for comment handling, but for now, I'll move forward. Following your suggestion:
I’ll work on these whenever I have time! 🚀
I love this idea! |
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.
|
@leaysgur I was just informed that So instead of using our current unfinished IR and printer, we can replace them with I pinged you on discord, for full context. |
Thanks for the pinning!
Yes, and I just started reading through I hadn’t considered it as a viable option for OXC (though I’m not entirely sure why), but if we shift our goal to generating I’ll look into this soon (after finishing the regex parser rework), including:
|
I think it's the same thing as |
Hey all, just dropping by!
I feel adding support for comments to the current implementation should be feasible. Since the oxc_ast's I could do a PoC on my fork and have it up in some time. I don't know how much more work the parenthesis bit will take, but my hunch is that emitting biome IR might be more work than supporting comments in oxc_prettier's current form. Of course, people who've worked on this will know better. Curious to know what you think! |
I want to share my overall idea from end user perspective: After oxc_prettier will be created I suggest to create something like Currently my lint CI time is 50% time for prettier eslint (too much). |
you might want to consider running prettier (or oxc_prettier later) separately: |
Recently, I've been reading and researching the code related to Biome's Formatter.
|
Progress: An experiment to see if we can use just the Based on
My personal impression is that it doesn't seem impossible, but I'm not yet sure if it's the right path. 😅 Here's what I've found out:
and,
I mentioned "minimal" at the beginning because this Gist does not use the
Next, I will check this part. I don't know whether it's possible or not in the first place, though. 🙄 |
@leaysgur Thank you so much for the research. I think it's enough evidence to just implement our own thing (with some parts ported from Biome)? |
Yes..., I also think that's probably the case. And if we choose to go our own way, even if there are parts we can refer to, I feel there won't be any parts where we can borrow as code. 😓 Go back to the square one, I will re-start by understanding/refactoring the current code, and then think about what to do next. |
@leaysgur will lead this project, and is free to make any changes to the |
Thanks~! Progress is likely to be slow due to limited resources, but I will try to update progress every 2-3 days or so at the latest comment on this issue. |
List of Prettier's print implementations and exports:
I don't think we need a 1:1 counterpart for these, but aligning them makes it easier to track our progress. Next, I'll check our current implementation. Fully or partially implemented? not yet covered? etc... |
You can assume all are partially implemented 😅 Cross referencing the function in our code to the prettier counter part would definitely make things easier to follow. |
JS / AST Entry points👻:
JS / Printer APIs👻: Fn not exists / 👀: Fn exists, to be verified / 🚧: W.I.P. / ✨: Completed
JSX, TSTBD, later... 🥲 Other TODOs
|
The basic flow is:
The current issues in 1: It assumes specifying However, as mentioned above, this cannot keep the 2: Basically it can be implemented for each AST node with default: false like Biome does, but... And for Note For the time being, I'd like to work on updating the table above, as possible as I can find the time. 💪🏻 Unfortunately, my family has had the flu... I won't be able to do anything else this year. 🤒 Next planned TBD:
|
Some refactoring works to update #5068 (comment) table. - Implement `array::is_consisely_printed_array()` and use it - This improved compat-rate a bit ✌🏻 - Align exported function names to align prettier's - Split `format/mod.rs` into `js`, `jsx` and `typescript` - Move `format/*.rs` to `format/print/*.rs`
Part of #5068 - [x] RegExpLiteral - [x] BigIntLiteral - [x] NumericLiteral - [x] StringLiteral - [x] replaceEndOfLine - [x] makeString keep usless escape - [x] NullLiteral - [x] BooleanLiteral
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: