Skip to content

Commit 6b6f915

Browse files
authored
fix: omit $$index parameter where possible (#12851)
1 parent 90334c8 commit 6b6f915

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

.changeset/lucky-experts-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: omit `$$index` parameter where possible

packages/svelte/src/compiler/phases/3-transform/client/visitors/EachBlock.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,23 @@ export function EachBlock(node, context) {
149149
each_node_meta.contains_group_binding || !node.index ? each_node_meta.index : b.id(node.index);
150150
const item = each_node_meta.item;
151151

152+
let uses_index = each_node_meta.contains_group_binding;
153+
let key_uses_index = false;
154+
152155
if (node.index) {
153-
if ((flags & EACH_INDEX_REACTIVE) !== 0) {
154-
child_state.transform[node.index] = { read: get_value };
155-
} else {
156-
delete child_state.transform[node.index];
157-
}
156+
child_state.transform[node.index] = {
157+
read: (node) => {
158+
uses_index = true;
159+
return (flags & EACH_INDEX_REACTIVE) !== 0 ? get_value(node) : node;
160+
}
161+
};
158162

159-
delete key_state.transform[node.index];
163+
key_state.transform[node.index] = {
164+
read: (node) => {
165+
key_uses_index = true;
166+
return node;
167+
}
168+
};
160169
}
161170

162171
/** @type {Statement[]} */
@@ -180,6 +189,8 @@ export function EachBlock(node, context) {
180189
child_state.transform[node.context.name] = {
181190
read: (flags & EACH_ITEM_REACTIVE) !== 0 ? get_value : (node) => node,
182191
assign: (_, value) => {
192+
uses_index = true;
193+
183194
const left = b.member(
184195
each_node_meta.array_name ? b.call(each_node_meta.array_name) : collection,
185196
index,
@@ -238,7 +249,7 @@ export function EachBlock(node, context) {
238249
context.visit(/** @type {Expression} */ (node.key), key_state)
239250
);
240251

241-
key_function = b.arrow([node.context, index], expression);
252+
key_function = b.arrow(key_uses_index ? [node.context, index] : [node.context], expression);
242253
}
243254

244255
if (node.index && each_node_meta.contains_group_binding) {
@@ -259,7 +270,10 @@ export function EachBlock(node, context) {
259270
b.literal(flags),
260271
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
261272
key_function,
262-
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(block.body)))
273+
b.arrow(
274+
uses_index ? [b.id('$$anchor'), item, index] : [b.id('$$anchor'), item],
275+
b.block(declarations.concat(block.body))
276+
)
263277
];
264278

265279
if (node.fallback) {

packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function Each_string_template($$anchor) {
55
var fragment = $.comment();
66
var node = $.first_child(fragment);
77

8-
$.each(node, 0, () => ['foo', 'bar', 'baz'], $.index, ($$anchor, thing, $$index) => {
8+
$.each(node, 0, () => ['foo', 'bar', 'baz'], $.index, ($$anchor, thing) => {
99
var text = $.text();
1010

1111
$.template_effect(() => $.set_text(text, `${thing ?? ""}, `));

0 commit comments

Comments
 (0)