Skip to content

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions crates/biome_formatter/src/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ where
let should_nestle =
leading_comments_iter.peek().is_some_and(|next_comment| {
should_nestle_adjacent_doc_comments(comment, next_comment)
});
}) || comment.piece().text().starts_with("<!--"); // HACK: we don't want to do this weird behavior to HTML comments.

write!(f, [maybe_space(!should_nestle)])?;
}
Expand Down Expand Up @@ -140,7 +140,7 @@ where

let should_nestle = previous_comment.is_some_and(|previous_comment| {
should_nestle_adjacent_doc_comments(previous_comment, comment)
});
}) || comment.piece().text().starts_with("<!--"); // HACK: we don't want to do this weird behavior to HTML comments.

// This allows comments at the end of nested structures:
// {
Expand Down
7 changes: 7 additions & 0 deletions crates/biome_formatter/src/verbatim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ pub struct FormatVerbatimNode<'node, L: Language> {
format_comments: bool,
}

impl<L: Language> FormatVerbatimNode<'_, L> {
pub fn with_format_comments(mut self, format_comments: bool) -> Self {
self.format_comments = format_comments;
self
}
}

impl<Context> Format<Context> for FormatVerbatimNode<'_, Context::Language>
where
Context: CstFormatContext,
Expand Down
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.

75 changes: 73 additions & 2 deletions crates/biome_html_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use biome_formatter::{
prelude::*,
write,
};
use biome_html_syntax::HtmlLanguage;
use biome_rowan::{SyntaxTriviaPieceComments, TextLen};
use biome_html_syntax::{HtmlClosingElement, HtmlLanguage, HtmlOpeningElement, HtmlSyntaxKind};
use biome_rowan::{SyntaxNodeCast, SyntaxTriviaPieceComments, TextLen};
use biome_suppression::parse_suppression_comment;

use crate::context::HtmlFormatContext;
Expand Down Expand Up @@ -96,10 +96,81 @@ impl CommentStyle for HtmlCommentStyle {
CommentKind::Block
}

/// This allows us to override which comments are associated with which nodes.
///
/// While every comment is directly attached to a **syntax token**, Biome actually builds a map of comments to **syntax nodes** separately. This map lives in [`HtmlComments`]. This is so that we can easily look up comments that are associated with a specific node. It's part of how suppression comments are handled.
///
/// This method specifically, however, lets us fine tune which comments are associated with which nodes. This is useful when the default heuristic fails.
fn place_comment(
&self,
comment: DecoratedComment<Self::Language>,
) -> CommentPlacement<Self::Language> {
// Fix trailing comments that are right before EOF being assigned to the wrong node.
//
// The issue is demonstrated in the example below.
// ```html
// Foo
//
// <!-- This comment gets assigned to the text node, despite it being actually attached to the EOF token. -->
// ```
if let Some(token) = comment.following_token() {
if token.kind() == HtmlSyntaxKind::EOF {
return CommentPlacement::trailing(comment.enclosing_node().clone(), comment);
}
}

// Fix trailing comments that should actually be leading comments for the next node.
// ```html
// 123<!--biome-ignore format: prettier ignore-->456
// ```
// This fix will ensure that the ignore comment is assigned to the 456 node instead of the 123 node.
if let Some(following_node) = comment.following_node() {
if comment.text_position().is_same_line() {
return CommentPlacement::leading(following_node.clone(), comment);
}
}
// match (comment.preceding_node(), comment.following_node()) {
// (Some(preceding_node), Some(following_node)) => {
// if preceding_node.kind() == HtmlSyntaxKind::HTML_CONTENT
// && following_node.kind() == HtmlSyntaxKind::HTML_CONTENT
// {
// return CommentPlacement::leading(following_node.clone(), comment);
// }

// if matches!(
// following_node.kind(),
// HtmlSyntaxKind::HTML_CONTENT
// | HtmlSyntaxKind::HTML_ELEMENT
// | HtmlSyntaxKind::HTML_SELF_CLOSING_ELEMENT
// | HtmlSyntaxKind::HTML_BOGUS_ELEMENT
// ) {
// return CommentPlacement::leading(following_node.clone(), comment);
// }
// }
// _ => {}
// }

// move leading comments placed on closing tags to trailing tags of previous siblings, or to be dangling if no siblings are present.
if let Some(_closing_tag) = comment
.following_node()
.and_then(|node| node.clone().cast::<HtmlClosingElement>())
{
if let Some(_preceding_opening_tag) = comment
.preceding_node()
.and_then(|node| node.clone().cast::<HtmlOpeningElement>())
{
return CommentPlacement::dangling(
comment.preceding_node().unwrap().clone(),
comment,
);
} else {
return CommentPlacement::trailing(
comment.preceding_node().unwrap().clone(),
comment,
);
}
}

CommentPlacement::Default(comment)
}
}
38 changes: 0 additions & 38 deletions crates/biome_html_formatter/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
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
Expand Up @@ -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),
Expand Down
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
Expand Up @@ -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;
Expand Down
32 changes: 17 additions & 15 deletions crates/biome_html_formatter/src/html/auxiliary/root.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
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)?;

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

Ok(())
write!(
f,
[
bom_token.format(),
directive.format(),
html.format(),
hard_line_break(),
format_removed(&eof_token?),
]
)
}
}
Loading
Loading