Skip to content

Commit d8212f4

Browse files
committed
remove $.unwrap from key functions
1 parent 3bff87a commit d8212f4

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ export function serialize_get_binding(node, state) {
7979
return node;
8080
}
8181

82+
if (Object.hasOwn(state.getters, node.name)) {
83+
const getter = state.getters[node.name];
84+
return typeof getter === 'function' ? getter(node) : getter;
85+
}
86+
8287
if (binding.node.name === '$$props') {
8388
// Special case for $$props which only exists in the old world
8489
return b.id('$$sanitized_props');
@@ -88,11 +93,6 @@ export function serialize_get_binding(node, state) {
8893
return b.call(node);
8994
}
9095

91-
if (Object.hasOwn(state.getters, node.name)) {
92-
const getter = state.getters[node.name];
93-
return typeof getter === 'function' ? getter(node) : getter;
94-
}
95-
9696
if (binding.kind === 'prop' || binding.kind === 'bindable_prop') {
9797
if (is_prop_source(binding, state)) {
9898
return b.call(node);

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,12 @@ export const template_visitors = {
24932493
getters: { ...context.state.getters }
24942494
};
24952495

2496+
/** The state used when generating the key function, if necessary */
2497+
const key_state = {
2498+
...context.state,
2499+
getters: { ...context.state.getters }
2500+
};
2501+
24962502
/**
24972503
* @param {Pattern} expression_for_id
24982504
* @returns {Binding['mutation']}
@@ -2552,6 +2558,8 @@ export const template_visitors = {
25522558
const index_with_loc = with_loc(index, id);
25532559
return b.call('$.unwrap', index_with_loc);
25542560
};
2561+
2562+
key_state.getters[node.index] = b.id(node.index);
25552563
}
25562564

25572565
/** @type {Statement[]} */
@@ -2565,6 +2573,8 @@ export const template_visitors = {
25652573
true
25662574
)
25672575
);
2576+
2577+
key_state.getters[node.context.name] = node.context;
25682578
} else {
25692579
const unwrapped = getter(binding.node);
25702580
const paths = extract_paths(node.context);
@@ -2592,23 +2602,22 @@ export const template_visitors = {
25922602
if (context.state.options.dev) {
25932603
declarations.push(b.stmt(getter));
25942604
}
2605+
2606+
key_state.getters[name] = path.node;
25952607
}
25962608
}
25972609

25982610
const block = /** @type {BlockStatement} */ (context.visit(node.body, child_state));
25992611

2600-
const key_function = node.key
2601-
? b.arrow(
2602-
[node.context.type === 'Identifier' ? node.context : b.id('$$item'), index],
2603-
declarations.length > 0
2604-
? b.block(
2605-
declarations.concat(
2606-
b.return(/** @type {Expression} */ (context.visit(node.key, child_state)))
2607-
)
2608-
)
2609-
: /** @type {Expression} */ (context.visit(node.key, child_state))
2610-
)
2611-
: b.id('$.index');
2612+
/** @type {Expression} */
2613+
let key_function = b.id('$.index');
2614+
2615+
if (node.key) {
2616+
key_function = b.arrow(
2617+
[node.context, index],
2618+
/** @type {Expression} */ (context.visit(node.key, key_state))
2619+
);
2620+
}
26122621

26132622
if (node.index && each_node_meta.contains_group_binding) {
26142623
// We needed to create a unique identifier for the index above, but we want to use the

0 commit comments

Comments
 (0)