From 86d55220960f78a7f057439346ec294c38f6b35c Mon Sep 17 00:00:00 2001 From: "jyc.dev" Date: Fri, 13 Feb 2026 18:03:47 +0100 Subject: [PATCH] fix: follow-up formatting in `print()` for block-level elements (#17699) Follow-up to #17319. The `Fragment` visitor in `print()` only flushed sequences on `RegularElement`, causing block-level elements (`Component`, `SvelteHead`, `SvelteBoundary`, etc.) to be lumped into the same sequence as adjacent nodes. This broke tools that programmatically manipulate the AST (e.g. [sveltejs/cli#915](https://github.com/sveltejs/cli/pull/915)). The fix flushes before and after all block-level element types, ensuring they get their own sequence and proper line separation. ### Before ```svelte Page Title
no space
child
after component
boundary content
after boundary
after comment
before comment
{#each items as item}
{item}
{/each}
after each
{@render children()}
after render
before render
{@render children()} ``` ### After ```svelte Page Title
no space
child
after component
boundary content
after boundary
after comment
before comment
{#each items as item}
{item}
{/each}
after each
{@render children()}
after render
before render
{@render children()} ``` --- .changeset/print-block-element-separation.md | 5 +++ packages/svelte/src/compiler/print/index.js | 18 +++++++- .../block-element-separation/input.svelte | 25 +++++++++++ .../block-element-separation/output.svelte | 42 +++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 .changeset/print-block-element-separation.md create mode 100644 packages/svelte/tests/print/samples/block-element-separation/input.svelte create mode 100644 packages/svelte/tests/print/samples/block-element-separation/output.svelte diff --git a/.changeset/print-block-element-separation.md b/.changeset/print-block-element-separation.md new file mode 100644 index 0000000000..57373fba5b --- /dev/null +++ b/.changeset/print-block-element-separation.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: follow-up formatting in `print()` — flush block-level elements into separate sequences diff --git a/packages/svelte/src/compiler/print/index.js b/packages/svelte/src/compiler/print/index.js index 094c578b83..5543b6b581 100644 --- a/packages/svelte/src/compiler/print/index.js +++ b/packages/svelte/src/compiler/print/index.js @@ -371,9 +371,23 @@ const svelte_visitors = { } } } else { + const is_block_element = + child_node.type === 'RegularElement' || + child_node.type === 'Component' || + child_node.type === 'SvelteHead' || + child_node.type === 'SvelteFragment' || + child_node.type === 'SvelteBoundary' || + child_node.type === 'SvelteDocument' || + child_node.type === 'SvelteSelf' || + child_node.type === 'SvelteWindow' || + child_node.type === 'SvelteComponent' || + child_node.type === 'SvelteElement' || + child_node.type === 'SlotElement' || + child_node.type === 'TitleElement'; + + if (is_block_element && sequence.length > 0) flush(); sequence.push(child_node); - - if (child_node.type === 'RegularElement') flush(); + if (is_block_element) flush(); } } diff --git a/packages/svelte/tests/print/samples/block-element-separation/input.svelte b/packages/svelte/tests/print/samples/block-element-separation/input.svelte new file mode 100644 index 0000000000..2a721648fe --- /dev/null +++ b/packages/svelte/tests/print/samples/block-element-separation/input.svelte @@ -0,0 +1,25 @@ +Page Title
no space
+ +child
after component
+
boundary content
after boundary
+
after comment
+
before comment
+{#each items as item}
{item}
{/each}
after each
+{@render children()}
after render
+
before render
{@render children()} + +
with spaces
+child
spaces after component
+
spaces after comment
+{@render children()}
spaces after render
+ + +
newline between
+ +
newline after comment
+{#each items as item} +
{item}
+{/each} +
newline after each
+{@render children()} +
after render
diff --git a/packages/svelte/tests/print/samples/block-element-separation/output.svelte b/packages/svelte/tests/print/samples/block-element-separation/output.svelte new file mode 100644 index 0000000000..380e4f7cee --- /dev/null +++ b/packages/svelte/tests/print/samples/block-element-separation/output.svelte @@ -0,0 +1,42 @@ +Page Title +
no space
+ + +child +
after component
+
boundary content
+
after boundary
+ +
after comment
+
before comment
+ + +{#each items as item} +
{item}
+{/each} + +
after each
+{@render children()} +
after render
+
before render
+{@render children()} + +
with spaces
+child +
spaces after component
+ +
spaces after comment
+{@render children()} +
spaces after render
+ +
newline between
+ +
newline after comment
+ +{#each items as item} +
{item}
+{/each} + +
newline after each
+{@render children()} +
after render