From 0370c0f8ff8dfdb67d6e941c6b22ab537b8979b8 Mon Sep 17 00:00:00 2001 From: Skyler Lipthay Date: Sun, 3 Nov 2024 00:19:07 -0700 Subject: [PATCH] fix: Fix support for Svelte 5 snippet blocks --- markup_fmt/src/parser.rs | 23 +++++++++++++++++++++-- markup_fmt/src/printer.rs | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/markup_fmt/src/parser.rs b/markup_fmt/src/parser.rs index e41b747..3b2c9cd 100644 --- a/markup_fmt/src/parser.rs +++ b/markup_fmt/src/parser.rs @@ -2174,13 +2174,32 @@ impl<'s> Parser<'s> { .and_then(|_| self.chars.next_if(|(_, c)| c.is_ascii_whitespace())) .is_none() { - return Err(self.emit_error(SyntaxErrorKind::ExpectSvelteIfBlock)); + return Err(self.emit_error(SyntaxErrorKind::ExpectSvelteSnippetBlock)); }; let expr = self.parse_svelte_or_astro_expr()?; let children = self.parse_svelte_block_children()?; - Ok(SvelteSnippetBlock { expr, children }) + if self + .chars + .next_if(|(_, c)| *c == '{') + .map(|_| self.skip_ws()) + .and_then(|_| self.chars.next_if(|(_, c)| *c == '/')) + .and_then(|_| self.chars.next_if(|(_, c)| *c == 's')) + .and_then(|_| self.chars.next_if(|(_, c)| *c == 'n')) + .and_then(|_| self.chars.next_if(|(_, c)| *c == 'i')) + .and_then(|_| self.chars.next_if(|(_, c)| *c == 'p')) + .and_then(|_| self.chars.next_if(|(_, c)| *c == 'p')) + .and_then(|_| self.chars.next_if(|(_, c)| *c == 'e')) + .and_then(|_| self.chars.next_if(|(_, c)| *c == 't')) + .map(|_| self.skip_ws()) + .and_then(|_| self.chars.next_if(|(_, c)| *c == '}')) + .is_some() + { + Ok(SvelteSnippetBlock { expr, children }) + } else { + Err(self.emit_error(SyntaxErrorKind::ExpectSvelteBlockEnd)) + } } fn parse_tag_name(&mut self) -> PResult<&'s str> { diff --git a/markup_fmt/src/printer.rs b/markup_fmt/src/printer.rs index 0a8d1dd..a03ca80 100644 --- a/markup_fmt/src/printer.rs +++ b/markup_fmt/src/printer.rs @@ -1319,6 +1319,7 @@ impl<'s> DocGen<'s> for SvelteSnippetBlock<'s> { ctx, state, )) + .append(Doc::text("{/snippet}")) } }