From 103dc574c709bed7d63448a14e134789d70fc1b4 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Sat, 6 Dec 2025 20:15:27 +0100 Subject: [PATCH 1/5] way cleaner new line handling for base_elements --- packages/svelte/src/compiler/print/index.js | 44 ++----------------- .../print/samples/formatting/input.svelte | 9 +++- .../print/samples/formatting/output.svelte | 28 ++++++++++-- .../samples/style-directive/output.svelte | 4 +- 4 files changed, 36 insertions(+), 49 deletions(-) diff --git a/packages/svelte/src/compiler/print/index.js b/packages/svelte/src/compiler/print/index.js index abd8b562977e..5bfbadc05fc7 100644 --- a/packages/svelte/src/compiler/print/index.js +++ b/packages/svelte/src/compiler/print/index.js @@ -115,56 +115,16 @@ function base_element(node, context) { const is_self_closing = is_void(node.name) || (node.type === 'Component' && node.fragment.nodes.length === 0); - let multiline_content = false; if (is_self_closing) { child_context.write(`${multiline_attributes ? '' : ' '}/>`); } else { child_context.write('>'); - - // Process the element's content in a separate context for measurement - const content_context = child_context.new(); - const allow_inline_content = child_context.measure() < LINE_BREAK_THRESHOLD; - block(content_context, node.fragment, allow_inline_content); - - // Determine if content should be formatted on multiple lines - multiline_content = content_context.measure() > LINE_BREAK_THRESHOLD; - - if (multiline_content) { - child_context.newline(); - - // Only indent if attributes are inline and content itself isn't already multiline - const should_indent = !multiline_attributes && !content_context.multiline; - if (should_indent) { - child_context.indent(); - } - - child_context.append(content_context); - - if (should_indent) { - child_context.dedent(); - } - - child_context.newline(); - } else { - child_context.append(content_context); - } - + block(child_context, node.fragment, true); child_context.write(``); } - const break_line_after = child_context.measure() > LINE_BREAK_THRESHOLD; - - if ((multiline_content || multiline_attributes) && !context.empty()) { - context.newline(); - } - context.append(child_context); - - if (is_self_closing) return; - if (multiline_content || multiline_attributes || break_line_after) { - context.newline(); - } } /** @type {Visitors} */ @@ -387,6 +347,8 @@ const svelte_visitors = { } } else { sequence.push(child_node); + + if (child_node.type === 'RegularElement') flush(); } } diff --git a/packages/svelte/tests/print/samples/formatting/input.svelte b/packages/svelte/tests/print/samples/formatting/input.svelte index 9b1898e9c82d..43c3b363cb1d 100644 --- a/packages/svelte/tests/print/samples/formatting/input.svelte +++ b/packages/svelte/tests/print/samples/formatting/input.svelte @@ -1 +1,8 @@ -

{m.hello_world({ name: 'SvelteKit User' })}

If you use VSCode, install the Sherlock i18n extensionfor a better i18n experience.

+ + +

{m.hello_world({ name: 'SvelteKit User' })}

If you use VSCode, install the Sherlock i18n extensionfor a better i18n experience.

+ +
+ + + diff --git a/packages/svelte/tests/print/samples/formatting/output.svelte b/packages/svelte/tests/print/samples/formatting/output.svelte index 7b4064258086..18ab9de9ad84 100644 --- a/packages/svelte/tests/print/samples/formatting/output.svelte +++ b/packages/svelte/tests/print/samples/formatting/output.svelte @@ -10,12 +10,32 @@

If you use VSCode, install the - - Sherlock i18n extension - + >Sherlock i18n extension for a better i18n experience.

+ +
+ + +
+
+ + + + + + diff --git a/packages/svelte/tests/print/samples/style-directive/output.svelte b/packages/svelte/tests/print/samples/style-directive/output.svelte index 5aa3a6dcdb96..5998f5371d75 100644 --- a/packages/svelte/tests/print/samples/style-directive/output.svelte +++ b/packages/svelte/tests/print/samples/style-directive/output.svelte @@ -3,6 +3,4 @@ style:color style:width="12rem" style:background-color={darkMode ? 'black' : 'white'} -> - ... - +>... From 980c2bb0bb8447286a89bd37316a78fa1458c381 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Sat, 6 Dec 2025 20:21:23 +0100 Subject: [PATCH 2/5] fix --- packages/svelte/src/compiler/print/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/svelte/src/compiler/print/index.js b/packages/svelte/src/compiler/print/index.js index 5bfbadc05fc7..942e91e4bae4 100644 --- a/packages/svelte/src/compiler/print/index.js +++ b/packages/svelte/src/compiler/print/index.js @@ -348,7 +348,7 @@ const svelte_visitors = { } else { sequence.push(child_node); - if (child_node.type === 'RegularElement') flush(); + if (next && child_node.type === 'RegularElement') flush(); } } From f2efe63f26d72c338491588a1a11950508ff95cb Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Sat, 6 Dec 2025 20:54:23 +0100 Subject: [PATCH 3/5] more fixes --- packages/svelte/src/compiler/print/index.js | 22 +++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/svelte/src/compiler/print/index.js b/packages/svelte/src/compiler/print/index.js index 942e91e4bae4..dac1d1975945 100644 --- a/packages/svelte/src/compiler/print/index.js +++ b/packages/svelte/src/compiler/print/index.js @@ -348,7 +348,7 @@ const svelte_visitors = { } else { sequence.push(child_node); - if (next && child_node.type === 'RegularElement') flush(); + if (child_node.type === 'RegularElement') flush(); } } @@ -357,18 +357,20 @@ const svelte_visitors = { let multiline = false; let width = 0; - const child_contexts = items.map((sequence) => { - const child_context = context.new(); + const child_contexts = items + .filter((x) => x.length > 0) + .map((sequence) => { + const child_context = context.new(); - for (const node of sequence) { - child_context.visit(node); - multiline ||= child_context.multiline; - } + for (const node of sequence) { + child_context.visit(node); + multiline ||= child_context.multiline; + } - width += child_context.measure(); + width += child_context.measure(); - return child_context; - }); + return child_context; + }); multiline ||= width > LINE_BREAK_THRESHOLD; From 41d8f5fc394452e1e580177cb3292ed8ee8daedb Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Sat, 6 Dec 2025 21:04:24 +0100 Subject: [PATCH 4/5] use esrap preview --- packages/svelte/package.json | 2 +- .../tests/print/samples/formatting/output.svelte | 8 ++++++++ .../tests/print/samples/style-directive/output.svelte | 1 + .../tests/print/samples/svelte-fragment/output.svelte | 1 + .../tests/print/samples/svelte-head/output.svelte | 1 + pnpm-lock.yaml | 11 ++++++----- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/svelte/package.json b/packages/svelte/package.json index a8f085dbd474..bc23e7e9fa85 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -176,7 +176,7 @@ "clsx": "^2.1.1", "devalue": "^5.5.0", "esm-env": "^1.2.1", - "esrap": "^2.2.1", + "esrap": "https://pkg.pr.new/sveltejs/esrap@101", "is-reference": "^3.0.3", "locate-character": "^3.0.0", "magic-string": "^0.30.11", diff --git a/packages/svelte/tests/print/samples/formatting/output.svelte b/packages/svelte/tests/print/samples/formatting/output.svelte index 18ab9de9ad84..ba871c66a585 100644 --- a/packages/svelte/tests/print/samples/formatting/output.svelte +++ b/packages/svelte/tests/print/samples/formatting/output.svelte @@ -4,33 +4,41 @@

{m.hello_world({ name: 'SvelteKit User' })}

+
+

If you use VSCode, install the + Sherlock i18n extension + for a better i18n experience.

+
+ + +