Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-zoos-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: multiline html blocks now properlery seperated from other multiline blocks while `print()`-ing an AST
2 changes: 1 addition & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
64 changes: 14 additions & 50 deletions packages/svelte/src/compiler/print/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`</${node.name}>`);
}

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<AST.SvelteNode>} */
Expand Down Expand Up @@ -387,6 +347,8 @@ const svelte_visitors = {
}
} else {
sequence.push(child_node);

if (child_node.type === 'RegularElement') flush();
}
}

Expand All @@ -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;

Expand Down
9 changes: 8 additions & 1 deletion packages/svelte/tests/print/samples/formatting/input.svelte
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
<script>import { setLocale } from '$lib/paraglide/runtime';import { m } from '$lib/paraglide/messages.js';</script><h1>{m.hello_world({ name: 'SvelteKit User' })}</h1><div><button onclick={() => setLocale('en')}>en</button><button onclick={() => setLocale('es')}>es</button></div><p>If you use VSCode, install the <a href="https://marketplace.visualstudio.com/items?itemName=inlang.vs-code-extension" target="_blank">Sherlock i18n extension</a>for a better i18n experience.</p>
<script>import { setLocale } from '$lib/paraglide/runtime';import { m } from '$lib/paraglide/messages.js';</script>

<h1>{m.hello_world({ name: 'SvelteKit User' })}</h1><div><button onclick={() => setLocale('en')}>en</button><button onclick={() => setLocale('es')}>es</button></div><p>If you use VSCode, install the <a href="https://marketplace.visualstudio.com/items?itemName=inlang.vs-code-extension" target="_blank">Sherlock i18n extension</a>for a better i18n experience.</p>

<Component><div><button>Hello, this is a test</button><button>Hello, this is a test</button></div></Component>
<Component><button>Hello, this is a test</button><button>Hello, this is a test</button></Component>
<button class="foo bar" aria-label="click" onclick={() => console.log("clicked")}>Click me!</button>
<button class="foo bar" aria-label="click" onclick={() => console.log("clicked")}><span>some fancy looking</span><span>really long button text</span></button>
34 changes: 31 additions & 3 deletions packages/svelte/tests/print/samples/formatting/output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,46 @@
</script>

<h1>{m.hello_world({ name: 'SvelteKit User' })}</h1>

<div>
<button onclick={() => setLocale('en')}>en</button>
<button onclick={() => setLocale('es')}>es</button>
</div>

<p>
If you use VSCode, install the

<a
href="https://marketplace.visualstudio.com/items?itemName=inlang.vs-code-extension"
target="_blank"
>
Sherlock i18n extension
</a>
>Sherlock i18n extension</a>

for a better i18n experience.
</p>

<Component>
<div>
<button>Hello, this is a test</button>
<button>Hello, this is a test</button>
</div>
</Component>

<Component>
<button>Hello, this is a test</button>
<button>Hello, this is a test</button>
</Component>

<button
class="foo bar"
aria-label="click"
onclick={() => console.log("clicked")}
>Click me!</button>

<button
class="foo bar"
aria-label="click"
onclick={() => console.log("clicked")}
>
<span>some fancy looking</span>
<span>really long button text</span>
</button>
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<div style:color="red">...</div>

<div
style:color
style:width="12rem"
style:background-color={darkMode ? 'black' : 'white'}
>
...
</div>
>...</div>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<Widget>
<h1 slot="header">Hello</h1>

<svelte:fragment slot="footer">
<p>All rights reserved.</p>
<p>Copyright (c) 2019 Svelte Industries</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<svelte:head>
<title>Hello world!</title>

<meta
name="description"
content="This is where the description goes for SEO"
Expand Down
11 changes: 6 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading