Skip to content
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

feat(format/html): implement suppression comments #5356

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(format/html): implement suppression comments
dyc3 committed Mar 14, 2025
commit 89290e1b63f31cf85e9a2b4e7b78b1b0e34c9932
14 changes: 0 additions & 14 deletions crates/biome_html_factory/src/generated/node_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions crates/biome_html_factory/src/generated/syntax_factory.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 0 additions & 38 deletions crates/biome_html_formatter/src/generated.rs
Original file line number Diff line number Diff line change
@@ -189,44 +189,6 @@ impl IntoFormat<HtmlFormatContext> for biome_html_syntax::HtmlClosingElement {
)
}
}
impl FormatRule<biome_html_syntax::HtmlComment>
for crate::html::auxiliary::comment::FormatHtmlComment
{
type Context = HtmlFormatContext;
#[inline(always)]
fn fmt(
&self,
node: &biome_html_syntax::HtmlComment,
f: &mut HtmlFormatter,
) -> FormatResult<()> {
FormatNodeRule::<biome_html_syntax::HtmlComment>::fmt(self, node, f)
}
}
impl AsFormat<HtmlFormatContext> for biome_html_syntax::HtmlComment {
type Format<'a> = FormatRefWithRule<
'a,
biome_html_syntax::HtmlComment,
crate::html::auxiliary::comment::FormatHtmlComment,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::html::auxiliary::comment::FormatHtmlComment::default(),
)
}
}
impl IntoFormat<HtmlFormatContext> for biome_html_syntax::HtmlComment {
type Format = FormatOwnedWithRule<
biome_html_syntax::HtmlComment,
crate::html::auxiliary::comment::FormatHtmlComment,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::html::auxiliary::comment::FormatHtmlComment::default(),
)
}
}
impl FormatRule<biome_html_syntax::HtmlContent>
for crate::html::auxiliary::content::FormatHtmlContent
{
1 change: 0 additions & 1 deletion crates/biome_html_formatter/src/html/any/element.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ impl FormatRule<AnyHtmlElement> for FormatAnyHtmlElement {
match node {
AnyHtmlElement::HtmlBogusElement(node) => node.format().fmt(f),
AnyHtmlElement::HtmlCdataSection(node) => node.format().fmt(f),
AnyHtmlElement::HtmlComment(node) => node.format().fmt(f),
AnyHtmlElement::HtmlContent(node) => node.format().fmt(f),
AnyHtmlElement::HtmlElement(node) => node.format().fmt(f),
AnyHtmlElement::HtmlSelfClosingElement(node) => node.format().fmt(f),
10 changes: 0 additions & 10 deletions crates/biome_html_formatter/src/html/auxiliary/comment.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/biome_html_formatter/src/html/auxiliary/mod.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ pub(crate) mod attribute_initializer_clause;
pub(crate) mod attribute_name;
pub(crate) mod cdata_section;
pub(crate) mod closing_element;
pub(crate) mod comment;
pub(crate) mod content;
pub(crate) mod directive;
pub(crate) mod element;
31 changes: 19 additions & 12 deletions crates/biome_html_formatter/src/html/auxiliary/root.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
use crate::prelude::*;
use biome_formatter::write;
use biome_html_syntax::HtmlRoot;
use biome_html_syntax::{HtmlRoot, HtmlRootFields};
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatHtmlRoot;
impl FormatNodeRule<HtmlRoot> for FormatHtmlRoot {
fn fmt_fields(&self, node: &HtmlRoot, f: &mut HtmlFormatter) -> FormatResult<()> {
if let Some(bom) = node.bom_token() {
bom.format().fmt(f)?;
}
if let Some(directive) = node.directive() {
directive.format().fmt(f)?;
}
let HtmlRootFields {
bom_token,
directive,
html,
eof_token,
} = node.as_fields();

node.html().format().fmt(f)?;
dbg!(node.syntax().text_with_trivia());
dbg!(eof_token.as_ref()?.leading_trivia());

if let Ok(eof) = node.eof_token() {
eof.format().fmt(f)?;
}
write!(f, [hard_line_break()])?;
write!(
f,
[
bom_token.format(),
directive.format(),
html.format(),
hard_line_break(),
format_removed(&eof_token?),
]
)?;

Ok(())
}
28 changes: 27 additions & 1 deletion crates/biome_html_formatter/src/html/lists/element_list.rs
Original file line number Diff line number Diff line change
@@ -56,6 +56,8 @@ impl FormatRuleWithOptions<HtmlElementList> for FormatHtmlElementList {
impl FormatRule<HtmlElementList> for FormatHtmlElementList {
type Context = HtmlFormatContext;
fn fmt(&self, node: &HtmlElementList, f: &mut HtmlFormatter) -> FormatResult<()> {
dbg!(node.syntax().text_with_trivia());

if node.is_empty() {
return Ok(());
}
@@ -181,6 +183,11 @@ impl FormatHtmlElementList {
Some(WordSeparator::BetweenWords)
}

Some(HtmlChild::Comment(_)) => {
// FIXME: probably not correct behavior here
Some(WordSeparator::Lines(0))
}

// Last word or last word before an element without any whitespace in between
Some(HtmlChild::NonText(next_child)) => Some(WordSeparator::EndOfText {
is_soft_line_break: !matches!(
@@ -210,6 +217,12 @@ impl FormatHtmlElementList {
}
}

HtmlChild::Comment(comment) => {
// FIXME: definitely wrong behavior
flat.write(&format_args![comment], f);
multiline.write_content(comment, f);
}

// * Whitespace after the opening tag and before a meaningful text: `<div> a`
// * Whitespace before the closing tag: `a </div>`
// * Whitespace before an opening tag: `a <div>`
@@ -315,6 +328,11 @@ impl FormatHtmlElementList {
}
}

Some(HtmlChild::Comment(_)) => {
// FIXME: definitely wrong behavior here.
Some(LineMode::Hard)
}

// Add a hard line break if what comes after the element is not a text or is all whitespace
Some(HtmlChild::NonText(next_non_text)) => {
// In the case of the formatter using the multiline layout, we want to treat inline elements like we do words.
@@ -426,7 +444,7 @@ impl FormatHtmlElementList {
/// Tracks the tokens of [HtmlContent] nodes to be formatted and
/// asserts that the suppression comments are checked (they get ignored).
///
/// This is necessary because the formatting of [HtmlContentList] bypasses the node formatting for
/// This is necessary because the formatting of [HtmlElementList] bypasses the node formatting for
/// [HtmlContent] and instead, formats the nodes itself.
#[cfg(debug_assertions)]
fn disarm_debug_assertions(&self, node: &HtmlElementList, f: &mut HtmlFormatter) {
@@ -524,6 +542,9 @@ enum WordSeparator {
/// `a b`
BetweenWords,

/// Seperator between 2 lines. Creates hard line breaks.
Lines(usize),

/// A separator of a word at the end of a [HtmlText] element. Either because it is the last
/// child in its parent OR it is right before the start of another child (element, expression, ...).
///
@@ -571,6 +592,11 @@ impl Format<HtmlFormatContext> for WordSeparator {
fn fmt(&self, f: &mut Formatter<HtmlFormatContext>) -> FormatResult<()> {
match self {
WordSeparator::BetweenWords => soft_line_break_or_space().fmt(f),
WordSeparator::Lines(count) => match count {
0 => Ok(()),
1 => hard_line_break().fmt(f),
_ => empty_line().fmt(f),
},
WordSeparator::EndOfText {
is_soft_line_break,
is_next_element_whitespace_sensitive,
Loading