Skip to content

Commit 0be3ba6

Browse files
committed
properly
1 parent 726c153 commit 0be3ba6

File tree

9 files changed

+55
-39
lines changed

9 files changed

+55
-39
lines changed

packages/svelte/src/compiler/phases/1-parse/state/tag.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ function open(parser) {
144144
}
145145
}
146146

147-
// TODO replace with guaranteed-not-conflicting identifier, or make context possibly null
148-
/** @type {Pattern} */
149-
let context = { type: 'Identifier', start: -1, end: -1, name: '_' };
147+
/** @type {Pattern | null} */
148+
let context = null;
150149
let index;
151150
let key;
152151

@@ -177,10 +176,6 @@ function open(parser) {
177176
}
178177

179178
if (parser.eat('(')) {
180-
if (!has_as) {
181-
e.expected_token(parser.index, '}');
182-
}
183-
184179
parser.allow_whitespace();
185180

186181
key = read_expression(parser);

packages/svelte/src/compiler/phases/2-analyze/visitors/EachBlock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function EachBlock(node, context) {
1616
validate_block_not_empty(node.fallback, context);
1717

1818
const id = node.context;
19-
if (id.type === 'Identifier' && (id.name === '$state' || id.name === '$derived')) {
19+
if (id?.type === 'Identifier' && (id.name === '$state' || id.name === '$derived')) {
2020
// TODO weird that this is necessary
2121
e.state_invalid_placement(node, id.name);
2222
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export function EachBlock(node, context) {
4747

4848
const key_is_item =
4949
node.key?.type === 'Identifier' &&
50-
node.context.type === 'Identifier' &&
51-
node.context.name === node.key.name;
50+
node.context?.type === 'Identifier' &&
51+
node.context?.name === node.key.name;
5252

5353
// if the each block expression references a store subscription, we need
5454
// to use mutable stores internally
@@ -147,7 +147,7 @@ export function EachBlock(node, context) {
147147
// which needs a reference to the index
148148
const index =
149149
each_node_meta.contains_group_binding || !node.index ? each_node_meta.index : b.id(node.index);
150-
const item = node.context.type === 'Identifier' ? node.context : b.id('$$item');
150+
const item = node.context?.type === 'Identifier' ? node.context : b.id('$$item');
151151

152152
let uses_index = each_node_meta.contains_group_binding;
153153
let key_uses_index = false;
@@ -185,7 +185,7 @@ export function EachBlock(node, context) {
185185
if (!context.state.analysis.runes) sequence.push(invalidate);
186186
if (invalidate_store) sequence.push(invalidate_store);
187187

188-
if (node.context.type === 'Identifier') {
188+
if (node.context?.type === 'Identifier') {
189189
const binding = /** @type {Binding} */ (context.state.scope.get(node.context.name));
190190

191191
child_state.transform[node.context.name] = {
@@ -218,7 +218,7 @@ export function EachBlock(node, context) {
218218
};
219219

220220
delete key_state.transform[node.context.name];
221-
} else {
221+
} else if (node.context) {
222222
const unwrapped = (flags & EACH_ITEM_REACTIVE) !== 0 ? b.call('$.get', item) : item;
223223

224224
for (const path of extract_paths(node.context)) {
@@ -260,11 +260,12 @@ export function EachBlock(node, context) {
260260
let key_function = b.id('$.index');
261261

262262
if (node.metadata.keyed) {
263+
const pattern = /** @type {Pattern} */ (node.context); // can only be keyed when a context is provided
263264
const expression = /** @type {Expression} */ (
264265
context.visit(/** @type {Expression} */ (node.key), key_state)
265266
);
266267

267-
key_function = b.arrow(key_uses_index ? [node.context, index] : [node.context], expression);
268+
key_function = b.arrow(key_uses_index ? [pattern, index] : [pattern], expression);
268269
}
269270

270271
if (node.index && each_node_meta.contains_group_binding) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export function EachBlock(node, context) {
2121
state.init.push(b.const(array_id, b.call('$.ensure_array_like', collection)));
2222

2323
/** @type {Statement[]} */
24-
const each = [b.let(/** @type {Pattern} */ (node.context), b.member(array_id, index, true))];
24+
const each = [];
25+
26+
if (node.context) {
27+
each.push(b.let(node.context, b.member(array_id, index, true)));
28+
}
2529

2630
if (index.name !== node.index && node.index != null) {
2731
each.push(b.let(node.index, index));

packages/svelte/src/compiler/phases/scope.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -527,31 +527,33 @@ export function create_scopes(ast, root, allow_reactive_declarations, parent) {
527527
const scope = state.scope.child();
528528
scopes.set(node, scope);
529529

530-
// declarations
531-
for (const id of extract_identifiers(node.context)) {
532-
const binding = scope.declare(id, 'each', 'const');
533-
534-
let inside_rest = false;
535-
let is_rest_id = false;
536-
walk(node.context, null, {
537-
Identifier(node) {
538-
if (inside_rest && node === id) {
539-
is_rest_id = true;
530+
if (node.context) {
531+
// declarations
532+
for (const id of extract_identifiers(node.context)) {
533+
const binding = scope.declare(id, 'each', 'const');
534+
535+
let inside_rest = false;
536+
let is_rest_id = false;
537+
walk(node.context, null, {
538+
Identifier(node) {
539+
if (inside_rest && node === id) {
540+
is_rest_id = true;
541+
}
542+
},
543+
RestElement(_, { next }) {
544+
const prev = inside_rest;
545+
inside_rest = true;
546+
next();
547+
inside_rest = prev;
540548
}
541-
},
542-
RestElement(_, { next }) {
543-
const prev = inside_rest;
544-
inside_rest = true;
545-
next();
546-
inside_rest = prev;
547-
}
548-
});
549+
});
549550

550-
binding.metadata = { inside_rest: is_rest_id };
551-
}
551+
binding.metadata = { inside_rest: is_rest_id };
552+
}
552553

553-
// Visit to pick up references from default initializers
554-
visit(node.context, { scope });
554+
// Visit to pick up references from default initializers
555+
visit(node.context, { scope });
556+
}
555557

556558
if (node.index) {
557559
const is_keyed =

packages/svelte/src/compiler/types/template.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ export namespace AST {
381381
export interface EachBlock extends BaseNode {
382382
type: 'EachBlock';
383383
expression: Expression;
384-
context: Pattern;
384+
/** The `entry` in `{#each item as entry}`. `null` if `as` part is omitted */
385+
context: Pattern | null;
385386
body: Fragment;
386387
fallback?: Fragment;
387388
index?: string;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `<div>hi</div> <div>hi</div> <div>0</div> <div>1</div>`
5+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{#each [10, 20]}
2+
<div>hi</div>
3+
{/each}
4+
5+
{#each [10, 20], i}
6+
<div>{i}</div>
7+
{/each}

packages/svelte/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,8 @@ declare module 'svelte/compiler' {
11881188
export interface EachBlock extends BaseNode {
11891189
type: 'EachBlock';
11901190
expression: Expression;
1191-
context: Pattern;
1191+
/** The `entry` in `{#each item as entry}`. `null` if `as` part is omitted */
1192+
context: Pattern | null;
11921193
body: Fragment;
11931194
fallback?: Fragment;
11941195
index?: string;

0 commit comments

Comments
 (0)