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