Skip to content

fix: Still failing bind:group for nested data #10379

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ const common_visitors = {
// If the same identifiers in the same order are used in another bind:group, they will be in the same group.
// (there's an edge case where `bind:group={a[i]}` will be in a different group than `bind:group={a[j]}` even when i == j,
// but this is a limitation of the current static analysis we do; it also never worked in Svelte 4)
const bindings = expression_ids.map((id) => context.state.scope.get(id.name));
const bindings = expression_ids.map((id) => context.state.scope.get(id.name) ?? id);
let group_name;
outer: for (const [b, group] of context.state.analysis.binding_groups) {
if (b.length !== bindings.length) continue;
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface ComponentAnalysis extends Analysis {
inject_styles: boolean;
reactive_statements: Map<LabeledStatement, ReactiveStatement>;
/** Identifiers that make up the `bind:group` expression -> internal group binding name */
binding_groups: Map<Array<Binding | null>, Identifier>;
binding_groups: Map<Array<Binding | Identifier | null>, Identifier>;
slot_names: Set<string>;
}

Expand Down
7 changes: 2 additions & 5 deletions packages/svelte/src/compiler/utils/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@ export function extract_all_identifiers_from_expression(expr) {
expr,
{},
{
Identifier(node, { path }) {
const parent = path.at(-1);
if (parent?.type !== 'MemberExpression' || parent.property !== node || parent.computed) {
nodes.push(node);
}
Identifier(node) {
nodes.push(node);
Copy link
Member

Choose a reason for hiding this comment

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

This will fail other parts of the code. And this would probably also yield a false-positive match for bind:group={group.a} and bind:group={group[a]}. We need to find a different way to compare these, possibly by exact string match. @tanhauhau how did this work exactly in Svelte 4?

Copy link
Author

Choose a reason for hiding this comment

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

Just want to point out another possibly tricky case, in packages\svelte\tests\runtime-legacy\samples\binding-input-group-each-8\main.svelte, where the group binding is using the key of a higher each loop, not the one it's in:

{#each keys as key (key)}
  <h2>{key}</h2>
  <ul>
  {#each values as value (value)}
    <li>
      <label>
        <input type="checkbox" name={key} {value} bind:group={object[key]} /> {value}
      </label>
    </li>
  {/each}
  </ul>
{/each}

Copy link
Author

Choose a reason for hiding this comment

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

My spontaneous thought is to use the bind:group expression itself as a key to the binding group object. What would be the problem with that?

}
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { test } from '../../test';

export default test({
async test({ assert, target }) {
const checkboxes = /** @type {NodeListOf<HTMLInputElement>} */ (
target.querySelectorAll('input[type="checkbox"]')
);

assert.isFalse(checkboxes[0].checked);
assert.isTrue(checkboxes[1].checked);
assert.isFalse(checkboxes[2].checked);

await checkboxes[1].click();

const noChecked = target.querySelector('#output')?.innerHTML;
assert.equal(noChecked, '');

await checkboxes[1].click();

const oneChecked = target.querySelector('#output')?.innerHTML;
assert.equal(oneChecked, 'Mint choc chip');
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script lang="ts">
import { writable } from 'svelte/store';
let menu = ['Cookies and cream', 'Mint choc chip', 'Raspberry ripple'];
let order = writable({flavours: ['Mint choc chip'], scoops: 1 });
</script>

<form method="POST">
<input type="radio" bind:group={$order.scoops} name="scoops" value={1} /> One scoop
<input type="radio" bind:group={$order.scoops} name="scoops" value={2} /> Two scoops
<input type="radio" bind:group={$order.scoops} name="scoops" value={3} /> Three scoops

{#each menu as flavour}
<input type="checkbox" bind:group={$order.flavours} name="flavours" value={flavour} /> {flavour}
{/each}
</form>

<div id="output">{$order.flavours.join('+')}</div>