Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/dry-hotels-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: remove $.unwrap calls from each block indexes
10 changes: 5 additions & 5 deletions packages/svelte/src/compiler/phases/3-transform/client/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export function serialize_get_binding(node, state) {
return node;
}

if (Object.hasOwn(state.getters, node.name)) {
const getter = state.getters[node.name];
return typeof getter === 'function' ? getter(node) : getter;
}

if (binding.node.name === '$$props') {
// Special case for $$props which only exists in the old world
return b.id('$$sanitized_props');
Expand All @@ -88,11 +93,6 @@ export function serialize_get_binding(node, state) {
return b.call(node);
}

if (Object.hasOwn(state.getters, node.name)) {
const getter = state.getters[node.name];
return typeof getter === 'function' ? getter(node) : getter;
}

if (binding.kind === 'prop' || binding.kind === 'bindable_prop') {
if (is_prop_source(binding, state)) {
return b.call(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2493,6 +2493,12 @@ export const template_visitors = {
getters: { ...context.state.getters }
};

/** The state used when generating the key function, if necessary */
const key_state = {
...context.state,
getters: { ...context.state.getters }
};

/**
* @param {Pattern} expression_for_id
* @returns {Binding['mutation']}
Expand Down Expand Up @@ -2550,8 +2556,12 @@ export const template_visitors = {
if (node.index) {
child_state.getters[node.index] = (id) => {
const index_with_loc = with_loc(index, id);
return b.call('$.unwrap', index_with_loc);
return (flags & EACH_INDEX_REACTIVE) === 0
? index_with_loc
: b.call('$.get', index_with_loc);
};

key_state.getters[node.index] = b.id(node.index);
}

/** @type {Statement[]} */
Expand All @@ -2565,6 +2575,8 @@ export const template_visitors = {
true
)
);

key_state.getters[node.context.name] = node.context;
} else {
const unwrapped = getter(binding.node);
const paths = extract_paths(node.context);
Expand Down Expand Up @@ -2592,23 +2604,20 @@ export const template_visitors = {
if (context.state.options.dev) {
declarations.push(b.stmt(getter));
}

key_state.getters[name] = path.node;
}
}

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

const key_function = node.key
? b.arrow(
[node.context.type === 'Identifier' ? node.context : b.id('$$item'), index],
declarations.length > 0
? b.block(
declarations.concat(
b.return(/** @type {Expression} */ (context.visit(node.key, child_state)))
)
)
: /** @type {Expression} */ (context.visit(node.key, child_state))
)
: b.id('$.index');
/** @type {Expression} */
let key_function = b.id('$.index');

if (node.key) {
const expression = /** @type {Expression} */ (context.visit(node.key, key_state));
key_function = b.arrow([node.context, index], expression);
}

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