Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't hoist snippets with bind:group #15040

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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/lazy-carrots-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: don't hoist snippets with `bind:group`
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ function open(parser) {
parameters: function_expression.params,
body: create_fragment(),
metadata: {
can_hoist: false,
can_hoist: undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't feel right, it should always be a boolean or if there's another state it should be an enum.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make it an enum but it doesn't change the sense: undefined it's basically "don't know yet" because if the first visitor that sets can_hoist is the bind:group one we don't want to override it within the snippet. In the end it will always be set by the snippet visitor and the fact that it's a falsy value allow us to default it to false without having to discriminate further.

sites: new Set()
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ export function BindDirective(node, context) {
throw new Error('Cannot find declaration for bind:group');
}

const snippet_parent = context.path.find((parent) => parent.type === 'SnippetBlock');

if (snippet_parent) {
snippet_parent.metadata.can_hoist = false;
}

// Traverse the path upwards and find all EachBlocks who are (indirectly) contributing to bind:group,
// i.e. one of their declarations is referenced in the binding. This allows group bindings to work
// correctly when referencing a variable declared in an EachBlock by using the index of the each block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export function SnippetBlock(node, context) {
}
}

node.metadata.can_hoist = can_hoist;
node.metadata.can_hoist =
node.metadata.can_hoist != null ? node.metadata.can_hoist && can_hoist : can_hoist;

const { path } = context;
const parent = path.at(-2);
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/types/template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export namespace AST {
body: Fragment;
/** @internal */
metadata: {
can_hoist: boolean;
can_hoist?: boolean;
/** The set of components/render tags that could render this snippet,
* used for CSS pruning */
sites: Set<Component | SvelteComponent | SvelteSelf | RenderTag>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target }) {
const [radio1, radio2] = target.querySelectorAll('input');
const p = target.querySelector('p');

assert.equal(p?.innerHTML, '');
flushSync(() => {
radio1.click();
});
assert.equal(p?.innerHTML, 'cool');
flushSync(() => {
radio2.click();
});
assert.equal(p?.innerHTML, 'cooler');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
let group = $state({ selected: undefined });
</script>

{#snippet radio(group, value)}
<input type="radio" bind:group={group.selected} {value}/>
{/snippet}

{@render radio(group, "cool")}
{@render radio(group, "cooler")}

<p>{group.selected}</p>
Loading