Skip to content

Commit

Permalink
fix: format Svelte snippet block as function decl (fix #88)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Dec 2, 2024
1 parent d4366c4 commit e1b7a20
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
3 changes: 3 additions & 0 deletions dprint_plugin/tests/integration/basic.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ function generateHtml(data:unknown) {
{#key 1+2}s{/key}

<button class="{}" on:click={}>{}{/**/}</button>

{#snippet foo(param: string)}
{/snippet}
3 changes: 3 additions & 0 deletions dprint_plugin/tests/integration/biome/basic.svelte.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ function generateHtml(data: unknown) {
{#key 1 + 2}s{/key}

<button class={} on:click={}>{}{/**/}</button>

{#snippet foo(param: string)}
{/snippet}
3 changes: 3 additions & 0 deletions dprint_plugin/tests/integration/dprint_ts/basic.svelte.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ function generateHtml(data: unknown) {
{#key 1 + 2}s{/key}

<button class={} on:click={}>{}{/**/}</button>

{#snippet foo(param: string)}
{/snippet}
2 changes: 1 addition & 1 deletion markup_fmt/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ pub struct SvelteThenBlock<'s> {
}

pub struct SvelteSnippetBlock<'s> {
pub expr: (&'s str, usize),
pub signature: (&'s str, usize),
pub children: Vec<Node<'s>>,
}

Expand Down
7 changes: 5 additions & 2 deletions markup_fmt/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2168,7 +2168,7 @@ impl<'s> Parser<'s> {
return Err(self.emit_error(SyntaxErrorKind::ExpectSvelteSnippetBlock));
};

let expr = self.parse_svelte_or_astro_expr()?;
let signature = self.parse_svelte_or_astro_expr()?;
let children = self.parse_svelte_block_children()?;

if self
Expand All @@ -2187,7 +2187,10 @@ impl<'s> Parser<'s> {
.and_then(|_| self.chars.next_if(|(_, c)| *c == '}'))
.is_some()
{
Ok(SvelteSnippetBlock { expr, children })
Ok(SvelteSnippetBlock {
signature,
children,
})
} else {
Err(self.emit_error(SyntaxErrorKind::ExpectSvelteBlockEnd))
}
Expand Down
18 changes: 12 additions & 6 deletions markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,13 +1415,19 @@ impl<'s> DocGen<'s> for SvelteSnippetBlock<'s> {
where
F: for<'a> FnMut(&'a str, Hints) -> Result<Cow<'a, str>, E>,
{
let wrapped = format!("function {}{{}}", self.signature.0);
let formatted = ctx.format_script(&wrapped, "ts", self.signature.1, state);
Doc::text("{#snippet ")
.append(Doc::text(ctx.format_expr(
self.expr.0,
false,
self.expr.1,
state,
)))
.append(Doc::text(
formatted
.trim()
.strip_prefix("function ")
.and_then(|s| s.trim_end().strip_suffix('}'))
.and_then(|s| s.trim_end().strip_suffix('{'))
.map(|s| s.trim())
.unwrap_or(&formatted)
.to_owned(),
))
.append(Doc::text("}"))
.append(format_control_structure_block_children(
&self.children,
Expand Down

0 comments on commit e1b7a20

Please sign in to comment.