Skip to content

Commit 74f7350

Browse files
committed
more conservative
1 parent 1ae3e09 commit 74f7350

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/** @import { Context } from '../types' */
33
/** @import { Scope } from '../../scope' */
44
import * as e from '../../../errors.js';
5+
import { extract_identifiers } from '../../../utils/ast.js';
56
import { mark_subtree_dynamic } from './shared/fragment.js';
67
import { validate_block_not_empty, validate_opening_tag } from './shared/utils.js';
78

@@ -38,21 +39,32 @@ export function EachBlock(node, context) {
3839
if (node.key) context.visit(node.key);
3940
if (node.fallback) context.visit(node.fallback);
4041

42+
let mutated = false;
43+
4144
// collect transitive dependencies...
4245
for (const binding of node.metadata.expression.dependencies) {
4346
collect_transitive_dependencies(binding, node.metadata.transitive_deps);
4447
}
4548

49+
if (node.context) {
50+
for (const id of extract_identifiers(node.context)) {
51+
const binding = context.state.scope.get(id.name);
52+
if (binding?.mutated) mutated = true;
53+
}
54+
}
55+
4656
// ...and ensure they are marked as state, so they can be turned
4757
// into mutable sources and invalidated
48-
for (const binding of node.metadata.transitive_deps) {
49-
if (
50-
binding.kind === 'normal' &&
51-
(binding.declaration_kind === 'const' ||
52-
binding.declaration_kind === 'let' ||
53-
binding.declaration_kind === 'var')
54-
) {
55-
binding.kind = 'state';
58+
if (mutated) {
59+
for (const binding of node.metadata.transitive_deps) {
60+
if (
61+
binding.kind === 'normal' &&
62+
(binding.declaration_kind === 'const' ||
63+
binding.declaration_kind === 'let' ||
64+
binding.declaration_kind === 'var')
65+
) {
66+
binding.kind = 'state';
67+
}
5668
}
5769
}
5870

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
const { foo } = $state(x());
2+
const { foo } = x();
33
</script>
44

55
{#each foo as f}{/each}

0 commit comments

Comments
 (0)