Skip to content

Commit 26baa70

Browse files
committed
feat(html/svelte): key blocks
1 parent 425963d commit 26baa70

File tree

30 files changed

+1171
-67
lines changed

30 files changed

+1171
-67
lines changed

.changeset/light-toys-check.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Added support Svelte syntax `{#key}`. Biome now is able to parse and format the Svelte syntax [`{#key}`](https://svelte.dev/docs/svelte/key):
6+
7+
```diff
8+
-{#key expression} <div></div> {/key}
9+
+{#key expression}
10+
+ <div></div>
11+
+{/key}
12+
```

crates/biome_html_factory/src/generated/node_factory.rs

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/biome_html_factory/src/generated/syntax_factory.rs

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/biome_html_formatter/src/generated.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,120 @@ impl IntoFormat<HtmlFormatContext> for biome_html_syntax::SvelteDebugBlock {
754754
)
755755
}
756756
}
757+
impl FormatRule<biome_html_syntax::SvelteKeyBlock>
758+
for crate::svelte::auxiliary::key_block::FormatSvelteKeyBlock
759+
{
760+
type Context = HtmlFormatContext;
761+
#[inline(always)]
762+
fn fmt(
763+
&self,
764+
node: &biome_html_syntax::SvelteKeyBlock,
765+
f: &mut HtmlFormatter,
766+
) -> FormatResult<()> {
767+
FormatNodeRule::<biome_html_syntax::SvelteKeyBlock>::fmt(self, node, f)
768+
}
769+
}
770+
impl AsFormat<HtmlFormatContext> for biome_html_syntax::SvelteKeyBlock {
771+
type Format<'a> = FormatRefWithRule<
772+
'a,
773+
biome_html_syntax::SvelteKeyBlock,
774+
crate::svelte::auxiliary::key_block::FormatSvelteKeyBlock,
775+
>;
776+
fn format(&self) -> Self::Format<'_> {
777+
FormatRefWithRule::new(
778+
self,
779+
crate::svelte::auxiliary::key_block::FormatSvelteKeyBlock::default(),
780+
)
781+
}
782+
}
783+
impl IntoFormat<HtmlFormatContext> for biome_html_syntax::SvelteKeyBlock {
784+
type Format = FormatOwnedWithRule<
785+
biome_html_syntax::SvelteKeyBlock,
786+
crate::svelte::auxiliary::key_block::FormatSvelteKeyBlock,
787+
>;
788+
fn into_format(self) -> Self::Format {
789+
FormatOwnedWithRule::new(
790+
self,
791+
crate::svelte::auxiliary::key_block::FormatSvelteKeyBlock::default(),
792+
)
793+
}
794+
}
795+
impl FormatRule<biome_html_syntax::SvelteKeyClosingBlock>
796+
for crate::svelte::auxiliary::key_closing_block::FormatSvelteKeyClosingBlock
797+
{
798+
type Context = HtmlFormatContext;
799+
#[inline(always)]
800+
fn fmt(
801+
&self,
802+
node: &biome_html_syntax::SvelteKeyClosingBlock,
803+
f: &mut HtmlFormatter,
804+
) -> FormatResult<()> {
805+
FormatNodeRule::<biome_html_syntax::SvelteKeyClosingBlock>::fmt(self, node, f)
806+
}
807+
}
808+
impl AsFormat<HtmlFormatContext> for biome_html_syntax::SvelteKeyClosingBlock {
809+
type Format<'a> = FormatRefWithRule<
810+
'a,
811+
biome_html_syntax::SvelteKeyClosingBlock,
812+
crate::svelte::auxiliary::key_closing_block::FormatSvelteKeyClosingBlock,
813+
>;
814+
fn format(&self) -> Self::Format<'_> {
815+
FormatRefWithRule::new(
816+
self,
817+
crate::svelte::auxiliary::key_closing_block::FormatSvelteKeyClosingBlock::default(),
818+
)
819+
}
820+
}
821+
impl IntoFormat<HtmlFormatContext> for biome_html_syntax::SvelteKeyClosingBlock {
822+
type Format = FormatOwnedWithRule<
823+
biome_html_syntax::SvelteKeyClosingBlock,
824+
crate::svelte::auxiliary::key_closing_block::FormatSvelteKeyClosingBlock,
825+
>;
826+
fn into_format(self) -> Self::Format {
827+
FormatOwnedWithRule::new(
828+
self,
829+
crate::svelte::auxiliary::key_closing_block::FormatSvelteKeyClosingBlock::default(),
830+
)
831+
}
832+
}
833+
impl FormatRule<biome_html_syntax::SvelteKeyOpeningBlock>
834+
for crate::svelte::auxiliary::key_opening_block::FormatSvelteKeyOpeningBlock
835+
{
836+
type Context = HtmlFormatContext;
837+
#[inline(always)]
838+
fn fmt(
839+
&self,
840+
node: &biome_html_syntax::SvelteKeyOpeningBlock,
841+
f: &mut HtmlFormatter,
842+
) -> FormatResult<()> {
843+
FormatNodeRule::<biome_html_syntax::SvelteKeyOpeningBlock>::fmt(self, node, f)
844+
}
845+
}
846+
impl AsFormat<HtmlFormatContext> for biome_html_syntax::SvelteKeyOpeningBlock {
847+
type Format<'a> = FormatRefWithRule<
848+
'a,
849+
biome_html_syntax::SvelteKeyOpeningBlock,
850+
crate::svelte::auxiliary::key_opening_block::FormatSvelteKeyOpeningBlock,
851+
>;
852+
fn format(&self) -> Self::Format<'_> {
853+
FormatRefWithRule::new(
854+
self,
855+
crate::svelte::auxiliary::key_opening_block::FormatSvelteKeyOpeningBlock::default(),
856+
)
857+
}
858+
}
859+
impl IntoFormat<HtmlFormatContext> for biome_html_syntax::SvelteKeyOpeningBlock {
860+
type Format = FormatOwnedWithRule<
861+
biome_html_syntax::SvelteKeyOpeningBlock,
862+
crate::svelte::auxiliary::key_opening_block::FormatSvelteKeyOpeningBlock,
863+
>;
864+
fn into_format(self) -> Self::Format {
865+
FormatOwnedWithRule::new(
866+
self,
867+
crate::svelte::auxiliary::key_opening_block::FormatSvelteKeyOpeningBlock::default(),
868+
)
869+
}
870+
}
757871
impl FormatRule<biome_html_syntax::SvelteName>
758872
for crate::svelte::auxiliary::name::FormatSvelteName
759873
{

crates/biome_html_formatter/src/html/lists/element_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use tag::GroupMode;
2323
#[derive(Debug, Clone, Default)]
2424
pub(crate) struct FormatHtmlElementList {
2525
layout: HtmlChildListLayout,
26-
/// Whether or not the parent element that encapsulates this element list is whitespace sensitive.
26+
/// Whether the parent element that encapsulates this element list is whitespace sensitive.
2727
is_element_whitespace_sensitive: bool,
2828

2929
borrowed_tokens: BorrowedTokens,

crates/biome_html_formatter/src/svelte/any/block.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ impl FormatRule<AnySvelteBlock> for FormatAnySvelteBlock {
1010
match node {
1111
AnySvelteBlock::SvelteBogusBlock(node) => node.format().fmt(f),
1212
AnySvelteBlock::SvelteDebugBlock(node) => node.format().fmt(f),
13+
AnySvelteBlock::SvelteKeyBlock(node) => node.format().fmt(f),
1314
}
1415
}
1516
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::html::lists::element_list::{FormatChildrenResult, FormatHtmlElementList};
2+
use crate::prelude::*;
3+
use biome_formatter::{format_args, write};
4+
use biome_html_syntax::{SvelteKeyBlock, SvelteKeyBlockFields};
5+
6+
#[derive(Debug, Clone, Default)]
7+
pub(crate) struct FormatSvelteKeyBlock;
8+
impl FormatNodeRule<SvelteKeyBlock> for FormatSvelteKeyBlock {
9+
fn fmt_fields(&self, node: &SvelteKeyBlock, f: &mut HtmlFormatter) -> FormatResult<()> {
10+
let SvelteKeyBlockFields {
11+
opening_block,
12+
children,
13+
closing_block,
14+
} = node.as_fields();
15+
16+
write!(f, [opening_block.format(),])?;
17+
// The order here is important. First, we must check if we can delegate the formatting
18+
// of embedded nodes, then we check if we should format them verbatim.
19+
let format_children = FormatHtmlElementList::default().fmt_children(&children, f)?;
20+
let attr_group_id = f.group_id("element-attr-group-id");
21+
22+
match format_children {
23+
FormatChildrenResult::ForceMultiline(multiline) => {
24+
write!(f, [multiline])?;
25+
}
26+
FormatChildrenResult::BestFitting {
27+
flat_children,
28+
expanded_children,
29+
} => {
30+
let expanded_children = expanded_children.memoized();
31+
write!(
32+
f,
33+
[
34+
// If the attribute group breaks, prettier always breaks the children as well.
35+
&if_group_breaks(&expanded_children).with_group_id(Some(attr_group_id)),
36+
// If the attribute group does NOT break, print whatever fits best for the children.
37+
&if_group_fits_on_line(&best_fitting![
38+
format_args![flat_children],
39+
format_args![expanded_children],
40+
])
41+
.with_group_id(Some(attr_group_id)),
42+
]
43+
)?;
44+
}
45+
}
46+
47+
write!(f, [closing_block.format()])
48+
}
49+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::prelude::*;
2+
use biome_formatter::write;
3+
use biome_html_syntax::{SvelteKeyClosingBlock, SvelteKeyClosingBlockFields};
4+
5+
#[derive(Debug, Clone, Default)]
6+
pub(crate) struct FormatSvelteKeyClosingBlock;
7+
impl FormatNodeRule<SvelteKeyClosingBlock> for FormatSvelteKeyClosingBlock {
8+
fn fmt_fields(&self, node: &SvelteKeyClosingBlock, f: &mut HtmlFormatter) -> FormatResult<()> {
9+
let SvelteKeyClosingBlockFields {
10+
key_token,
11+
r_curly_token,
12+
sv_curly_slash_token,
13+
} = node.as_fields();
14+
15+
write!(
16+
f,
17+
[
18+
sv_curly_slash_token.format(),
19+
key_token.format(),
20+
r_curly_token.format(),
21+
]
22+
)
23+
}
24+
}

0 commit comments

Comments
 (0)