diff --git a/.changeset/rich-zoos-walk.md b/.changeset/rich-zoos-walk.md new file mode 100644 index 000000000000..9af5fd580e6f --- /dev/null +++ b/.changeset/rich-zoos-walk.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: multiline html blocks now properlery seperated from other multiline blocks while `print()`-ing an AST 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/src/compiler/print/index.js b/packages/svelte/src/compiler/print/index.js index abd8b562977e..dac1d1975945 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(); } } @@ -395,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; 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..ba871c66a585 100644 --- a/packages/svelte/tests/print/samples/formatting/output.svelte +++ b/packages/svelte/tests/print/samples/formatting/output.svelte @@ -4,18 +4,46 @@

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

+
+

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..27c12c5d47a9 100644 --- a/packages/svelte/tests/print/samples/style-directive/output.svelte +++ b/packages/svelte/tests/print/samples/style-directive/output.svelte @@ -1,8 +1,7 @@
...
+
- ... -
+>... diff --git a/packages/svelte/tests/print/samples/svelte-fragment/output.svelte b/packages/svelte/tests/print/samples/svelte-fragment/output.svelte index eb80023626a6..73a4451ed821 100644 --- a/packages/svelte/tests/print/samples/svelte-fragment/output.svelte +++ b/packages/svelte/tests/print/samples/svelte-fragment/output.svelte @@ -4,6 +4,7 @@

Hello

+

All rights reserved.

Copyright (c) 2019 Svelte Industries

diff --git a/packages/svelte/tests/print/samples/svelte-head/output.svelte b/packages/svelte/tests/print/samples/svelte-head/output.svelte index 68d352260ef4..da4345d731ef 100644 --- a/packages/svelte/tests/print/samples/svelte-head/output.svelte +++ b/packages/svelte/tests/print/samples/svelte-head/output.svelte @@ -1,5 +1,6 @@ Hello world! + =0.10'} - esrap@2.2.1: - resolution: {integrity: sha512-GiYWG34AN/4CUyaWAgunGt0Rxvr1PTMlGC0vvEov/uOQYWne2bpN03Um+k8jT+q3op33mKouP2zeJ6OlM+qeUg==} + esrap@https://pkg.pr.new/sveltejs/esrap@101: + resolution: {tarball: https://pkg.pr.new/sveltejs/esrap@101} + version: 2.2.1 esrecurse@4.3.0: resolution: {integrity: sha512-KmfKL3b6G+RXvP8N1vr3Tq1kL/oCFgn2NYXEtqP8/L3pKapUA4G8cFVaoF3SU323CD4XypR/ffioHmkti6/Tag==} @@ -3720,7 +3721,7 @@ snapshots: dependencies: estraverse: 5.3.0 - esrap@2.2.1: + esrap@https://pkg.pr.new/sveltejs/esrap@101: dependencies: '@jridgewell/sourcemap-codec': 1.5.0