Skip to content

Commit f2cca53

Browse files
authored
feat: use implicit return for each block keys (#10938)
* feat: use implicit return for each block keys * swap args * update test
1 parent 808cc6f commit f2cca53

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

.changeset/eleven-beers-yell.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: use implicit return for each block keys

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,11 +2317,13 @@ export const template_visitors = {
23172317
const key_function = node.key
23182318
? b.arrow(
23192319
[node.context.type === 'Identifier' ? node.context : b.id('$$item'), index],
2320-
b.block(
2321-
declarations.concat(
2322-
b.return(/** @type {import('estree').Expression} */ (context.visit(node.key)))
2323-
)
2324-
)
2320+
declarations.length > 0
2321+
? b.block(
2322+
declarations.concat(
2323+
b.return(/** @type {import('estree').Expression} */ (context.visit(node.key)))
2324+
)
2325+
)
2326+
: /** @type {import('estree').Expression} */ (context.visit(node.key))
23252327
)
23262328
: b.literal(null);
23272329

@@ -2347,16 +2349,16 @@ export const template_visitors = {
23472349

23482350
args.push(
23492351
context.state.node,
2350-
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
23512352
b.literal(each_type),
2353+
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
23522354
key_function,
23532355
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children)))
23542356
);
23552357
} else {
23562358
args.push(
23572359
context.state.node,
2358-
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
23592360
b.literal(each_type),
2361+
each_node_meta.array_name ? each_node_meta.array_name : b.thunk(collection),
23602362
b.arrow([b.id('$$anchor'), item, index], b.block(declarations.concat(children)))
23612363
);
23622364
}

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ export function set_current_each_item(item) {
4646
/**
4747
* @template V
4848
* @param {Element | Comment} anchor The next sibling node, or the parent node if this is a 'controlled' block
49-
* @param {() => V[]} get_collection
5049
* @param {number} flags
50+
* @param {() => V[]} get_collection
5151
* @param {null | ((item: V) => string)} get_key
5252
* @param {(anchor: null, item: V, index: import('#client').MaybeSource<number>) => void} render_fn
5353
* @param {null | ((anchor: Node | null) => void)} fallback_fn
5454
* @param {typeof reconcile_indexed_array | reconcile_tracked_array} reconcile_fn
5555
* @returns {void}
5656
*/
57-
function each(anchor, get_collection, flags, get_key, render_fn, fallback_fn, reconcile_fn) {
57+
function each(anchor, flags, get_collection, get_key, render_fn, fallback_fn, reconcile_fn) {
5858
/** @type {import('#client').EachState} */
5959
var state = { flags, items: [] };
6060

@@ -193,28 +193,28 @@ function each(anchor, get_collection, flags, get_key, render_fn, fallback_fn, re
193193
/**
194194
* @template V
195195
* @param {Element | Comment} anchor
196-
* @param {() => V[]} get_collection
197196
* @param {number} flags
197+
* @param {() => V[]} get_collection
198198
* @param {null | ((item: V) => string)} get_key
199199
* @param {(anchor: null, item: V, index: import('#client').MaybeSource<number>) => void} render_fn
200200
* @param {null | ((anchor: Node | null) => void)} [fallback_fn]
201201
* @returns {void}
202202
*/
203-
export function each_keyed(anchor, get_collection, flags, get_key, render_fn, fallback_fn = null) {
204-
each(anchor, get_collection, flags, get_key, render_fn, fallback_fn, reconcile_tracked_array);
203+
export function each_keyed(anchor, flags, get_collection, get_key, render_fn, fallback_fn = null) {
204+
each(anchor, flags, get_collection, get_key, render_fn, fallback_fn, reconcile_tracked_array);
205205
}
206206

207207
/**
208208
* @template V
209209
* @param {Element | Comment} anchor
210-
* @param {() => V[]} get_collection
211210
* @param {number} flags
211+
* @param {() => V[]} get_collection
212212
* @param {(anchor: null, item: V, index: import('#client').MaybeSource<number>) => void} render_fn
213213
* @param {null | ((anchor: Node | null) => void)} [fallback_fn]
214214
* @returns {void}
215215
*/
216-
export function each_indexed(anchor, get_collection, flags, render_fn, fallback_fn = null) {
217-
each(anchor, get_collection, flags, null, render_fn, fallback_fn, reconcile_indexed_array);
216+
export function each_indexed(anchor, flags, get_collection, render_fn, fallback_fn = null) {
217+
each(anchor, flags, get_collection, null, render_fn, fallback_fn, reconcile_indexed_array);
218218
}
219219

220220
/**

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
@@ -10,7 +10,7 @@ export default function Each_string_template($$anchor, $$props) {
1010
var fragment = $.comment($$anchor);
1111
var node = $.first_child(fragment);
1212

13-
$.each_indexed(node, () => ['foo', 'bar', 'baz'], 1, ($$anchor, thing, $$index) => {
13+
$.each_indexed(node, 1, () => ['foo', 'bar', 'baz'], ($$anchor, thing, $$index) => {
1414
var text = $.space_frag($$anchor);
1515

1616
$.render_effect(() => $.set_text(text, `${$.stringify($.unwrap(thing))}, `));

0 commit comments

Comments
 (0)