-
-
Notifications
You must be signed in to change notification settings - Fork 714
feat(format/html): implement suppression comments #5356
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
Parser conformance results onjs/262
jsx/babel
symbols/microsoft
ts/babel
ts/microsoft
|
CodSpeed Performance ReportMerging #5356 will not alter performanceComparing Summary
Footnotes |
9700f3a
to
89290e1
Compare
89290e1
to
5c441aa
Compare
5c441aa
to
100dcd5
Compare
f50bbb8
to
deaaf94
Compare
deaaf94
to
5d597a8
Compare
5d597a8
to
9bfa5fa
Compare
A quick update on the status of this PR: It's like 90% done, just gotta deal with the other 90%. I say that unironically though. Every edge case I fix seems to result in some other breakage elsewhere. I have legitimately spent 100+ hours debugging various breakages in this PR, mostly because of the differences between "trivia on tokens" vs "trivia associated with nodes based on |
7d57192
to
4ea4ad2
Compare
Totally reasonable! I wished you didn't spend soooo many hours! We can take a break for a bit, or I can take it over for you. Thank you for the efforts! |
I've actually found the root cause to be that the comment formatters in |
7794594
to
29d9f26
Compare
That's weird. CSS, JSON and Graphql use the same infrastructure. I'll look into this when I have time |
29d9f26
to
29e6546
Compare
Ok, so the current regression is complex because it's a hitting the reformatting assertion. There's multiple layers to this. In short, this prettier case shows the regression: <span></span><!--prettier-ignore--><span></span> (note that this internally gets turned into a To run this case:
The intended functionality is that:
1st formatWhen this code is formatted, it becomes this: <span></span><!--prettier-ignore-->
<span></span> How we get here is a a little convoluted. Firstly, the biome/crates/biome_html_formatter/src/comments.rs Lines 78 to 82 in c89e041
This leads to the comment getting formatted as a leading comment. This is the exact line that emits the erroneous hard break in biome/crates/biome_formatter/src/trivia.rs Lines 95 to 98 in c89e041
2nd formatWhen the code is reformatted, we get a different result. <span></span> <!--prettier-ignore-->
<span></span> This time, a space gets added in front of the comment. This is also no good because whitespace sensitivity. The comment correctly gets detected as In biome/crates/biome_formatter/src/trivia.rs Lines 190 to 191 in c89e041
(because The fix?Naturally, the fix for this would be for HTML to have it's own comment trivia formatters that don't add whitespace. This would make sense because HTML needs fine control over the whitespace that is emitted (again, because whitespace sensitivity). In fact, we aggressively opt out of much of the formatter's defaults when it comes to formatting 123<!--prettier-ignore-->456 Normally, we would opt out of biome/crates/biome_html_formatter/src/lib.rs Lines 194 to 196 in c89e041
However, this does not work. This function will never get invoked if the node that is being formatted is suppressed. Instead, it appears that |
It may make sense to have language-specific format rules for comments with a shared implementation in E.g. we (ruff, python formatter) had to customize the comment placement logic too. |
Thank you @MichaReiser , do you have some Ruff code you can share with us, so we can check how you guys fixed the problem? |
It probably makes sense to see how prettier handles HTML comments. All we did is to move the comment formatting into our language implementation instead of having a generic one. See https://github.com/astral-sh/ruff/blob/9ae698fe30cf3526f0e7ae237b800b3ed19a819f/crates/ruff_python_formatter/src/comments/format.rs#L14-L636 |
That makes sense, that's essentially the conclusion I came to last time I worked on this |
|
|
```html | ||
Foo | ||
<!-- Bar -->``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This snapshot looks a little weird because it's not adding an extra newline at the end. Not sure if it's worth fixing in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can fix it later, I think, after all, the HTML parser and formatter are still experimental. We can follow up, I think
crates/biome_html_formatter/tests/specs/prettier/html/interpolation/example.html.snap
Outdated
Show resolved
Hide resolved
I believe this is ready to go. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go :)
Summary
This reverts #4056 to make HTML comments actual comment trivia. It also attempts to fix the formatting for comments as well, trying to preserve whitespace in the same places that prettier does.
This PR is also the result of a cascading clusterfuck of issues that appeared as a result of making this change, so it may seem a little scatterbrained, and a little difficult to review.
This PR also doesn't attempt to handle or reimplement
prettier-ignore-attribute
comments in any way. Example from prettier docs:The reason these exist is because you can't put comments inside the
<>
of HTML tags.Test Plan
CI should pass. Added new tests, and updated snapshots.
I've temporarily disabled
specs::prettier::html::comments::surrounding_empty_line_html
because I'm actually loosing my mind and its the only one failing debug assertions now. 🫠 I'll fix it later.