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

Conversation

paoloricciuti
Copy link
Member

Closes #15037

This is a bit of weird one...the point is that the snippet was technically not accessing anything from the instance so it was hoisted but it was using bind:group so it needed the binding_group...i tried to think it it make sense to export a snippet with a bind group but i don't think it does (especially since we specify in the docs that for bind:group to work inputs needs to be in the same component) and i don't even think we could do it because even if we would accept the binding_group as an argument for the snippet a component that renders that snippet should know that it needs a binding_group in some way and i don't think it's doable.

However while trying to come up with various situations where the snippet could've been exported i noticed that we can technically "kinda" export snippets by passing them as prop. So a user could do something like this

App.svelte

<script>
    import Component from "./Component.svelte";
</script>

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

<Component {radio} />
<Component {radio} />

Component.svelte

<script>
    let { radio } = $props();
    let group = $state({ selected: undefined });
</script>

{@render radio(group, "A")}
{@render radio(group, "B")}

this is a bit weird because now App has the binding_group array but the state is inside Component. So i was wondering...should we disallow bind:group in snippets all togheter? Technically we can't do it now since it would be a breaking change but i wonder if we should do something about this weirdness.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Sorry, something went wrong.

Copy link

changeset-bot bot commented Jan 16, 2025

🦋 Changeset detected

Latest commit: f380b34

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-svelte-15040-svelte.vercel.app/

this is an automated message

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@15040

@@ -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.

@dummdidumm
Copy link
Member

dummdidumm commented Jan 21, 2025

Yeah we need to disallow bind:group referencing something from the snippet parameters. bind:group is this weird static heuristics thing to build up the distinct groups. Here's a playground on this branch where it bleeds across snippets

I'm not sure if it is a breaking change, is there any situation where it did work correctly before?

@paoloricciuti
Copy link
Member Author

I'm not sure if it is a breaking change, is there any situation where it did work correctly before?

I don't think it's a breaking because it was an error before so nobody could've used this syntax right? I think we should ban the usage too.

@paoloricciuti
Copy link
Member Author

Oh no wait, it's not a breaking for exporting but it is for passing props...but i would say if it's broken we should fix it regardless. I don't think many people were using this anyway...maybe we can add a special changeset to this?

@dummdidumm
Copy link
Member

Not sure I follow what you mean by the last two posts. It's not breaking if bind:group references only variables that are not snippet pararameters. If it does, then it probably did not work anyway, so we can ban usage. (at least that's what I'm thinking)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

binding_group is not defined
4 participants