Skip to content

Commit 7af0e60

Browse files
authored
feat: remove $.unwrap calls from each block indexes (#12640)
* remove $.unwrap from key functions * feat: remove `$.unwrap` calls from each block indexes * tweak
1 parent 3bff87a commit 7af0e60

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

.changeset/dry-hotels-matter.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+
feat: remove $.unwrap calls from each block indexes

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: 22 additions & 13 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']}
@@ -2550,8 +2556,12 @@ export const template_visitors = {
25502556
if (node.index) {
25512557
child_state.getters[node.index] = (id) => {
25522558
const index_with_loc = with_loc(index, id);
2553-
return b.call('$.unwrap', index_with_loc);
2559+
return (flags & EACH_INDEX_REACTIVE) === 0
2560+
? index_with_loc
2561+
: b.call('$.get', index_with_loc);
25542562
};
2563+
2564+
key_state.getters[node.index] = b.id(node.index);
25552565
}
25562566

25572567
/** @type {Statement[]} */
@@ -2565,6 +2575,8 @@ export const template_visitors = {
25652575
true
25662576
)
25672577
);
2578+
2579+
key_state.getters[node.context.name] = node.context;
25682580
} else {
25692581
const unwrapped = getter(binding.node);
25702582
const paths = extract_paths(node.context);
@@ -2592,23 +2604,20 @@ export const template_visitors = {
25922604
if (context.state.options.dev) {
25932605
declarations.push(b.stmt(getter));
25942606
}
2607+
2608+
key_state.getters[name] = path.node;
25952609
}
25962610
}
25972611

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

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');
2614+
/** @type {Expression} */
2615+
let key_function = b.id('$.index');
2616+
2617+
if (node.key) {
2618+
const expression = /** @type {Expression} */ (context.visit(node.key, key_state));
2619+
key_function = b.arrow([node.context, index], expression);
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)