+ // ```
+ //
+ // Without this rule, the comment would be a trailing comment of the preceding attribute
+ // (or a dangling comment of the opening element if it's the first attribute), and
+ // `is_suppressed(class_attr)` would return false.
+ //
+ // NOTE: Do NOT use `comment.kind().is_line()` here to detect `//` comments — the
+ // comment kind is determined after `place_comment()` runs in the pipeline, so
+ // `kind()` always returns `Block` at this point. Instead, inspect the text directly.
+ if matches!(
+ comment.enclosing_node().kind(),
+ HtmlSyntaxKind::HTML_OPENING_ELEMENT | HtmlSyntaxKind::HTML_SELF_CLOSING_ELEMENT
+ ) && let Some(following_node) = comment.following_node()
+ {
+ // Only re-attach for attribute-level following nodes.
+ // The closing tag case is already handled by the check above.
+ if !HtmlClosingElement::can_cast(following_node.kind()) {
+ return CommentPlacement::leading(following_node.clone(), comment);
+ }
+ }
+
// Fix trailing comments that should actually be leading comments for the next node.
// ```html
// 123456
diff --git a/crates/biome_html_formatter/src/verbatim.rs b/crates/biome_html_formatter/src/verbatim.rs
index d3d98d1b2850..657cb833107c 100644
--- a/crates/biome_html_formatter/src/verbatim.rs
+++ b/crates/biome_html_formatter/src/verbatim.rs
@@ -6,7 +6,7 @@ use biome_formatter::format_element::tag::VerbatimKind;
use biome_formatter::formatter::Formatter;
use biome_formatter::prelude::{
Tag, empty_line, expand_parent, format_with, hard_line_break, line_suffix,
- should_nestle_adjacent_doc_comments, soft_line_break_or_space, space, text,
+ should_nestle_adjacent_doc_comments, space, text,
};
use biome_formatter::{
@@ -274,17 +274,15 @@ fn format_leading_comments_impl(
}
CommentKind::Line => {
- // TODO: review logic here
+ // `//` line comments always require a hard line break after them because
+ // everything after `//` to end-of-line is part of the comment. Using
+ // `soft_line_break_or_space` would collapse the comment with the next
+ // attribute onto a single line, turning the `>` into part of the comment.
match comment.lines_after() {
0 => {}
- 1 => {
- if comment.lines_before() == 0 {
- biome_formatter::write!(f, [soft_line_break_or_space()])?;
- } else {
- biome_formatter::write!(f, [hard_line_break()])?;
- }
+ _ => {
+ biome_formatter::write!(f, [hard_line_break()])?;
}
- _ => biome_formatter::write!(f, [empty_line()])?,
}
}
}
diff --git a/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-component-tags.svelte b/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-component-tags.svelte
new file mode 100644
index 000000000000..c9d7ec7e266d
--- /dev/null
+++ b/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-component-tags.svelte
@@ -0,0 +1,15 @@
+
+
+
+
+
diff --git a/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-component-tags.svelte.snap b/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-component-tags.svelte.snap
new file mode 100644
index 000000000000..115d1bbf17ec
--- /dev/null
+++ b/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-component-tags.svelte.snap
@@ -0,0 +1,66 @@
+---
+source: crates/biome_formatter_test/src/snapshot_builder.rs
+info: svelte/comments-in-component-tags.svelte
+---
+
+# Input
+
+```svelte
+
+
+
+
+
+
+```
+
+
+=============================
+
+# Outputs
+
+## Output 1
+
+-----
+Indent style: Tab
+Indent width: 2
+Line ending: LF
+Line width: 80
+Attribute Position: Auto
+Bracket same line: false
+Whitespace sensitivity: css
+Indent script and style: false
+Self close void elements: never
+Trailing newline: true
+-----
+
+```svelte
+
+
+
+
+
+
+```
diff --git a/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-tags.svelte b/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-tags.svelte
new file mode 100644
index 000000000000..8cf5835046e1
--- /dev/null
+++ b/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-tags.svelte
@@ -0,0 +1,58 @@
+
+
text
+
text
+
+
+
text
+
+
+
text
+
+
+
text
+
+
+
text
+
+
+
text
+
+
+
text
+
+
+
+
+
+
+
+
+
diff --git a/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-tags.svelte.snap b/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-tags.svelte.snap
new file mode 100644
index 000000000000..30cdbef52ce4
--- /dev/null
+++ b/crates/biome_html_formatter/tests/specs/html/svelte/comments-in-tags.svelte.snap
@@ -0,0 +1,175 @@
+---
+source: crates/biome_formatter_test/src/snapshot_builder.rs
+info: svelte/comments-in-tags.svelte
+---
+
+# Input
+
+```svelte
+
+
text
+
text
+
+
+
text
+
+
+
text
+
+
+
text
+
+
+
text
+
+
+
text
+
+
+
text
+
+
+
+
+
+
+
+
+
+
+```
+
+
+=============================
+
+# Outputs
+
+## Output 1
+
+-----
+Indent style: Tab
+Indent width: 2
+Line ending: LF
+Line width: 80
+Attribute Position: Auto
+Bracket same line: false
+Whitespace sensitivity: css
+Indent script and style: false
+Self close void elements: never
+Trailing newline: true
+-----
+
+```svelte
+
+
text
+